首页 文章

Google API Rest:用于刷新和访问令牌的Exchange授权代码 - 错误404

提问于
浏览
3

我在逐步执行"Using OAuth 2.0 for Web Server Applications"帮助页面时遇到404错误 . Using OAuth 2.0 for Web Server Applications

我使用http / rest请求 .

在步骤5:“用于刷新和访问令牌的Exchange授权代码”之前,一切正常 .

我的请求(POST或GET)返回404错误:“Not Found” . 这是我的要求,与指南中的相同:

https://www.googleapis.com/oauth2/v4/token?
code=4/_XXXXXXXXXXXXXXXXXXX__XXXXXXXXXXXXXX-XXXXXX?&client_id=012345678912-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.apps.googleusercontent.com&client_secret=XXXXXXXXXXXXXXXXXXX_XXXX&grant_type=authorization_code&redirect_uri=http%3a%2f%2fwww.mydomain.com%3a50000%2fMyPage

以下是指南的相关部分:

“要交换访问令牌的授权码,请调用https://www.googleapis.com/oauth2/v4/token endpoints 并设置以下参数:字段代码从初始请求返回的授权码.client_id客户端从API控制台获取的ID.client_secret从API控制台获取的客户端密钥.redirect_uri API控制台中为项目列出的重定向URI之一.grant_type如OAuth 2.0规范中所定义,此字段必须包含authorization_code值 . “

我认为服务URL存在问题 . 我错了吗?

任何人都可以提供工作网址吗?

2 回答

  • 3

    工作要求将是

    POST /oauth2/v4/token HTTP/1.1
    Host: www.googleapis.com
    Content-length: 233
    content-type: application/x-www-form-urlencoded
    user-agent: google-oauth-playground
    code=4%2FKxoYTS-jeq5-d6Lv7YvSz9ZrK0pJ_5lZsMExzNC1M0o&redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&client_id=407408718192.apps.googleusercontent.com&client_secret=************&scope=&grant_type=authorization_code
    

    注意:-

    • 这是一个POST,所以 code= 等是请求正文,而不是URL .

    • 因为它是一个帖子,请确保内容类型 Headers 是正确的,即 . "application/x-www-form-urlencoded"

    • 如果访问代码包含非字母数字,例如 . 第二个字符中的"/",需要进行URL编码 . 这通常由您正在使用的http库完成(例如,jquery,Angular $ http等) . 检查电线是否正确完成

    • 我非常怀疑授权码是否包含问号 . 如果(并且仅当)它,它需要进行URL编码 .

  • 2

    auth流程中的第二步是HTTP POST .

    https://www.googleapis.com/oauth2/v4/token
    code={The code from step one}&client_id={ClientId}&client_secret={ClientSecret}&redirect_uri={RedirectURI}&grant_type=authorization_code
    

    我看到的主要区别是你在URL的末尾添加了 ? ,这是不需要的 . 请记住,您需要完整地发布字符串,并将&s显示为字符串 .

    这是我在Google 3 Legged OAuth2 Flow上的教程,它可能有所帮助

相关问题