Google OAuth 安装的应用程序流程:redirect_uri_mismatch
Google OAuth Installed Application Flow: redirect_uri_mismatch
我正在尝试遵循此指南:Using OAuth 2.0 for Installed Applications
我通过了第一部分 OK,用户可以授权我的应用程序访问他们的 Google 驱动器。用户授权后我已成功获取授权码
不幸的是,我卡在了第二部分:发送 POST 请求以交换访问令牌的授权代码时,我一直遇到 redirect_uri_mismatch
错误。
指南说我应该使用 "The redirect URI you obtained from the Developers Console"。我在开发者控制台中从这个 link 下载了凭据:
其中包含 JSON 以及以下 属性:
"redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]
我已经尝试了对每个选项的请求设置 redirect_uri
;我试过将它设置为空字符串;我也尝试过完全丢失它。没有任何效果。
每次我都遇到 redirect_uri_mismatch
错误。
我也试过从下载的凭据发帖到这个URL:
"token_uri":"https://accounts.google.com/o/oauth2/token"
还有指南页面中的 URL (https://www.googleapis.com//oauth2/v4/token),但每次我都遇到同样的错误。
如何解决此错误并成功交换访问令牌的授权码?
来自 Fiddler 的屏幕截图示例显示了我尝试过的众多请求之一:
或原始 HTTP 请求示例:
POST https://accounts.google.com/o/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: accounts.google.com
Content-Length: 253
Expect: 100-continue
Connection: Keep-Alive
client_id=175836713882-m783k1ksu2rc6vepq35j4o8hhpk94ndj.apps.googleusercontent.com&client_secret=A---removed-for-security---A&code=4---removed-for-security---E&grant_type=authorization_code&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob
N.B。我知道关于 Web App Authentication flow but this specifically relates to the Installed application flow 之后的重定向 URI miss-spells 的错误,有很多关于 SO 的已回答问题。我在已安装的应用程序流程中找不到与此错误消息相关的已回答问题
当授权请求中设置的重定向 URI 和令牌请求中设置的重定向 URI 不相同(必须与上面列出的值之一完全匹配)时,会发生此错误。
恕我直言,您应该首先验证授权请求中的 redirect_uri
参数。它应该是这样的:
https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=175836713882-m783k1ksu2rc6vepq35j4o8hhpk94ndj.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob
我正在尝试遵循此指南:Using OAuth 2.0 for Installed Applications
我通过了第一部分 OK,用户可以授权我的应用程序访问他们的 Google 驱动器。用户授权后我已成功获取授权码
不幸的是,我卡在了第二部分:发送 POST 请求以交换访问令牌的授权代码时,我一直遇到 redirect_uri_mismatch
错误。
指南说我应该使用 "The redirect URI you obtained from the Developers Console"。我在开发者控制台中从这个 link 下载了凭据:
其中包含 JSON 以及以下 属性:
"redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]
我已经尝试了对每个选项的请求设置 redirect_uri
;我试过将它设置为空字符串;我也尝试过完全丢失它。没有任何效果。
每次我都遇到 redirect_uri_mismatch
错误。
我也试过从下载的凭据发帖到这个URL:
"token_uri":"https://accounts.google.com/o/oauth2/token"
还有指南页面中的 URL (https://www.googleapis.com//oauth2/v4/token),但每次我都遇到同样的错误。
如何解决此错误并成功交换访问令牌的授权码?
来自 Fiddler 的屏幕截图示例显示了我尝试过的众多请求之一:
或原始 HTTP 请求示例:
POST https://accounts.google.com/o/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: accounts.google.com
Content-Length: 253
Expect: 100-continue
Connection: Keep-Alive
client_id=175836713882-m783k1ksu2rc6vepq35j4o8hhpk94ndj.apps.googleusercontent.com&client_secret=A---removed-for-security---A&code=4---removed-for-security---E&grant_type=authorization_code&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob
N.B。我知道关于 Web App Authentication flow but this specifically relates to the Installed application flow 之后的重定向 URI miss-spells 的错误,有很多关于 SO 的已回答问题。我在已安装的应用程序流程中找不到与此错误消息相关的已回答问题
当授权请求中设置的重定向 URI 和令牌请求中设置的重定向 URI 不相同(必须与上面列出的值之一完全匹配)时,会发生此错误。
恕我直言,您应该首先验证授权请求中的 redirect_uri
参数。它应该是这样的:
https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=175836713882-m783k1ksu2rc6vepq35j4o8hhpk94ndj.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob