获取刷新令牌谷歌API
我无法使用我的代码获取我的刷新标记。 我只能得到我的访问令牌,令牌types等,我已经按照我的loginurlaccess_type =离线一些教程
echo "<a href='https://accounts.google.com/o/oauth2/auth?" . "access_type=offline&client_id=123345555.apps.googleusercontent.com& " . "scope=https://www.googleapis.com/auth/calendar+https://www.googleapis.com/auth/plus.me&response_type=code& " . "redirect_uri=http://www.sample.com/sample.php&state=/profile'>Google</a>"; 和我的领域获取访问令牌
 $fields=array( 'code'=> urlencode($authcode), 'client_id'=> urlencode($clientid), 'client_secret'=> urlencode($clientsecret), 'redirect_uri'=> urlencode($redirecturi), 'grant_type'=> 'authorization_code', ); 
但我无法获取refresh_token,只有access_token,token_type,id_token和expires_in。
通过将这添加到您的url参数中find
approval_prompt =力
如果我可以扩展user987361的答案:
从OAuth2.0文档的脱机访问部分:
当您的应用程序收到刷新令牌时,将该刷新令牌存储以供将来使用非常重要。 如果您的应用程序丢失了刷新标记,那么在获取另一个刷新标记之前,必须重新提示用户同意。 如果需要重新提示用户同意,请在授权码请求中包含
approval_prompt参数,并将该值设置为force。
 因此,即使您在授权页的查询string中将access_type设置为offline ,那么当您已经授予访问权限时,后续对grant_type of authorization_code请求也不会返回refresh_token 。 
 正如上面的引用中所述,为了在已经接收到refresh_token后获得新的 refresh_token ,您需要通过提示信息发回用户,您可以将approval_prompt设置为force 。 
干杯,
PS这个变化也是在一篇博文中宣布的。
 它是access_type=offline ,你想要的。 
 这将在用户第一次授权应用程序时返回刷新令牌。 后续调用不会强制您重新批准应用程序( approval_prompt=force )。 
请参阅更多详细信息: https : //developers.google.com/accounts/docs/OAuth2WebServer#offline
这是使用谷歌官方SDK完整的PHP代码
 $client = new Google_Client(); ## some need parameter $client->setApplicationName('your application name'); $client->setClientId('****************'); $client->setClientSecret('************'); $client->setRedirectUri('http://your.website.tld/complete/url2redirect'); $client->setScopes('https://www.googleapis.com/auth/userinfo.email'); ## these two lines is important to get refresh token from google api $client->setAccessType('offline'); $client->setApprovalPrompt('force'); # this line is important when you revoke permission from your app, it will prompt google approval dialogue box forcefully to user to grant offline access 
嗨,我遵循以下步骤,我已经能够得到刷新令牌。
授权stream程有两个步骤。
- 
是使用 https://accounts.google.com/o/oauth2/auth?获取授权码https://accounts.google.com/o/oauth2/auth?URL。为此,发送发布请求提供以下参数。 'scope=' + SCOPE + '&client_id=' + CLIENTID + '&redirect_uri=' + REDIRECT + '&response_type=' + TYPE + '&access_type=offline'以上提供将获得授权码。
- 
使用 https://accounts.google.com/o/oauth2/token?检索AcessToken和RefreshTokenhttps://accounts.google.com/o/oauth2/token?URL。 为此,发送发布请求提供以下参数。“code”:代码,“client_id”:CID,“client_secret”:CSECRET,“redirect_uri”:REDIRECT,“grant_type”:“authorization_code”, 
因此,在第一次尝试授权时,您将能够获得刷新令牌。 随后的尝试将不会提供刷新令牌。 如果你想要令牌再次撤销你的应用程序的访问。
希望这会帮助别人欢呼:)
OAuth在实模式下有两种情况。 正常和默认的访问方式在线调用。 在某些情况下,当用户不在场时,您的应用程序可能需要访问Google API,这是脱机场景。 在第一授权码交换期间在离线场景中获得刷新令牌。
所以你可以得到referh_token是一些场景,不是全部。
您可以在https://developers.google.com/identity/protocols/OAuth2WebServer#offline中获得该内容。;
 对于我们的应用程序,我们不得不使用这两个参数access_type=offline&prompt=consent 。  approval_prompt=force 不适合我们