【问题标题】:400 Bad Request when sending http post request to get token from auth code?发送http post请求以从身份验证代码获取令牌时出现400错误请求?
【发布时间】:2013-12-11 18:26:17
【问题描述】:

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

    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 服务器收到授权码后,可以用授权码交换访问令牌和刷新令牌。该请求是一个 HTTPS 帖子,包含以下参数:

字段说明 code 初始请求返回的授权码 client_id 应用注册时获取的client_id client_secret 应用注册时获取的客户端秘钥 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

【问题讨论】:

    标签: oauth-2.0 google-oauth


    【解决方案1】:

    我得到了这个工作..我正在为那些坚持这个的人分享代码:

    $.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);
            }
        });
    

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

    【讨论】:

    • 谢谢。这帮助了我。我忘记在内容类型中添加“charset=utf-8”,它解决了我的问题。
    • 如果您解决了您的问题,请发布解决方案。
    • Google 服务器需要 Json。文档中的错误。发送 json 而不是表单编码。但将内容类型保持为表单编码
    • @pamu 你能发布一个完整的答案吗?我尝试发送json。但没有运气。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 2021-12-21
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    相关资源
    最近更新 更多