Crate graph_rs_sdk

source ·
Expand description

§Microsoft Graph API Client in Rust

graph-rs-sdk is an API client for Microsoft Graph V1.0 and Graph Beta.

Installation and basic usage can be found below and there are extensive examples in the example’s directory on GitHub.

§What APIs are available

The APIs available are generated from Microsoft’s msgraph-metadata repository which stores OpenApi configs for the Graph API. There may be some requests and/or API not yet included in this project but in general most of them are implemented.

§Feature requests or Bug reports.

For bug reports please file an issue on GitHub and a response or fix will be given as soon as possible.

The Discussions tab on GitHub is enabled so feel free to stop by there with any questions or feature requests as well. For bugs, please file an issue first. Other than that feel free to ask questions, provide tips to others, and talk about the project in general.

§Use

The client is async by default and it is recommended to use tokio as the runtime. Tokio is what is used internally and what the project is tested with.

use graph_rs_sdk::*;

#[tokio::main]
async fn main() -> GraphResult<()> {
    let client =  Graph::new("ACCESS_TOKEN");

    let response = client.users()
        .list_user()
        .send()
        .await?;

    println!("{response:#?}");

    let body: serde_json::Value = response.json().await?;
    println!("{body:#?}");

    Ok(())
}

§Using the blocking client

The blocking client can be used by calling into_blocking() on a request.

use graph_rs_sdk::*;

fn main() -> GraphResult<()> {
    let client =  Graph::new("ACCESS_TOKEN");

    let response = client.users()
        .list_user()
        .into_blocking()
        .send()?;

    println!("{response:#?}");

    let body: serde_json::Value = response.json()?;
    println!("{body:#?}");

    Ok(())
}

§Use the Graph version one or beta Api

v1() refers to the endpoint for version 1 of the Microsoft graph API. You can also use the beta() method which uses the Microsoft graph beta API endpoint or use custom_endpoint() for those graph APIs that have custom endpoints such as in countries or governments with their own endpoint.

The Graph client must be mutable in order to change from v1 to beta or a custom endpoint.

§Beta
use graph_rs_sdk::*;

#[tokio::main]
async fn main() -> GraphResult<()> {
    let mut client =  Graph::new("ACCESS_TOKEN");

    let response = client.beta()
        .users()
        .list_user()
        .send()
        .await?;

    println!("{response:#?}");

    let body: serde_json::Value = response.json().await?;
    println!("{body:#?}");

    Ok(())
}
§Custom Endpoint
use graph_rs_sdk::*;

#[tokio::main]
async fn main() -> GraphResult<()> {
    let mut client =  Graph::new("ACCESS_TOKEN");

    let response = client.custom_endpoint("https://api.microsoft.com/api")
        .users()
        .list_user()
        .send()
        .await?;

    println!("{response:#?}");

    let body: serde_json::Value = response.json().await?;
    println!("{body:#?}");

    Ok(())
}
§Custom endpoint using use_endpoint()
use graph_rs_sdk::*;

#[tokio::main]
async fn main() -> GraphResult<()> {
    let mut client =  Graph::new("ACCESS_TOKEN");
    client.use_endpoint("https://graph.microsoft.com");

    let response = client
        .users()
        .list_user()
        .send()
        .await?;

    println!("{response:#?}");

    let body: serde_json::Value = response.json().await?;
    println!("{body:#?}");

    Ok(())
}
  • For more information and examples please see the repository on GitHub
  • If you run into issues related to graph-rs specifically please file an issue on GitHub

§OAuth

OAuth client implementing the OAuth 2.0 and OpenID Connect protocols on Microsoft identity platform

Purpose built as OAuth client for Microsoft Graph and the graph-rs-sdk project. This project can however be used outside graph-rs-sdk as an OAuth client for Microsoft Identity Platform.

§Supported Authorization Flows

§Microsoft OneDrive and SharePoint
§Microsoft Identity Platform

§Example

use graph_rs_sdk::oauth::OAuth;
let mut oauth = OAuth::new();
oauth
    .client_id("<YOUR_CLIENT_ID>")
    .client_secret("<YOUR_CLIENT_SECRET>")
    .add_scope("files.read")
    .add_scope("files.readwrite")
    .add_scope("files.read.all")
    .add_scope("files.readwrite.all")
    .add_scope("offline_access")
    .redirect_uri("http://localhost:8000/redirect")
    .authorize_url("https://login.microsoftonline.com/common/oauth2/v2.0/authorize")
    .access_token_url("https://login.microsoftonline.com/common/oauth2/v2.0/token")
    .refresh_token_url("https://login.microsoftonline.com/common/oauth2/v2.0/token")
    .response_type("code")
    .logout_url("https://login.microsoftonline.com/common/oauth2/v2.0/logout")
    .post_logout_redirect_uri("http://localhost:8000/redirect");

Get the access code for the authorization code grant by sending the user to log in using their browser.

let mut request = oauth.build().authorization_code_grant();
let _ = request.browser_authorization().open();

The access code will be appended to the url on redirect. Pass this code to the OAuth instance:

oauth.access_code("<ACCESS CODE>");

Perform an authorization code grant request for an access token:

let mut request = oauth.build().authorization_code_grant();

let response = request.access_token().send()?;
println!("{:#?}", access_token);

if response.status().is_success() {
    let mut access_token: AccessToken = response.json()?;

    let jwt = access_token.jwt();
    println!("{jwt:#?}");

    // Store in OAuth to make requests for refresh tokens.
    oauth.access_token(access_token);
} else {
    // See if Microsoft Graph returned an error in the Response body
    let result: reqwest::Result<serde_json::Value> = response.json();
    println!("{:#?}", result);
}

Modules§

Structs§

Enums§

Statics§

Traits§

Type Aliases§