简体   繁体   中英

Google OAuth access token not refresh token NULL

I'm trying to get my access token on my server to use the GMail API from the PHP client library and all I get is NULL after I var_drump the variable I have store the access token in through the getAccessToken(); method.

Any idea what I am doing wrong so that how I can an access token?

I have a valid auth code in the URL with the code parameter and I don't know why I am getting null when I try to fetch the access token. Any ideas?

Here is my code:

require_once 'vendor/autoload.php';
$redirect_uri = 'https://website.com/m/?mail=tokened';
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
var_dump($access_token);

My further google searching discovered this: Google API - request for token from Oauth2 returns null token

And I tried the following based on that code because this is what would run without errors, not what was exactly in the answer, and I still am getting NULL

This time I tried doing authorization code and fetching access token from it all on the server side, the only difference is that this time it does ask for permission to access gmail data.

require_once 'vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setScopes('https://mail.google.com');
if($_GET['mail']=='approved'){
    $client->setRedirectUri('https://website.com/m/php/googleTokens.php?mail=tokened');
    return header('Location: ' . $client->createAuthUrl());
}
else{
    $client->authenticate($_GET['code']);
    $tokens = $client->getAccessToken();
    var_dump($tokens);
}

Let's make sure we are following the authentication flow properly. First, the client sends an authentication request to Google's OAuth system and then Google returns an access code which later on you can exchange for an access token. The logic of the process should be like this:

require_once 'vendor/autoload.php'; //Include PHP Client Library

//Create client object and set its configuration
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/index.php');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->addScope(array("email", "profile"));

//Check if the access token is already set and if it is, var dump access token
if(isset($_SESSION["access_token"]) && $_SESSION["access_token"] ) {

    $client->setAccessToken($_SESSION['access_token']);

    var_dump($_SESSION['access_token']);

} else { // if access token is not set, authenticate client

  if( !isset($_GET["code"]) ) { // if there is no access code

    $auth_url = $client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));

  } else { //if there is an access code

    $client->authenticate($_GET['code']); //authenticate client
    $_SESSION['access_token'] = $client->getAccessToken(); //save access token to session
    $redirect_uri = "http://".$_SERVER['HTTP_HOST']."/index.php";
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));

  }
}

Before running the logic, please go to myaccount.google.com/permissions and delete the application, then run the above code. Finally, please lets not forget to review the official documentation for a more detailed explanation. There are also several examples of this here on stackoverflow so I would recommend checking them too. I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM