msgraph 0.0.1

Microsoft Graph API.


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

Library of Microsoft Graph for D

status main dub downloads BSL-1.0 codecov

This project provides a library to retrieve and manipulate information associated with Office 365 and other accounts through Microsoft Graph.

Usage

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",
}

Authorize

There are two ways.

  • for native application (ex. windows desktop appliation)
  • for service application (ex. web appliation)

Authorize for native application

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.setupWithInstanceServer(authInfo);
}

The clientId and tenantId are created and obtained by the application developer in Asure Active Directory.

Specifically, the setupWithInstanceServer function does the following:

  1. Create a URL based on the client ID and other necessary information, and open a browser with std.process.browse.
  2. The user grants login and access rights on the browser.
  3. If successful, the browser accesses the local server launched ad hoc by redirection.
  4. The redirected query contains an authorize code, which is read.
  5. Use the authorization code to obtain an access token.
  6. 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();

Authorize for service application

Service applications access information without the user's permission and by context without the user. Instead of user permissions, authentication is performed using the clientsecret. The clientsecret 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);
}

Raw query

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.

Reference

Contributing

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.
Authors:
  • SHOO
Dependencies:
none
Versions:
0.0.1 2022-Jul-24
~main 2022-Jul-30
Show all 2 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 2 downloads total

Score:
0.5
Short URL:
msgraph.dub.pm