[英]Access Token error - Graph API getting user profile
我正在使用Microsoft graph API進行登錄並獲取用戶個人資料。 我有accessToken。 雖然我試圖獲取獲得AccessToken的用戶的個人資料。
這是我的代碼,我在這里遺漏了什么嗎? 只需要用戶個人資料。 注意:我正在通過代理服務器在任何地方使用Cors,該服務器已用於獲取代碼和accessToken。
謝謝你的幫助!
我嘗試添加資源URL。 我嘗試過更改標頭(您不需要GET
和DEL
請求的主體參數)。
let auth =
"http://localhost:8080/https://graph.microsoft.com/v1.0/me?access_token=";
let client_id = "?client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
let redirect_uri = "&redirect_uri=http://localhost:8100/";
let response = "&response_type=code";
let resource = "&resource=https://graph.microsoft.com";
let scope =
"&scope=openid+https://outlook.office.com/Contacts.read+offline_access";
let url =
auth + token + resource + client_id + redirect_uri;
//let url = 'http://localhost:8080/https://graph.microsoft.com/v1.0/me?access_token=' + token +"&resource=https://graph.microsoft.com";
this.http.get(url, {
headers: {
Authorization: "Bearer " + token,
"Content-Type": "application/x-www-form-urlencoded",
"Access-Control-Allow-Origin": "*",
resource: "https://graph.microsoft.com"
}
});
預期:像第4部分中一樣,獲取AccessToken並獲取用戶個人資料。
您這里發生了很多事情。
您要同時指定scope
和resource
屬性。 這些不屬於一起。 如果使用的是v1端點,則應該使用resource
;如果使用的是v2端點,則應該使用scope
。 請參閱文檔中的范圍,而不是資源 。
您的url
不正確。 v1的實際網址應如下所示:
https://login.microsoftonline.com/common/oauth2/authorize?client_id={id}&scope=https%3A%2F%2Fgraph.microsoft.com&redirect_uri={uri}&response_type=code
或對於v2,如下所示:
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={id}&scope=openid+user.read+contacts.read+offline_access&redirect_uri={uri}&response_type=code
不能為此使用http.get()
。 OAuth的授權碼授予通過將用戶重定向到此URL開始。 然后,它會返回code
,那么你POST
回/token
端點檢索access_token
和refresh_token
。
您需要User.Read
范圍來檢索用戶的配置文件(或User.ReadBasic.All
來檢索其他用戶的配置文件)。
我建議使用v2端點並從此處開始:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.