[英]Angular exchanging acces token for bearer's token Oauth2
我需要在Google Api服務中將授權碼交換為承載者的令牌。 有一個名為OauthPlayground的不錯的應用程序,它描述了Oauth2的流程。 對於令牌交換,它使用如下請求:
POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-length: 233
content-type: application/x-www-form-urlencoded
user-agent: google-oauth-playground
code=4%2F6RMgcR3WKuY8_bIaZa0o8P6xfxMKZh1f6AZmh9Q-h6Y&redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&client_id=407408718192.apps.googleusercontent.com&client_secret=************&scope=&grant_type=authorization_code
我已經在Google開發者控制台上注冊了我的應用程序,並且擁有自己的client_id和client_secret。 我的無效代碼如下:
tryGetBearers(){
let connString='www.googleapis.com/oauth2/v4/token';
let connBody='code='+this.accessToken;
connBody+='&redirect_uri='+this.redirect_uri
connBody+='&client_id='+this.client_id
connBody+='&client_secret='+this.client_secret
connBody+='&scope=&grant_type=authorization_code'
let headers = new Headers();
headers.append('content-type', 'application/x-www-form-urlencoded');
this.http.post(connString,connBody,{headers:headers}).subscribe(
res=>{console.log(res);},
(error)=>{console.log(error);
}
);
}
發送請求的示例正文是:
code=4/Q5p52c8zMfwzAKErog-7QUJzv7aYGyAlCDz_5KE0Mgw&redirect_uri=http://localhost:4200&client_id=665991054229-jmd9i1ovcohglkrlbu1ff1nih9j1cqia.apps.googleusercontent.com&client_secret=hnrh1eGPKFmdEWkdWna7zs6K&scope=&grant_type=authorization_code
該函數的響應僅為404 Not Found。 我不知道怎么了
我發現網址是錯誤的。 代替
let connString='www.googleapis.com/oauth2/v4/token';
可以使用它:
let connString='https://accounts.google.com/o/oauth2/token'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.