简体   繁体   English

使用客户端凭据流访问Office365 API

[英]Accessing Office365 API with Client Credentials Flow

I am trying to develop a node application that would be able to access my Outlook.com mails. 我正在尝试开发一个能够访问我的Outlook.com邮件的节点应用程序。

I am trying to do it in a way it doens't require me to enter my credentials, the application will know them (user name and password). 我正在尝试以一种不需要输入我的凭据的方式进行操作,应用程序将知道它们(用户名和密码)。 I am not too worried about storing them in the config of my application. 我不太担心将它们存储在应用程序的配置中。

I am using simple-oauth2 but I keep getting an error. 我正在使用simple-oauth2,但始终收到错误消息。 The following is the code that is trying to retrieve the Oauth token: 以下是尝试检索Oauth令牌的代码:

    const credentials = {
        client: {
            id: this.appId,
            secret: this.appSecret,
        },
        auth: {
            tokenHost: "https://login.microsoftonline.com",
            authorizePath: "common/oauth2/v2.0/authorize",
            tokenPath: "common/oauth2/v2.0/token",
        },
    };

    const oathClient = oauth2.create(credentials);
    const tokenConfig =  {
        username: "zzz@outlook.com",
        password: "xxxxx",
        scope: "Mail.Read",
    };

    const result = await oathClient.ownerPassword.getToken(tokenConfig);
    const token = oathClient.accessToken.create(result);

However when calling get token I get the following response: 但是,当调用get令牌时,我得到以下响应:

   "error": "invalid_grant",
    "error_description": "AADSTS70000: The grant is not supported by this API version\r\nTrace ID: 91935472-5d7b-4210-9a56-341fbda12a00\r\nCorrelation ID: 6b075f4e-b649-493e-a87b-c74f0e427b47\r\nTimestamp: 2017-08-19 14:00:33Z",
    "error_codes": [ 70000],

I have aded the application in apps.dev.microsoft.com 我在apps.dev.microsoft.com中添加了应用程序

Added a platform (Web API) for it. 为此添加了一个平台(Web API)。 Added the "Mail.Read" permission on Microsoft Grah 在Microsoft Grah上添加了“ Mail.Read”权限

And I am using the apikey and secret I generated there. 我正在使用我在那里生成的apikey和秘密。

Googling looks like all the examples I find are to connect is using a client certificate. 谷歌搜索看起来像我发现要连接的所有示例都是使用客户端证书。 Is it possible to use the API using the API credentials? 是否可以通过API凭据使用API​​?

If the only way is using certificates, is there a way I can use simple-oauth2 for that? 如果唯一的方法是使用证书,那么有什么方法可以使用simple-oauth2吗?

Ok, looks like I was using the wrong method. 好的,看来我使用了错误的方法。

Trying to access using the ClientCredentials module on simple-ouath2 and its workign now: 现在尝试在simple-ouath2及其工作上使用ClientCredentials模块进行访问:

    const tokenConfig =  {
        scope: "https://graph.microsoft.com/.default",
    };

    const result = await oathClient.clientCredentials.getToken(tokenConfig);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM