簡體   English   中英

如何從 Google Sign-In Javascript SDK 獲取訪問令牌?

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

我有一個簡單的單頁 javascript webapp,它使用“Google 網站登錄”: https : //developers.google.com/identity/sign-in/web/sign-in

如何獲取用戶的訪問令牌? 我需要在我的服務器上對用戶身份進行可驗證的斷言。 我不想離線訪問; 我只想知道,當 Web 客戶端向我的服務器發送 ajax 請求時,我可以信任登錄用戶的身份。

出於驗證目的,最好使用id_token作為身份驗證響應的一部分,並且可以像這樣隨時檢索:

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

Google API 客戶端庫提供了驗證 id_token 並在服務器端為您提供相關用戶信息的功能: https : //developers.google.com/api-client-library/

首先需要初始化SDK然后調用下面的函數來獲取access-token

gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token; 

此解決方案幫助我在 javascript 中獲取訪問令牌。

希望你做得好。 這是解決方案,您可以試試這個:實際上沒有這樣的函數名稱 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);
    });
  });

問題是我們必須明智地使用此代碼。 我使用下面的代碼來獲取 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);
    });
  });

我從 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();
}

您可以將其作為一個單獨的函數調用,它看起來像這樣。 在 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);
      });
  }

並從 index.android.js 像這樣調用這個函數

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

你唯一要做的就是通過當前用戶來獲取訪問令牌。 希望這會幫助你。祝你有美好的一天。謝謝。

是的,現在是 2021 年。我也面臨着同樣的問題。

我的解決方案是

gapi.signin2.render(this.id, {
    scope: this.scope,
    width: this._width,
    height: this._height,
    longtitle: this._longTitle,
    theme: this.theme,
    // Set to true, otherwise only user actively logging in with Google will have access token 
    onsuccess: (googleUser: gapi.auth2.GoogleUser) => googleUser.getAuthResponse(true),
    onfailure: () => this.handleFailure()
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM