Google API OAuth2 - 从授权令牌获取刷新令牌
Google API OAuth2 - Get refresh token from Authorization token
按照 Using OAuth 2.0 for Installed Applications 中描述的步骤,我已经能够为我的应用程序获取授权码。
我已在 Google 开发人员控制台中将我的应用程序注册为 OAuth 2.0 客户端 ID:
我正在使用类型 "Other",因为应用程序只需要获取一个新的 access_token(使用 refresh_token),而不会使用任何类型的用户同意。
安全性无关紧要,因为它将是一个私有应用程序。
应用程序需要能够读取和写入电子表格,以及运行 Google 链接到电子表格的 Apps 脚本。
这在范围内都是可能的 "https://www.googleapis.com/auth/spreadsheets", according to Google's OAuth 2.0 Playground:
我已经能够使用以下请求(按照 this 步骤)获得授权码:
https://accounts.google.com/o/oauth2/v2/auth?
scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&
redirect_uri=urn:ietf:wg:oauth:2.0:oob&
response_type=code&
client_id=#####.apps.googleusercontent.com
将此 URL 粘贴到我的浏览器中,它会将我重定向到此页面:
通过这种方式,我获得了授权码,理论上可以将其兑换成refresh_token和access_token。
我尝试模仿 Google 的 OAuth 2.0 Playground 在交换 refresh_token 和 access_token 的授权码时所做的请求:
它向以下 URL 发送一个 POST 请求:
https://www.googleapis.com/oauth2/v3/token?
code={auth_code}&
redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&
client_id=#####.apps.googleusercontent.com&
client_secret=#####&
scope=&
grant_type=authorization_code
当我尝试执行此请求时,我收到错误 400 和以下响应:
{"error": "invalid_request", "error_description": "Invalid parameter value for redirect_uri: Missing scheme: {redirect_uri}}
它为 redirect_uri 抛出关于 "Missing scheme" 的错误。在我看来这并不奇怪,因为我的 应用程序类型是 "Other",而你 不能用它授权重定向 URI类型。
我已经尝试了 OAuth2ForDevices(正是我想要的),但我无法使用它Google 的电子表格。
使用授权码获取 refresh_token(和 access_token)的正确方法是什么,通过客户端 ID 类型 "Other"(其中可用于 Google 电子表格)?
我想通了。
使用此请求:
https://accounts.google.com/o/oauth2/v2/auth?
scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&
redirect_uri=urn:ietf:wg:oauth:2.0:oob&
response_type=code&
client_id=#####.apps.googleusercontent.com
此 returns 授权码。
然后,提出以下 POST 请求:
POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded
code={auth_code}&
client_id=######.apps.googleusercontent.com&
client_secret=#####&
redirect_uri=urn:ietf:wg:oauth:2.0:oob&
grant_type=authorization_code
如果您不使用 "urn:ietf:wg:oauth:2.0:oob" 作为您的 redirect_uri,它将不起作用。这没有在 OAuth2InstalledApp Guide (it uses "https://oauth2-login-demo.appspot.com/code 中作为 redirect_uri 的示例说明,这让我很困惑。
简答:将"urn:ietf:wg:oauth:2.0:oob"用作redirect_uri
使用您用于调用的相同 redirect_uri:
https://accounts.google.com/o/oauth2/v2/auth
Google 不会对该 uri 发出任何进一步的请求,但我认为它会将其用于匹配目的,作为一些小的附加安全性。
按照 Using OAuth 2.0 for Installed Applications 中描述的步骤,我已经能够为我的应用程序获取授权码。
我已在 Google 开发人员控制台中将我的应用程序注册为 OAuth 2.0 客户端 ID:
我正在使用类型 "Other",因为应用程序只需要获取一个新的 access_token(使用 refresh_token),而不会使用任何类型的用户同意。
安全性无关紧要,因为它将是一个私有应用程序。
应用程序需要能够读取和写入电子表格,以及运行 Google 链接到电子表格的 Apps 脚本。
这在范围内都是可能的 "https://www.googleapis.com/auth/spreadsheets", according to Google's OAuth 2.0 Playground:
我已经能够使用以下请求(按照 this 步骤)获得授权码:
https://accounts.google.com/o/oauth2/v2/auth?
scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&
redirect_uri=urn:ietf:wg:oauth:2.0:oob&
response_type=code&
client_id=#####.apps.googleusercontent.com
将此 URL 粘贴到我的浏览器中,它会将我重定向到此页面:
通过这种方式,我获得了授权码,理论上可以将其兑换成refresh_token和access_token。
我尝试模仿 Google 的 OAuth 2.0 Playground 在交换 refresh_token 和 access_token 的授权码时所做的请求:
它向以下 URL 发送一个 POST 请求:
https://www.googleapis.com/oauth2/v3/token?
code={auth_code}&
redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&
client_id=#####.apps.googleusercontent.com&
client_secret=#####&
scope=&
grant_type=authorization_code
当我尝试执行此请求时,我收到错误 400 和以下响应:
{"error": "invalid_request", "error_description": "Invalid parameter value for redirect_uri: Missing scheme: {redirect_uri}}
它为 redirect_uri 抛出关于 "Missing scheme" 的错误。在我看来这并不奇怪,因为我的 应用程序类型是 "Other",而你 不能用它授权重定向 URI类型。
我已经尝试了 OAuth2ForDevices(正是我想要的),但我无法使用它Google 的电子表格。
使用授权码获取 refresh_token(和 access_token)的正确方法是什么,通过客户端 ID 类型 "Other"(其中可用于 Google 电子表格)?
我想通了。
使用此请求:
https://accounts.google.com/o/oauth2/v2/auth?
scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&
redirect_uri=urn:ietf:wg:oauth:2.0:oob&
response_type=code&
client_id=#####.apps.googleusercontent.com
此 returns 授权码。 然后,提出以下 POST 请求:
POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded
code={auth_code}&
client_id=######.apps.googleusercontent.com&
client_secret=#####&
redirect_uri=urn:ietf:wg:oauth:2.0:oob&
grant_type=authorization_code
如果您不使用 "urn:ietf:wg:oauth:2.0:oob" 作为您的 redirect_uri,它将不起作用。这没有在 OAuth2InstalledApp Guide (it uses "https://oauth2-login-demo.appspot.com/code 中作为 redirect_uri 的示例说明,这让我很困惑。
简答:将"urn:ietf:wg:oauth:2.0:oob"用作redirect_uri
使用您用于调用的相同 redirect_uri:
https://accounts.google.com/o/oauth2/v2/auth
Google 不会对该 uri 发出任何进一步的请求,但我认为它会将其用于匹配目的,作为一些小的附加安全性。