2016-11-25 78 views
4

我正在做服务器端oauth,遵循this指南。没有在YouTube上获得刷新令牌OAuth

我顺利地完成了OAuth的,但我没有收到refresh_token交易授权代码更新和访问令牌步:

请求:

POST /o/oauth2/token HTTP/1.1 
HOST: accounts.google.com 
content-type: application/x-www-form-urlencoded 
content-length: 260 

code=4/KEOuzih9jwfnHj7Rl1DeqHhcJF0goKPwtwR5IQ09ieg&client_id=****.apps.googleusercontent.com&client_secret=****&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2FsSignIn.html&grant_type=authorization_code 

响应:

{ 
    "access_token" : "****", 
    "expires_in" : 3580, 
    "token_type" : "Bearer" 
} 

我错过了什么吗?

回答

3

有两件事情需要做:

  1. 要获得刷新令牌,你必须通过access_type=offline的查询参数的OAuth开始请求。这将确保您在第一次为帐户执行oauth时获得刷新令牌。
  2. 要在同一个帐户上一次又一次地执行oauth获取刷新标记,必须将prompt=consent作为查询参数传递给oauth启动请求。

参考:https://developers.google.com/identity/protocols/OAuth2WebServer#offline

+1

感谢阿布舍克!我离线传递了access_type,但我没有启用提示同意。现在它返回刷新标记。 – jremi