php - Unable to get Google API access token using auth code - redirect uri mismatch -
i'm trying use google api php client library create folder in users drive space. unfortunately keep getting "redirect_uri_mistmatch bad request" error.
i've looked @ several posts trying resolve issue no avail. steps i've taken being,
- verified urls correct , current client id in google developer console.
- clearing , re-entering said urls in dev console.
- updating client-secret.json file , manually verifying urls making file.
- switched drive_client->authenticate() fetchaccesstokenwithauthcode() error reporting.
the code spread across 3 different files end requiring each other @ different points during execution - if makes difference, though i've tried combining 1 file , still had same issue. server running behind cloudflare if makes difference, toggle developer mode when ever working on this.
generate oauth request , redirect user:
$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 else header("location: " . $authurl); exit();
call back
$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 error
exact error
array(2) { ["error"]=> string(21) "redirect_uri_mismatch" ["error_description"]=> string(11) "bad request" }
i left out code creating actual file since it's not @ issue (no don't try , create folder before retrieving access token), other stuff in between make calls database. thanks!
i had similar issue before , because redirect uri not being set on call back. please add following line:
$this->_google_client->setredirecturi("https://example.org/accounts/settings/oauthcallback.php");
to callback, after line:
$this->_google_client->setaccesstype("offline");
so should be:
$this->_google_client->setincludegrantedscopes(true); $this->_google_client->setaccesstype("offline"); $this->_google_client->setredirecturi("https://example.org/accounts/settings/oauthcallback.php");
i hope helps.
Comments
Post a Comment