如何从 Google Sign-In Javascript SDK 获取访问令牌?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29765905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:02:21  来源:igfitidea点击:

How to get the access token from Google Sign-In Javascript SDK?

javascriptgoogle-plusgoogle-oauthgoogle-plus-signin

提问by stickfigure

I have a simple single-page javascript webapp which uses "Google Sign-In for Websites": https://developers.google.com/identity/sign-in/web/sign-in

我有一个简单的单页 javascript webapp,它使用“Google 网站登录”:https: //developers.google.com/identity/sign-in/web/sign-in

How can I get an access token for the user? I need a verifiable assertion of the user's identity on my server. I don't want offline access; I just want to know that when the web client sends an ajax request to my server, I can trust the identity of the logged-in user.

如何获取用户的访问令牌?我需要在我的服务器上对用户身份进行可验证的断言。我不想离线访问;我只是想知道,当 web 客户端向我的服务器发送 ajax 请求时,我可以信任登录用户的身份。

回答by Scarygami

For verification purposes it would be better to use the id_tokenwhich is part of the auth response, and can be retrieved at any point like this:

出于验证目的,最好使用id_tokenauth 响应的一部分,并且可以像这样在任何时候检索:

gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().id_token

The Google API Client libraries offer functions to verify the id_token and give you the associated user information on the server side: https://developers.google.com/api-client-library/

Google API 客户端库提供了验证 id_token 并在服务器端为您提供相关用户信息的功能:https: //developers.google.com/api-client-library/

回答by Pravin Ghorle

Hope your are doing well. Here is the solution, You may try this: Actually there is no such a function name getAccessToken (Android Only) define in GoogleSignin.android.js as written here https://github.com/devfd/react-native-google-signin. But the best part is they have already implemented the solution in GoogleSignin.android.js. just take a look at the code below from GoogleSignin.android.js

希望你做得好。这是解决方案,您可以试试这个:实际上没有这样的函数名称 getAccessToken (Android Only) 在 GoogleSignin.android.js 中定义,如此处所写https://github.com/devfd/react-native-google-signin. 但最好的部分是他们已经在 GoogleSignin.android.js 中实现了解决方案。看看下面来自 GoogleSignin.android.js 的代码

currentUserAsync() {
return new Promise((resolve, reject) => {
  const sucessCb = DeviceEventEmitter.addListener('RNGoogleSignInSilentSuccess', (user) => {
    this._user = user;

    RNGoogleSignin.getAccessToken(user).then((token) => {
      this._user.accessToken = token;
      this._removeListeners(sucessCb, errorCb);
      resolve(this._user);
    })
    .catch(err => {
      this._removeListeners(sucessCb, errorCb);
      resolve(this._user);
    });
  });

The thing is only we have do use this code wisely. I have use the below code to get access_token and it help me to solve my access token problem. I change above function like this in GoogleSignin.android.js

问题是我们必须明智地使用此代码。我使用下面的代码来获取 access_token,它帮助我解决了我的访问令牌问题。我在 GoogleSignin.android.js 中像这样更改了上述功能

currentUserAsync() {
return new Promise((resolve, reject) => {
  const sucessCb = DeviceEventEmitter.addListener('RNGoogleSignInSilentSuccess', (user) => {
    this._user = user;

    RNGoogleSignin.getAccessToken(user).then((token) => {
      this._user.accessToken = token;
      this._removeListeners(sucessCb, errorCb);
      resolve(token);
    })
    .catch(err => {
      this._removeListeners(sucessCb, errorCb);
      resolve(this._user);
    });
  });

and I call this function like this from index.android.js.

我从 index.android.js 中这样调用这个函数。

 _signIn() {
    GoogleSignin.signIn()
        .then((user) => {
            console.log('this1' + JSON.stringify(user));
            this.setState({user: user});
           var gettoken =  GoogleSignin.currentUserAsync(user).then((token) => {

                console.log('USER token', token);
                this.setState({user: user});
            }).done();

        }).catch((err) => {
            console.log('WRONG SIGNIN', err);
        })
        .done();
}

You can call it as a individual function it look like this. in GoogleSignin.android.js

您可以将其作为一个单独的函数调用,它看起来像这样。在 GoogleSignin.android.js 中

getAccessTok(user)
 {
  RNGoogleSignin.getAccessToken(user).then((token) => {
      this._user.accessToken = token;
      resolve(token);
      })
      .catch(err => {
          this._removeListeners(sucessCb, errorCb);
          console.log('got error');
          resolve(this._user);
      });
  }

and from index.android.js just call this function like this

并从 index.android.js 像这样调用这个函数

_getToken(){
    console.log(GoogleSignin.getAccessTok(this.state.user));
}

only you have to do is to pass the current user to get access token. Hope this will help you.Have a great day.Thank You.

你唯一要做的就是通过当前用户来获取访问令牌。希望这会帮助你。祝你有美好的一天。谢谢。