2017-09-13 4 views
1

Google API PHPクライアントライブラリを使用して、ユーザードライブスペースにフォルダを作成しようとしています。残念ながら、私は "redirect_uri_mistmatch Bad Request"エラーを受けています。認証コードを使用してGoogle APIアクセストークンを取得できません - URLの不一致をリダイレクト

私は、この問題を無駄に解決しようとしているいくつかの投稿を見てきました。私はすでにビーイングを撮影した段階では、

  • 確認済みのURLがすべて正しいとGoogle デベロッパーコンソールでの私のクライアントIDの現在のです。
  • 上記のURlsをクリアしてdevコンソールに再入力する。
  • 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"); 

私はそれが役に立てば幸い。

関連する問題