【问题标题】:Getting Bad Request Response with graph.microsoft.com, missing UPN and PUID claims使用 graph.microsoft.com 获得错误的请求响应,缺少 UPN 和 PUID 声明
【发布时间】:2016-01-30 04:07:37
【问题描述】:

我们正在尝试使用 Office 365 统一 API 向 graph.microsoft.com 发出请求。

身份验证成功,但访问令牌缺少 UPN 和 PUID,这意味着针对 https://graph.microsoft.com/beta/me 的请求失败。

验证码:

$code = $_GET['code'];
//build the request body
$tokenRequestBody = "grant_type=authorization_code&" .
    "redirect_uri=" . '<redirectURI>' . "&" .
    "client_id=" . '<cliendId>' . "&" .
    "client_secret=" . urlencode('<clientsecret>') . "&" .
    "resource=" . 'https://graph.microsoft.com' . "&" .
    "code=" . $code;


$request = curl_init("https://login.microsoftonline.com/0e06e1f9-24b3-4026-8bd0-2a6c28937df1/oauth2/token");
curl_setopt($request, CURLOPT_POST, 1);
curl_setopt($request, CURLOPT_POSTFIELDS, $tokenRequestBody);
curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

curl_setopt($request, CURLOPT_RETURNTRANSFER, true);

$tokenOutput = curl_exec($request);
$token = json_decode($tokenOutput);

图表请求代码:

    $path = "https://graph.microsoft.com/beta/me";

    //perform a REST query for the user
    $request = curl_init($path);
    curl_setopt($request, CURLOPT_HTTPHEADER, array(
        "Authorization: Bearer ".$token->access_token
    ,
        "Accept: application/json"));

    curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($request);

解码的访问令牌:

{
 typ: "JWT",
 alg: "RS256",
 x5t: "MnC_VZcATfM5pOYiJHMba9goEKY",
 kid: "MnC_VZcATfM5pOYiJHMba9goEKY"
}.
{
 aud: "https://graph.microsoft.com",
 iss: "https://sts.windows.net/0e06e1f9-24b3-4026-8bd0-2a6c28937df1/",
 iat: 1447345801,
 nbf: 1447345801,
 exp: 1447349701,
 acr: "1",
 altsecid: "1:live.com:0003BFFD977FF496",
 amr: [
  "pwd"
 ],
 appid: "<appid>",
 appidacr: "1",
 email: "<emailaddress>",
 family_name: "<familyname>",
 given_name: "<givenname>",
 idp: "live.com",
 ipaddr: "<ipaddress>",
 scp: "Calendars.Read Calendars.ReadWrite Contacts.Read Contacts.ReadWrite Directory.AccessAsUser.All Directory.Read.All Directory.ReadWrite.All Files.Read Files.Read.Selected Files.ReadWrite Files.ReadWrite.Selected Group.Read.All Group.ReadWrite.All Mail.Read Mail.ReadWrite Mail.Send Notes.Create Notes.Read Notes.Read.All Notes.ReadWrite Notes.ReadWrite.All Notes.ReadWrite.CreatedByApp offline_access openid People.Read People.ReadWrite Sites.Read.All Sites.ReadWrite.All User.Read User.Read.All User.ReadBasic.All User.ReadWrite User.ReadWrite.All",
 sub: "5je0Jdv8442iS3rLXa-3a7KWSiKCyBrq9Q0c0d4sbBY",
 tid: "0e06e1f9-24b3-4026-8bd0-2a6c28937df1",
 unique_name: "<uniquename>",
 ver: "1.0"
}.
[signature]

图表请求响应:

{
  "error": {
    "code": "BadRequest",
    "message": "Missing UPN and PUID claims.",
    "innerError": {
      "request-id": "158c62f6-fece-4f64-bbb5-a1e691334daa",
      "date": "2015-11-12T14:09:40"
    }
  }
}

希望对此有所帮助!提前谢谢。

【问题讨论】:

  • 仍然有这个问题 - 我希望其中一位天蓝色的家伙有机会看看,因为支持合同在预览模式下不涵盖统一 API。
  • 我也遇到了同样的问题,你解决了吗?

标签: php azure office365 azure-active-directory


【解决方案1】:

您可以检查以下几点以进行故障排除:

1、在你的AD应用面板中,点击USERS选项卡,查看用户是否在你的Azure AD中。

2,请检查从on-promise AD或Office 365到Azure AD的用户同步过程是否成功。您可以使用 office 365 帐户登录 Azure 门户,首次登录 Azure 门户时会自动将其添加到 Azure AD 中。此外,如果您遇到错误或 UPN 有冲突,您可能会遇到此错误。

您可以在 Powershell 中尝试以下脚本添加您的应用程序服务主体并为您的应用程序设置角色:

#-----------------------------------------------------------
# This will add your Application Service Prinicpal to 
# the Company Administrator role
#-----------------------------------------------------------
$msolcred=get-credential
connect-msolservice -credential $msolcred

$ClientIdWebApp = 'Your application client id'
$webApp = Get-MsolServicePrincipal –AppPrincipalId $ClientIdWebApp

#use Add-MsolRoleMember to add it to “Company Administrator” role).
Add-MsolRoleMember -RoleName "Company Administrator" -RoleMemberType ServicePrincipal -RoleMemberObjectId $webApp.ObjectId 

有关如何使用 Azure AD Powershell 的更多信息,您可以参考Manage Azure AD service principals

此外,您可以参考Deep Dive into the Office 365 Unified API 集成office 365,并检查第一节中提到的先决条件。

【讨论】:

    【解决方案2】:

    您似乎正在尝试使用应用程序身份进行访问。 应用程序没有 Me 端点。 此外,您需要向应用程序添加应用程序权限。

    【讨论】:

      【解决方案3】:

      虽然 Microsoft Graph 的预览版支持使用 Microsoft ID 进行身份验证,但截至 2016 年 1 月,它目前不支持使用 altsecid 声明通过 AAD 进行 Microsoft ID 身份验证。我们正在努力解决这个问题。

      【讨论】:

      猜你喜欢
      • 2016-05-04
      • 2019-01-26
      • 2016-08-29
      • 2014-08-13
      • 1970-01-01
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多