Nodejs获取Microsoft Graphics Token

2022年1月14日 1276点热度 0人点赞 0条评论

问题描述

最近想要使用 dkatavic/onedrive-api: OneDrive API module for Node.js (github.com) 包获取 onedrive 文件,需要自行获取 access_token,简介中介绍可以使用 lelylan/simple-oauth2: A simple Node.js client library for Oauth2 (github.com) 包来获取 Microsoft Graphics 的 Access Token。

解决方案

这里做的是一个控制台程序,E5开发者订阅,使用应用程序权限。

https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps 注册应用。

API 权限添加 Microsoft Graphics 应用程序权限。

<client_id> 是应用程序(客户端) ID

<client_secret> 是客户端密码

<tenant_id> 是分配的域名 xxx.onmicrosoft.com

const { ClientCredentials } = require('simple-oauth2');

const config = {
    client: {
        id: '<client_id>',
        secret: '<client_secret>',
    },
    auth: {
        tokenHost: 'https://login.microsoftonline.com',
        authorizePath: "<tenant_id>/oauth2/v2.0/authorize",
        tokenPath: "<tenant_id>/oauth2/v2.0/token",
    }
};
const client = new ClientCredentials(config);

const tokenParams = {
    scope: 'https://graph.microsoft.com/.default',
};

client.getToken(tokenParams).then(result => {

    const accessToken = result.token

    console.log("Token:", accessToken)

}).catch(error => {

    console.log(error)
})

KAMINO

这个人很懒,什么都没留下

文章评论