无效的附件令牌gmail api

时间:2016-11-09 10:37:52

标签: php gmail-api

我正在尝试从gmail api获取附件 -

https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get#try-it

令牌是主要的用户令牌 - 我可以使用此令牌,ID和消息ID读取电子邮件并且是正确的。

我需要解码附件ID吗? 如果爱上$p->getBody()['attachmentId']

我就会得到

附件ID是 ANGjdJ9jgabAPGnlI7oCAAEvz_Jo-xNYh4-kf9NoCyn-aRPPlf8KuRTsbolmQH0bdDl4Qh3UdfWCBBl8Roaly-rxqRoxTotvIEmls8zqCkFasvFcC-wvQ_6Qun2RM8f8SCDvVpmwguVf​​6fvWfkl1uu5qdu3iR-GzpyU6zLsV0wcwVuiTtPNh8XjAuqKvFk7PaVgDiNW_Lwk_DDEWP8UxfTqw2afanJMNY5GrqPLhga6FmarDIh5AiM67tY6x5Vl

我也尝试在线测试,但有错误 什么是附件令牌?其他令牌?

enter image description here

我在这里测试令牌https://www.googleapis.com/oauth2/v1/tokeninfo?access_token= 这是有效的

"scope": " https://mail.google.com/ https://www.googleapis.com/auth/drive ",
 "expires_in": 3518,
 "verified_email": true,
 "access_type": "offline"

更新1

这个例子

$token = $client->getAccessToken();
$authObj = json_decode($token);
if(isset($authObj->refresh_token)) {
save_refresh_token($authObj->refresh_token);
}


$token = $client->getAccessToken();

是数组而不是json

"access_token" => "ya29.Ci-XA8H0Qq33gA00E92Nx9CQufeG3U4NvyHUFbUUzyXcOEp50FbuK-z1hic8aNbxZg"
  "token_type" => "Bearer"
  "expires_in" => 3600
  "id_token" => ....
"created" => 1479227459

我无法获得refresh_token - 我从auth_code获取access_token

 $client->setApprovalPrompt('force');
            $client->setAccessType ("offline");

            $client->authenticate($tokenCode);

            $token = $client->getAccessToken();

我有令牌,但没有refresh_token为什么?

1 个答案:

答案 0 :(得分:-1)

您应该对Web服务器应用程序使用标准 OAuth 2.0 。您的应用程序接收短期访问令牌,允许它访问这些资源并刷新令牌以允许长期访问,并确保在oauth2授权上添加脱机标记。您将收到一个将在3600秒或一小时后到期的令牌,过期是正常的。因此,您需要使用刷新令牌来获取新的工作令牌。

以下是您需要的步骤:

$token = $client->getAccessToken();
$authObj = json_decode($token);
if(isset($authObj->refresh_token)) {
save_refresh_token($authObj->refresh_token);
}

保存此refresh_token很重要,然后您可以使用

更新它
$client->refreshToken($your_saved_refresh_token);
And then set your new access token to the session:

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

我做了一些研究,发现SO票可以帮助您了解oauth网络流程:How to refresh token with Google API client?

相关问题