简体   繁体   English

使用纯 JavaScript 获取 Microsoft 图形 API 的访问令牌

[英]Get access token for Microsoft graph api using plain JavaScript

I try to fetch planner data using Microsoft graph api using ajax call But I am getting https://graph.microsoft.com/v1.0/me/planner/tasks 400 (Bad Request):我尝试使用 Microsoft 图形 API 使用 ajax 调用获取规划器数据但我收到https://graph.microsoft.com/v1.0/me/planner/tasks 400(错误请求):

function requestToken() {
        $.ajax({
            "async": true,
            "crossDomain": true,
            "url": "https://cors-anywhere.herokuapp.com/https://login.microsoftonline.com/common/oauth2/v2.0/token", // Pass your tenant name instead of sharepointtechie
            "method": "POST",
            "headers": {
                "content-type": "application/x-www-form-urlencoded"
            },
            "data": {
                "grant_type": "client_credentials",
                "client_id ": "--REDACTED--", //Provide your app id
                "client_secret": "--REDACTED--",

                                     //Provide your client secret genereated from your app
                "scope ": "https://graph.microsoft.com/.default"
            },
            success: function (response) {
                console.log(response);
                token = response.access_token;


                $.ajax({
                    url: 'https://graph.microsoft.com/v1.0/me/planner/tasks',
                    type: 'GET',
                    dataType: 'json',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('Authorization', 'Bearer '+token+'');
                    },
                    data: {},
                    success: function (results) {                            
                        console.log(results);
                        debugger;
                    },
                    error: function (error) {
                        console.log("Error in getting data: " + error);
                    }
                });
            }

        })
    }

Looking for json data from planner but getting error code https://graph.microsoft.com/v1.0/me/planner/tasks 400 (Bad Request) while fetching from graph api.从规划器中查找 json 数据,但在从图形 api 获取时获取错误代码https://graph.microsoft.com/v1.0/me/planner/tasks 400(错误请求)。

Firstly there are a couple of major issues with the code you've shared right now:首先,您现在共享的代码存在几个主要问题:

  1. You should not be using Client Credentials Grant, ie clientId and client secret to make the call to Microsoft Graph API from your client side JavaScript code, it's only meant for confidential clients like a daemon or service.您不应使用 Client Credentials Grant,即 clientId 和 client secret 从客户端 JavaScript 代码调用 Microsoft Graph API,它仅适用于像守护程序或服务这样的机密客户端。

  2. You are anyways trying to hit the endpont https://graph.microsoft.com/v1.0/me/planner/tasks , which includes the keyword me that is valid only for a user identity.无论如何,您都在尝试访问端点https://graph.microsoft.com/v1.0/me/planner/tasks ,其中包含仅对用户身份有效的关键字me So you should try to acquire token using identity of the user who is currently signed in or prompt the user if they're not signed in.因此,您应该尝试使用当前登录的用户的身份获取令牌,或者在用户未登录时提示用户。

You can make use of Microsoft Graph JavaScript Client Library to call Microsoft Graph.您可以使用Microsoft Graph JavaScript 客户端库来调用 Microsoft Graph。

The link for client library also provides good step-by-step guidance with sample code.客户端库的链接还通过示例代码提供了良好的分步指导。

NOTE : please don't put your client secret or any other sensitive information as part of your question on stackoverflow.注意:请不要将您的客户机密或任何其他敏感信息作为您在 stackoverflow 上的问题的一部分。 I'll edit the question for now, but you should still delete this particular secret for your application and generate new ones for any future use.我现在会编辑这个问题,但您仍然应该为您的应用程序删除这个特定的秘密,并生成新的秘密以供将来使用。

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

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