2017-09-13 183 views
1

我試圖使用Google API PHP客戶端庫在用戶驅動器空間中創建文件夾。不幸的是,我不斷收到「redirect_uri_mistmatch錯誤請求」錯誤。無法使用授權碼獲取Google API訪問令牌 - 重定向uri不匹配

我看了幾個帖子試圖解決問題無濟於事。我已經採取的步驟,

  • 已驗證的網址對我的客戶端ID在Google 開發人員控制檯中都是正確的和最新的。
  • 在dev控制檯中清除並重新輸入所述URls。
  • 正在更新client-secret.json文件並手動驗證該URL是否爲 。
  • 從drive_client-> authenticate()切換到fetchAccessTokenWithAuthCode()以進行錯誤報告。

代碼是跨所有最終執行過程中需要彼此在不同的點3個不同的文件傳播 - 如果有差別,雖然我已經竭盡所能組合成1個文件,仍然有同樣的問題。 我的服務器也在CloudFlare後面運行,如果這有所幫助,但是我在開始工作時切換開發者模式。

產生OAuth請求,並重定向用戶:

$this->_Google_Client = new Google_Client(); 
$this->_Google_Client->setAuthConfig("path/to/client_secret....json"); 
$this->_Google_Client->setIncludeGrantedScopes(true); 
$this->_Google_Client->setAccessType("offline"); 
$this->_Google_Client->addScope(Google_Service_Drive::DRIVE_FILE); 
$this->_Google_Client->setRedirectUri("https://example.org/accounts/settings/oAuthCallback.php"); 
$authUrl = $this->_Google_Client->createAuthUrl(); 

$_SESSION["oAuth_Action"] = "GDrive_API_Setup";//Used internally for something else 
header("Location: " . $authUrl); 
exit(); 

回撥

$code = @$_GET["code"]; 
$this->_Google_Client = new Google_Client(); 
$this->_Google_Client->setAuthConfig("path/to/client_secret....json"); 
$this->_Google_Client->setIncludeGrantedScopes(true); 
$this->_Google_Client->setAccessType("offline"); 
$this->_Google_Client->addScope(Google_Service_Drive::DRIVE_FILE); 
$accessToken = $this->_Google_Client->fetchAccessTokenWithAuthCode($code); 
$this->_Google_Client->setAccessToken($accessToken); 

echo var_dump($accessToken) . " -- " . $code; //Debug where I get error 

確切的錯誤

array(2) { ["error"]=> string(21) "redirect_uri_mismatch" ["error_description"]=> string(11) "Bad Request" } 

我遺漏了用於創建實際文件的代碼,因爲它沒有問題(不,我不嘗試在檢索訪問令牌之前創建文件夾),以及其中一些其他內容,我在其中對數據庫進行一些調用。 謝謝!

回答

0

我以前有類似的問題,這是因爲重定向uri沒有被設置回調。能否請您添加以下行:

$this->_Google_Client->setRedirectUri("https://example.org/accounts/settings/oAuthCallback.php"); 

回調,該行之後:

$this->_Google_Client->setAccessType("offline"); 

所以,它應該是:

$this->_Google_Client->setIncludeGrantedScopes(true); 
$this->_Google_Client->setAccessType("offline"); 
$this->_Google_Client->setRedirectUri("https://example.org/accounts/settings/oAuthCallback.php"); 

我希望幫助。

相關問題