Rob*_*tan 72 google-calendar-api token gdata access-token
我无法使用我的代码获取刷新令牌.我只能获得我的访问令牌,令牌类型等,我已经按照一些教程,比如access_type=offline输入我的登录URL:
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>";
Run Code Online (Sandbox Code Playgroud)
以及我获取访问令牌的字段:
$fields=array(
'code'=> urlencode($authcode),
'client_id'=> urlencode($clientid),
'client_secret'=> urlencode($clientsecret),
'redirect_uri'=> urlencode($redirecturi),
'grant_type'=> 'authorization_code',
);
Run Code Online (Sandbox Code Playgroud)
但是我无法获得refresh_token,只有access_token,token_type,id_token和expires_in.
Rob*_*tan 96
通过将此添加到您的url参数找到
approval_prompt =力
bos*_*ter 60
如果我可以扩展user987361的答案:
从OAuth2.0文档的离线访问部分:
当您的应用程序收到刷新令牌时,存储该刷新令牌以供将来使用非常重要.如果您的应用程序丢失了刷新令牌,则必须在获得另一个刷新令牌之前重新提示用户同意.如果需要重新提示用户同意,请
approval_prompt在授权代码请求中包含该参数,并将值设置为force.
因此,当您已经授予访问权限时,即使在同意页面的查询字符串中设置了后续请求grant_type,authorization_code也不会返回.refresh_tokenaccess_typeoffline
由于在报价如上所述,为了获得一个新的 refresh_token已经接收到一个后,您将需要通过提示,您可以通过设置做你的用户发回approval_prompt给force.
干杯,
PS此更改也在博客文章中公布.
Ell*_*oad 12
这是access_type=offline你想要的.
这将在用户第一次授权应用程序时返回刷新令牌.后续调用不会强制您重新批准app(approval_prompt=force).
详情请参阅:https: //developers.google.com/accounts/docs/OAuth2WebServer#offline
Sha*_*han 10
这是使用谷歌官方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
Run Code Online (Sandbox Code Playgroud)
对于我们的应用程序,我们必须同时使用这两个参数access_type=offline&prompt=consent。
对我们approval_prompt=force 没有用
嗨,我按照以下步骤操作,我已经能够获得刷新令牌.
授权流程有两个步骤.
是使用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?URL 检索AcessToken和RefreshToken .为此,发送提供以下参数的发布请求.
"code":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 中获取内容 。