【问题标题】:Google oauth2.0 405 error谷歌 oauth2.0 405 错误
【发布时间】:2014-07-24 10:53:26
【问题描述】:

我尝试使用以下链接使用 google oauth,但收到 405 错误,

请告诉我参数是否正确?

client_id = changed to a diff value
response_type = code
scope= openid%20email
redirecturl = given the value based on what I registered in console.developers.com
login_hint = my gmail id..

https://accounts.google.com/o/oauth2/token?
 client_id=690178314820-85fvo4eq56se4mppdaf0pt6tnnjo552&
 response_type=code&
 scope=openid%20email&
 redirect_uri=http://test.webfactional.com&
 state=security_token%3D138r5719ru3e1%26url%3Dhttps://oa2cb.example.com/myHome&
 login_hint=myemail@gmail.com

我在浏览器中进行了上述获取请求..

【问题讨论】:

    标签: oauth google-api google-oauth


    【解决方案1】:

    您似乎使用了错误的api,您应该使用https://accounts.google.com/o/oauth2/auth而不是https://accounts.google.com/o/oauth2/token

    你得到错误405的原因是https://accounts.google.com/o/oauth2/token只能通过POST调用,它是为了获取令牌。您需要先获取授权码,然后将其换成令牌。

    【讨论】:

    • 谢谢....accounts.google.com/o/oauth2/token会给我token...我将如何获得授权...需要使用什么url链接?
    • 使用https://accounts.google.com/o/oauth2/auth?client_id=YOUR_CLIENTID&response_type=code&scope=YOUR_SCOPE&redirect_uri=YOUR_REDIRECTURI可以参考this
    • 我使用上面的链接更改令牌通过 auth 但它失败了
    【解决方案2】:

    有几个步骤可以让我更轻松地访问 Google,我会向您展示完整的流程。我的猜测是您被困在第二步,因为您没有将其作为帖子发送。

    第 1 步:请求访问权限

    https://accounts.google.com/o/oauth2/auth?client_id={clientid}.apps.googleusercontent.com&redirect_uri={From console}&scope=openid%20email&response_type=code
    

    这只是显示要求他们批准您的窗口。用户批准访问后,您将获得一次性验证码。

    第 2 步:Authentication Code 交换为 AccessTokenRefreshToken。请注意,这需要作为 HTTP POST 而不是 HTTP Get 发送。

    https://accounts.google.com/o/oauth2/token
    code={Authentication Code from step 1}&client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&redirect_uri=={From console}&grant_type=authorization_code
    

    你应该得到一个看起来像这样的 JSon 字符串。

    {
    "access_token" : "ya29.1.AADtN_VSBMC2Ga2lhxsTKjVQ_ROco8VbD6h01aj4PcKHLm6qvHbNtn-_BIzXMw",
    "token_type" : "Bearer",
    "expires_in" : 3600,
    "refresh_token" : "1/J-3zPA8XR1o_cXebV9sDKn_f5MTqaFhKFxH-3PUPiJ4"
    }
    

    现在您可以使用 Access_token 并使用它来提出您的请求。但是访问令牌只能使用 1 小时,然后它们会在此之前过期,您需要使用 Refresh_token 来获取新的访问令牌。此外,如果您要再次访问您的用户数据,您应该将 refresh_token 保存在某个位置,以便您始终可以访问那里的数据。

    第 3 步:使用 Refreshtoken

    https://accounts.google.com/o/oauth2/token
    client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&refresh_token={RefreshToken from step 2}&grant_type=refresh_token
    

    这一次您只会取回访问令牌,因为您的 refreshtoken 是好的,直到用户删除身份验证或您已经 6 个月没有使用它。

    {
    "access_token" : "ya29.1.AADtN_XK16As2ZHlScqOxGtntIlevNcasMSPwGiE3pe5ANZfrmJTcsI3ZtAjv4sDrPDRnQ",
    "token_type" : "Bearer",
    "expires_in" : 3600
    }
    

    您可以在这里找到更多详细信息Google 3 Legged oauth2 flow

    【讨论】:

    • 第一步工作正常..第二步连接正常,但我得到以下响应..access_token、token_type、expires_in 和 id_token..我没有看到任何刷新令牌.. id_token 可以认为是刷新令牌吗?
    • 我设置了 'access_type':'offline' 作为谷歌文档中指定的获取 refresh_token 但我收到以下错误“”error_description”:“此消息类型不允许使用参数:access_type”\n }'"
    • 这个还是要我Accept,没有这个有选项吗?
    【解决方案3】:

    请注意这个 /oauth2/v3/token 和 /oauth2/token

    我确实在 link 遵循谷歌指南

    它显示我关注

    1. 通过 /o/oauth2/auth 获取验证码 => 有效,响应如指南中的示例

    2. 通过/oauth2/v3/token获取访问令牌=>报错,响应状态码405

    正确的一定是/oauth2/token

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-11
      • 2018-11-02
      • 2012-10-28
      • 1970-01-01
      • 2018-12-15
      • 2021-07-28
      • 1970-01-01
      • 2018-11-27
      相关资源
      最近更新 更多