错误403使用PHP刷新GMAIL令牌

时间:2015-08-04 12:06:02

标签: php gmail gmail-api

我试图从Gmail收到电子邮件,但我无法刷新令牌。我也跟着这个问题,但我不知道为什么它不起作用

How to refresh token with Google API client?

我就是这样做的:

$email="xxxx@gmail.com"; //It can't be "me";

$client = new Google_Client();
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->addScope(Google_Service_Gmail::MAIL_GOOGLE_COM);
$client->setRedirectUri($this->redirect_url);
$client->setState('offline');
$client->setAccessType('offline');
$client->setLoginHint($email);

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

if($client->isAccessTokenExpired())
{
    $access_token= json_decode($gmail['access_token'],true);
    $refresh=$client->refreshToken($access_token["refresh_token"]);
    $new_token_array_str=$client->getAccessToken();

    /*
    Old Token ($access_token):
    ------------------
    array (size=5)
      'access_token' => string 'ya29.xQE0___' (length=73)
      'token_type' => string 'Bearer' (length=6)
      'expires_in' => int 3600
      'refresh_token' => string '1/M57-GPcccc' (length=66)
      'created' => int 1438679813

    New token:
    ----------
    array (size=3)
      'access_token' => string 'ya29.xQEllll-jjj-jjj' (length=73)
      'expires_in' => int 3600
      'created' => int 1438689530
    */
}

$j=0;
do 
{
    try
    {
        $messagesResponse = $service->users_messages->listUsersMessages($email, $opt_param);
        if ($messagesResponse->getMessages()) 
        {
            $messages = array_merge($messages, $messagesResponse->getMessages());
            $pageToken = $messagesResponse->getNextPageToken();
        }
        $j++;
    }
    catch (Exception $e)
    {
        print 'An error occurred: ' . $e->getMessage();
    }
}while ($pageToken);

这就是我得到的:

  

发生错误:调用GET时出错   https://www.googleapis.com/gmail/v1/users/xxxx%40gmail.com%20/messages?q=in%3Ainbox:   (403)xxxx@gmail.com的代表团被拒绝

第一次,我收到了电子邮件。我的问题是当令牌访问令牌过期时,我不知道它为什么不起作用。

0 个答案:

没有答案