oauth-2.0 - 发送 http post 请求以从身份验证代码获取 token 时出现 400 Bad Request?

标签 oauth-2.0 google-oauth

我正在尝试访问accounts.google.com,以从使用HTTP post请求收到的授权代码中获取 token 。

    var searchurl = "https://accounts.google.com/o/oauth2/token";

    $.ajax({
        dataType: "json",
        url:searchurl,
        data: {code:auth_code, client_id:'client_id', client_secret:'secret', redirect_uri:'http%3A%2F%2Flocalhost:8085%2FGmailIntegration%2FgetAuthResponse1.jsp', grant_type:'authorization_code'},
        type:"Post",
        contentType:"application/x-www-form-urlencoded",
        success:function(data) {
            alert(data);
        },
        error: function(jqXHR, exception) {
            console.log(jqXHR);

        }
    });

错误:

"NetworkError: 400 Bad Request - https://accounts.google.com/o/oauth2/token?
 code=4/PlKII3f0vsPUhl1QNIUXkiIhlfGA.sq9lFf-oCiIcXE-sT2ZLcbRFnpEphQI&client_id={clientid}   
 &client_secret={secret}&redirect_uri=https://oauth2-login-
 demo.appspot.com/code&grant_type=authorization_code"

请求:

Response Headers
Alternate-Protocol  443:quic
Cache-Control   no-cache, no-store, max-age=0, must-revalidate
Content-Encoding    gzip
Content-Type    application/json
Date    Tue, 26 Nov 2013 14:20:56 GMT
Expires Fri, 01 Jan 1990 00:00:00 GMT
Pragma  no-cache
Server  GSE
X-Firefox-Spdy  3
X-Frame-Options SAMEORIGIN
X-XSS-Protection    1; mode=block
x-content-type-options  nosniff

Request Header:
Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control   no-cache
Connection  keep-alive
Content-Length  0
Content-Type    application/x-www-form-urlencoded
Host    accounts.google.com
Origin  http://localhost:8085
Pragma  no-cache

这是我正在使用的文档: Web服务器接收到授权码后,可以将授权码交换为访问 token 和刷新 token 。该请求是一个HTTPs post,包含以下参数:

字段说明 code 初始请求返回的授权码 client_id 应用注册时获取的client_id client_secret 应用程序注册时获取的客户端 key redirect_uri 向应用程序注册的 URI grant_type 根据OAuth 2.0规范中的定义,该字段必须包含authorization_code的值 实际的请求可能如下所示:

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code

对此请求的成功响应包含以下字段:

Field   Description
access_token    The token that can be sent to a Google API
refresh_token   A token that may be used to obtain a new access token. Refresh tokens are valid until the user revokes access. This field is only present if access_type=offline is included in the authorization code request.
expires_in  The remaining lifetime on the access token
token_type  Indicates the type of token returned. At this time, this field will always have the value Bearer

最佳答案

我成功了..我正在为那些受此困扰的人分享代码:

$.ajax({
        dataType: "json",
        url:searchurl,
        data: {code:code, client_id:'clientid', client_secret:'secret', redirect_uri:'http://localhost:8085/GmailIntegration/getAuthResponse.jsp', grant_type:'authorization_code'},
        type:"POST",
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        crossDomain:true,
        cache : true, 
        success:function(data) {
            alert(data);
        },
        error: function(jqXHR, exception, errorstr) {
            console.log(jqXHR);
            alert(errorstr);
        }
    });

但现在我有新问题。该网址得到 200 OK 响应,但我根本没有得到响应

enter image description here

关于oauth-2.0 - 发送 http post 请求以从身份验证代码获取 token 时出现 400 Bad Request?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20219994/

相关文章:

spring-boot - 分离 OAuth2 授权服务器和资源服务器

angular - 如何使用正确的 header 使用 Angular 6 发送 OAuth2 POST 请求?

spring-boot - 如何在 Spring 中使用 OAuth2 和 JWT token 代表特定用户调用 protected 资源?

oauth-2.0 - WSO2:端点是一个休息服务,有自己的 oauth2 服务。是否有可能在 WSO2 中为一个 API 禁用身份验证?

google-oauth - 为了获取我的市场应用程序的 OAuth2 凭据,需要什么?

google-oauth - 让 Cloud Run 服务成为 Apps Script 项目目标的困难 - 受众和范围

google-oauth - 当我尝试在我的网络服务上授权 Google Assistant 时,发生 "Accounts failed to link"错误

azure - AADSTS90002 : Tenant 'xx' not found. 如果租户没有事件订阅,则可能会发生这种情况

google-chrome - G+ 登录时 Chrome 未捕获错误 'Protocols must match'

javascript - Google OAuth2 - 使用外部生成的访问 token - 使用 JS 客户端库