2016-11-09 103 views
-1

我試圖從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

I測試令牌這裏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

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

      $client->authenticate($tokenCode); 

      $token = $client->getAccessToken(); 

ACCESS_TOKEN我有令牌,但沒有refresh_token爲什麼呢?

回答

-1

對於Web服務器應用程序,您應該使用標準OAuth 2.0。您的應用程序會收到短期訪問令牌,它可以訪問這些資源並刷新令牌以允許長期訪問,並確保您在oauth2授權中添加脫機標誌。您將收到一個將在3600秒或1小時內過期的令牌,並且過期正常。所以你需要使用刷新令牌來獲得新的工作令牌。

這裏有您需要的步驟:

$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 web流程:How to refresh token with Google API client?