繁体   English   中英

访问令牌错误-Graph API获取用户个人资料

[英]Access Token error - Graph API getting user profile

我正在使用Microsoft graph API进行登录并获取用户个人资料。 我有accessToken。 虽然我试图获取获得AccessToken的用户的个人资料。

这是我的代码,我在这里遗漏了什么吗? 只需要用户个人资料。 注意:我正在通过代理服务器在任何地方使用Cors,该服务器已用于获取代码和accessToken。

谢谢你的帮助!

我尝试添加资源URL。 我尝试过更改标头(您不需要GETDEL请求的主体参数)。

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并获取用户个人资料。

您这里发生了很多事情。

  1. 您要同时指定scoperesource属性。 这些不属于一起。 如果使用的是v1端点,则应该使用resource ;如果使用的是v2端点,则应该使用scope 请参阅文档中的范围,而不是资源

  2. 您的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 
  3. 不能为此使用http.get() OAuth的授权码授予通过用户重定向到此URL开始。 然后,它会返回code ,那么你POST/token端点检索access_tokenrefresh_token

  4. 您需要User.Read范围来检索用户的配置文件(或User.ReadBasic.All来检索其他用户的配置文件)。

我建议使用v2端点并从此处开始:

暂无
暂无

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

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