Microsoft Graph API请求上的InvalidAuthenticationToken

时间:2017-06-09 10:47:41

标签: php windows api microsoft-graph

我想从Microsoft Graph API获取登录用户的基本配置文件信息,但是我收到以下错误消息:

{ "error": 
{ "code": "InvalidAuthenticationToken", 
 "message": "Access token validation failure.", 
 "innerError": { 
  "request-id": "[HIDDEN]", 
  "date": "2017-06-09T10:39:13" 
  } 
 }
}

我使用以下PHP代码:

<?php
$redirectUri = 'http%3A%2F%2Flocalhost%3A8888%2Flogin%2F';
$clientId = [HIDDEN];
 $clientSecret = [HIDDEN];

// Get authorization code
 if (!isset($_GET['code'])) {

 header('Location: https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id='.$clientId.'&redirect_uri='.$redirectUri.'&response_type=code&state=1234&scope=openid');
  exit;

} else {

  // Get access code
  $authCode = $_GET['code'];

  $ch = curl_init();

  $headers = array();
  $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  $postFields = 'grant_type=authorization_code&code='.$authCode.'&redirect_uri='.$redirectUri.'&scope=openid&client_id='.$clientId.'&client_secret='.$clientSecret;

  curl_setopt($ch, CURLOPT_URL,'https://login.microsoftonline.com/common/oauth2/v2.0/token');
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  $serverOutput = curl_exec ($ch);
  curl_close ($ch);

  $jsonOutput = json_decode($serverOutput, true);

 // Get user details
  $chUser = curl_init();

  $headersUser[] = 'Authorization: Bearer '.$jsonOutput['id_token'];

  curl_setopt($chUser, CURLOPT_URL,'https://graph.microsoft.com/v1.0/me/');
  curl_setopt($chUser, CURLOPT_CUSTOMREQUEST, 'GET');
  curl_setopt($chUser, CURLOPT_HTTPHEADER, $headersUser);

  $serverOutputUser = curl_exec($chUser);

  curl_close($chUser);

  var_dump($serverOutputUser);

}

有人可以帮我解决这个问题吗?

0 个答案:

没有答案