Php Google OAuth访问令牌未刷新令牌NULL

Php Google OAuth访问令牌未刷新令牌NULL,php,oauth,google-api,google-oauth,google-api-php-client,Php,Oauth,Google Api,Google Oauth,Google Api Php Client,我试图在我的服务器上获取访问令牌,以使用PHP客户端库中的GMail API,在我通过getAccessToken()对存储访问令牌的变量进行varu_drump之后,得到的结果是NULL方法 你知道我做错了什么吗?我怎么才能得到一个访问令牌 我在URL中有一个带有code参数的有效身份验证代码,我不知道在尝试获取访问令牌时为什么会出现null。有什么想法吗 这是我的密码: require_once 'vendor/autoload.php'; $redirect_uri = 'https://

我试图在我的服务器上获取访问令牌,以使用PHP客户端库中的GMail API,在我通过
getAccessToken()对存储访问令牌的变量进行varu_drump之后,得到的结果是NULL方法

你知道我做错了什么吗?我怎么才能得到一个访问令牌

我在URL中有一个带有code参数的有效身份验证代码,我不知道在尝试获取访问令牌时为什么会出现null。有什么想法吗

这是我的密码:

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);
我的进一步谷歌搜索发现:

我尝试了以下基于该代码的代码,因为这是运行时不会出现错误的代码,而不是答案中的代码,我仍然会得到NULL

这次我尝试在服务器端编写授权代码并从中获取访问令牌,唯一的区别是这次它确实请求访问gmail数据的权限

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);
}

让我们确保正确地遵循身份验证流程。首先,客户端向Google的OAuth系统发送一个身份验证请求,然后Google返回一个访问代码,稍后您可以交换访问令牌。流程的逻辑应如下所示:

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));

  }
}

在运行逻辑之前,请转到并删除应用程序,然后运行上述代码。最后,请不要忘了回顾,以获得更详细的解释。在stackoverflow上也有几个这样的例子,所以我建议也检查它们。我希望这有帮助

谢谢你,但我进入了谷歌登录的重定向循环,你认为这正常吗?因为当我删除if(!isset($\u GET[“code”])部分时,它会工作perfectly@AITMANSOURMohamed唯一的解释是访问令牌没有保存到
$\u会话
,否则它将打印
$\u会话['access\u token']
信息,