This project provides a library to retrieve and manipulate information associated with Office 365 and other accounts through Microsoft Graph.
msgraph's API lists are here
If you are using dub, you can add a dependency by describing it as follows:
dub add msgraph
Or you can add it directly to the project file.
"dependencies": {
"msgraph": "~>0.0.1",
}
There are two ways.
- for native application (ex. windows desktop appliation)
- for service application (ex. web appliation)
In native application, the application obtains permission from the user and accesses information using the user's context. To obtain permission, the user must go through an authentication sequence once in the web browser. The following code authenticates the user by signing in and granting permission to obtain the information necessary to execute the API.
import msgraph;
void main()
{
AuthInfo authInfo;
with (authInfo)
{
tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
requireScope = ["offline_access", "User.Read"];
}
auto graph = Graph();
graph.setupWithAdHocServer(authInfo);
}
The clientId and tenantId are created and obtained by the application developer in Asure Active Directory.
Specifically, the setupWithAdHocServer function does the following:
- Create a URL based on the client ID and other necessary information, and open a browser with std.process.browse.
- The user grants login and access rights on the browser.
- If successful, the browser accesses the local server launched ad hoc by redirection.
- The redirected query contains an authorize code, which is read.
- Use the authorization code to obtain an access token.
- Thereafter, Graph API can be used by communicating with an access token added to the HTTP header.
Thereafter, access tokens can be updated periodically to reduce the need to re-signin, as shown in the following code:
graph.updateTokens();
Service applications access information without the user's permission and by context without the user. Instead of user permissions, authentication is performed using the client_secret. The client_secret must be stored securely in a location accessible only to the service administrator. (For this reason, it cannot be used in native applications.) The following code will authenticate with the client secret and obtain an access token:
import msgraph;
void main()
{
AuthInfo authInfo;
with (authInfo)
{
tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxx";
}
auto graph = Graph(authInfo);
}
After obtaining an access token, the API is handled by doing the following:
import std.json, std.stdio;
auto res = graph.get("/me/");
auto userInfo = parseJSON(cast(const(char)[])res.responseBody);
writeln(userInfo["displayName"].str);
The API can be tried in Graph Explorer.
And see also the reference for details.
- Microsoft Graph API reference
- Microsoft Graph auth overview
- Use the Microsoft Graph API
- Graph Explorer
- msgraph's API
This project accepts Issue reports and PullRequests. The PullRequest must pass all tests in CI of GitHub Actions. First, make sure that your environment passes the test with the following commands.
rdmd scripts/runner.d -m=ut # or dub test
rdmd scripts/runner.d -m=it # or dub build / test / run for all ./testcases/* directories.