400 Fehlerhafte Anfrage beim Senden einer http-Post-Anfrage, um ein Token vom Authentifizierungscode zu erhalten?

Ich versuche, auf accounts.google.com zuzugreifen, um das Token aus dem Autorisierungscode zu erhalten, der über die HTTP-Post-Anfrage empfangen wurde.

    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"

Anfordern:

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

Hier ist das Dokument, das ich verwende: Nachdem der Webserver den Autorisierungscode erhalten hat, kann er den Autorisierungscode gegen ein Zugriffstoken und ein Aktualisierungstoken austauschen. Diese Anforderung ist ein HTTP-Beitrag und enthält die folgenden Parameter:

Feld Beschreibung code Der von der anfänglichen Anforderung zurückgegebene Autorisierungscode client_id Die bei der Anwendungsregistrierung erhaltene client_id client_secret Das bei der Anwendungsregistrierung erhaltene Clientgeheimnis redirect_uri Der mit der Anwendung grant_type registrierte URI Wie in der OAuth 2.0-Spezifikation definiert, muss dieses Feld den Wert authority_code enthalten Die eigentliche Anfrage könnte so aussehen:

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

Eine erfolgreiche Antwort auf diese Anfrage enthält die folgenden Felder:

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

Antworten auf die Frage(1)

Ihre Antwort auf die Frage