Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

oauth 2.0 - 400 Bad Request when sending http post request to get token from auth code?

I am trying to access accounts.google.com to get token from authorization code received using HTTP post request.

    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);

        }
    });

Error:

"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"

Request:

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

here is the document which i am using: After the web server receives the authorization code, it may exchange the authorization code for an access token and a refresh token. This request is an HTTPs post, and includes the following parameters:

Field Description code The authorization code returned from the initial request client_id The client_id obtained during application registration client_secret The client secret obtained during application registration redirect_uri The URI registered with the application grant_type As defined in the OAuth 2.0 specification, this field must contain a value of authorization_code The actual request might look like:

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

A successful response to this request contains the following fields:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got this working.. i am sharing the code for those who are stuck with this:

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

but now i have new issue. The url get 200 OK response but i am not getting response at all

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...