Ich versuche, einen Node.js-Authentifizierungsdienst zur Authentifizierung mit der Google OAuth-API zu erstellen.Authentifizieren mit Google OAuth und Fehler "Erforderlicher Parameter fehlt: grant_type"
Ich habe diesen Code:
private exchangeTokens(code: string): Rx.Observable<IAuthTokens>{
code = decodeURIComponent(code);
console.log(`exchanging code for tokens: ${code}`);
const redirectUri = "http://localhost:8080";
let url = YouTubeAuthenticationServer.baseUrl + "token";
var postData= "code=" + encodeURIComponent(code);
postData += "&redirect_uri=" + encodeURIComponent(redirectUri);
postData += "&client_id=" + encodeURIComponent(process.env.CLIENT_ID);
postData += "&client_secret=" + encodeURIComponent(process.env.CLIENT_SECRET);
postData += "&scope=";
postData += "&grant_type=authorization_code";
return this.makePostRequest<IAuthTokens>(url,postData);
}
private makePostRequest<T>(targetUrl:string, data: string): Rx.Observable<T>{
console.log(`getting targetUrl: ${targetUrl}`);
var urlObject = url.parse(targetUrl);
var options: http.RequestOptions = {
hostname: urlObject.hostname,
port: Number(urlObject.port),
path: urlObject.path,
protocol: "https:",
method: "POST"
};
const request = https.request(options);
const returnObservable = Rx.Observable.fromEvent(<any>request, "response")
.take(1)
.flatMap(response => RxNode.fromWritableStream(<any>response))
.do(data => console.log(`data pumped: ${data}`))
.toArray()
.map(function(allData){
return JSON.parse(allData.join("")) as T;
});
console.log(`write data: ${data}`);
request.write(data);
request.end();
return returnObservable;
}
und daraus ich die folgenden Protokolle erhalten:
exchanging code for tokens: 4/k6Pp7jCPDms2meo0qfINqs0c9FZSjJ7PvGp8mdnh3Y8#
getting targetUrl: https://accounts.google.com/o/oauth2/token
write data: code=4%2Fk6Pp7jCPDms2meo0qfINqs0c9FZSjJ7PvGp8mdnh3Y8%23&redirect_uri=http%3A%2F%2Flocalhost%3A8080&client_id=myCorrectId&client_secret=myCorrectSecret&scope=&grant_type=authorization_code
data pumped: {
"error" : "invalid_request",
"error_description" : "Required parameter is missing: grant_type"
}
So das ich sende folgende:
code=4%2Fk6Pp7jCPDms2meo0qfINqs0c9FZSjJ7PvGp8mdnh3Y8%23
redirect_uri=http%3A%2F%2Flocalhost%3A8080
client_id=myCorrrectId
client_secret=myCorrectSecret
scope=
grant_type=authorization_code
So kann ich‘ Ich verstehe nicht, warum ich sage, dass ich nicht Grant_Type senden
Ich habe nur Umfang wie das ist, was am Beispiel auf dem Google OAuth Spielplatz übergeben wird. Es funktioniert nicht, wenn das auch entfernt wird. – Roaders
Stellen Sie außerdem sicher, dass die Anfrage diese Kopfzeile enthält: Content-Type: application/x-www-form-urlencoded, da Google möglicherweise andere Inhaltstypen zulässt und die Payload möglicherweise falsch interpretiert. –
YESSS !!!!!! Danke Joan - ich füge das als Antwort hinzu und akzeptiere es. – Roaders