2017-04-11 61 views
1

我目前正在嘗試使用Azure實現一種同步我的客戶端的Outlook日曆的PHP應用程序日曆API。 我用的OAuth2和Microsoft provider by Steven Maguire. 我目前的一個問題運行自定義,我在我的迴應得到一個錯誤:Outlook - OAuth 2 - 無法檢索訪問令牌,grant_type =密碼不受支持,只有授權代碼和refresh_token是

{「錯誤」:「unsupported_grant_type」,「ERROR_DESCRIPTION」:「爲輸入參數提供的值'grant_type'無效,期望值如下:'authorization_code','refresh_token'。「}

我很難理解爲什麼不支持grant_type密碼,即使它在文檔Azure,它是。

請求看起來像這樣:

CLIENT_ID = 44bef79b - ********************** & client_secret = H ***** *********** & redirect_uri = https%3A%2F%2F192.168.1.123%2Fmapeyral%2Fcalendarsync.php & grant_type = password & username = ************ ****** &密碼= *********** &範圍=的OpenID%20profile%20offline_access%20Calendars.ReadWrite

使用的授權網址爲:https://login.live.com/oauth20_token.srf ,如Steven Maguire提供商所定義。 標題包含內容類型的應用程序/ x-www-form-urlencoded(我見過很多帖子,這是導致錯誤的原因)。

我的一些代碼:

$this->provider = new Microsoft([ 
     'clientId'    => MicrosoftGraphConstants::CLIENT_ID, 
     'clientSecret'   => MicrosoftGraphConstants::CLIENT_SECRET, 
     'redirectUri'    => MicrosoftGraphConstants::REDIRECT_URI, 
     'urlAuthorize'   => MicrosoftGraphConstants::AUTHORITY_URL . MicrosoftGraphConstants::AUTHORIZE_ENDPOINT, 
     'urlAccessToken'   => MicrosoftGraphConstants::AUTHORITY_URL . MicrosoftGraphConstants::TOKEN_ENDPOINT, 
     'urlResourceOwnerDetails' => MicrosoftGraphConstants::RESOURCE_ID, 
     'scope'     => MicrosoftGraphConstants::SCOPES 
    ]); 

if ($_SERVER['REQUEST_METHOD'] === 'GET' && !isset($_GET['code'])) 
    { 
     // Try getting access token from Database 
     $workingAccount = $GLOBALS['AppUI']->getState('working_account'); 
     if (isset($workingAccount)) 
     { 
     // DB access 
     $DB = new DatabaseConnection(); 
     $dbAccess = $DB->getConnection(); 

     $contactData = DBUserUtils::getContactDataFromEmail($GLOBALS['AppUI']->getState('working_account'), $dbAccess); 

     // If at least one user contact found 
     if (!is_null($contactData)) 
     { 
      // If has refresh token => fill session variables using refresh token 
      if (!is_null($contactData['contact_refreshToken'])) 
      { 
      log_msg('debug.log', 'Has refresh token'); 
      $GLOBALS['AppUI']->setState('preferred_username', $contactData['contact_email']); 
      $GLOBALS['AppUI']->setState('given_name', $contactData['contact_first_name']." ".$contactData['contact_last_name']); 
      // Get new tokens 
      $newAccessToken = $this->provider->getAccessToken('refresh_token', [ 
       'refresh_token' => $contactData['contact_refreshToken'] 
      ]); 

      // Update tokens and DB 
      $GLOBALS['AppUI']->setState('refresh_token', $newAccessToken->getRefreshToken()); 
      $GLOBALS['AppUI']->setState('access_token', $newAccessToken->getToken()); 

      DBOAuthUtils::updateTokenForUser($contactData['contact_id'], $GLOBALS['AppUI']->getState('refresh_token'), $dbAccess); 

      $this->redirectTo($redirectURL); 
      } 
      else 
      { 
      $this->getAccessToken(); 

      } 
     } 
     else 
     { 
      $this->getAccessToken(); 
     } 
     } 
     else 
     { 
     $this->getAccessToken(); 
     } 

function getAccessToken(){ 
$accessToken = $this->provider->getAccessToken('password', [ 
       'username' => '*************', 
       'password' => '********', 
       'scope' => MicrosoftGraphConstants::SCOPES 
      ]); 
} 

在第一次嘗試不通過,如果(isset($ workingAccount))條件(如預期),直接進入到最後的人。 代碼現在有點難看,但我認爲它對我的問題沒有影響。

任何幫助,將不勝感激! 感謝

編輯:添加代碼

+0

請顯示您的代碼。 –

+0

這是我的代碼示例,但它已完成。數據庫的東西是無用的,因爲它是用於當用戶已經認證自己 – iddys

+0

正確的,所以你實際上是特別告訴lib使用密碼授權類型。正如錯誤消息所述,這不被它們支持。唯一允許的是授權代碼(忽略刷新,這是以後需要查看的)類型,在您發佈的回購鏈接的自述文件中有一個示例。 –

回答

0

,幫助我,問題是,我需要使用Azure的Active Directory和不Azure的AD 2.0。 問題解決了!

+0

請接受您自己的答案,以便其他人可以從中受益。謝謝 –

+1

@iddys能否請您詳細描述一下,如何從Ad2.0更改爲Azure Active Directory? – Gaurav

+0

@Gaurav爲你幸運我最終沒有使用Azure Active Directory,我找到了另一種方法來做我想做的與AD 2.0,所以我不能幫你:/祝你好運! – iddys

相關問題