Angular交换对承载令牌Oauth2的访问令牌

时间:2017-11-22 15:35:30

标签: angular oauth-2.0 google-api google-oauth

我需要在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 developer console注册了我的应用程序并拥有自己的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。我不知道出了什么问题。

1 个答案:

答案 0 :(得分:0)

我发现网址不好。而不是

let connString='www.googleapis.com/oauth2/v4/token';

使用这个就可以了:

let connString='https://accounts.google.com/o/oauth2/token'
相关问题