如何获取React Native GoogleSignIn的accessToken? [英] How to get accessToken of React Native GoogleSignIn?

查看:93
本文介绍了如何获取React Native GoogleSignIn的accessToken?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获取accessToken,我在后端api中需要它.

I am not able to get accessToken, i need it in my backend api.

当我尝试此操作时,

googleLogin = () => {

  GoogleSignin.signIn()
    .then((data) => {
      console.log("TEST "+JSON.stringify(data));
      var postData = {
        access_token: data.accessToken,
        code: data.idToken,
      };

      let axiosConfig = {
        headers: {
            'Content-Type': 'application/json',
            "Accept": "application/json",
        }
      };
     ....
      //Backend api axios call
     ....

    })
    .then((user) => {
      console.log("TEST G LOGIN 1 "+JSON.stringify(user))
    })
    .catch((error) => {
      console.log("....."+JSON.stringify(error))
    });
}

得到此回复,它不会感染

got this response, it doesn't inculde

accessToken

{
    "scopes": ["https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/drive.readonly"],
    "serverAuthCode": "4/jwE5LLLLLLa7-f33333jmMD2V978oyp44444eb9yGe_qhHnkXXXXXXXLFXKZlQdDzlH-iIJx_gzqlLLLLLL3Q0PP0",
    "idToken": "...v65a4MCC-ZUQmys_wf_DoCOBJEMuI........",
    "user": {
        "photo": "https://lh3.googleusercontent.com/-tLLLLLyeS0KE/AAAMMMMAAAI/AAAAAAAAAAA/ACHi3reMhihoeTe_6NjL666666EUVU82Q/s96-c/photo.jpg",
        "email": "test@gmail.com",
        "familyName": "tech",
        "givenName": "test",
        "name": "testtech",
        "id": "11688888817288868"
    }
}

根据文档

getTokens()使用包含{idToken:string,accessToken:字符串,}或由于错误而拒绝.注意使用不建议使用accessToken.

getTokens() Resolves with an object containing { idToken: string, accessToken: string, } or rejects with an error. Note that using accessToken is discouraged.

所以,我在

GoogleSignin.Sign({
....
 var gettoken =  GoogleSignin.currentUserAsync(data.user).then((token) => {
      console.log('USER token', token);
    }).done();
...
 })

它出错了,还尝试了 const token = GoogSignIn.getTokens(),它返回了 null .

it got error and also tried const token = GoogSignIn.getTokens(), it return null.

package.json信息

package.json info

{
...
    "react": "16.8.3",
    "react-native": "0.59.9",
    "react-native-firebase": "5.3.1",
    "react-native-google-signin": "^2.0.0"
...
}

请提出获取accessToken的程序.

Please suggest what would be procedure to get accessToken.

推荐答案

最后,我获得了accessToken.

Finally i get accessToken.

步骤1:-我在goolge开发人员控制台中删除了所有生成的clidenId(仅保留我在Web项目中使用的Web应用程序clientId),还删除了Firebase项目中的android应用程序.

Step 1:- I deleted all the generated clidenId in goolge developer console (keep only web application clientId as i used in my web project) and also deleted android app in firebase project.

步骤2:-在firebase中创建了新的android应用,并下载google-services.json并将其粘贴到 android/app/google-services.json

Step 2:- Created new android app in firebase and download google-services.json and paste it in android/app/google-services.json

步骤3:-从google-services.json的这一部分复制了clidenId

Step 3:- Copied the clidenId from this part of google-services.json

...
  "services": {
        "appinvite_service": {
          "other_platform_oauth_client": [
            {
              "client_id": "xxxxxxx-xxxxx.apps.googleusercontent.com", //<--- copied this clientID
              "client_type": 3
            },
            {
              "client_id": "XXXXXXXXXX-fBBBBBBBBBBBBBBBBBBBBBBBBpugnhrade.apps.googleusercontent.com",
              "client_type": 2,
              "ios_info": {
                "bundle_id": "com.projectios"
              }
            }
          ]
        }
      }

...

并粘贴

GoogleSignin.configure({
  webClientId: 'paste it here',
});

第4步:-这是获取accessToken的代码(但是此代码在我之前的google-services.json文件中不起作用)

Step 4:- This is the code to get accessToken (But this code was not working in my previous google-services.json file)

googleLogin = () => {
 
    GoogleSignin.signIn()
      .then((data) => {

        console.log("TEST " + JSON.stringify(data));

        const currentUser = GoogleSignin.getTokens().then((res)=>{
          console.log(res.accessToken ); //<-------Get accessToken
          var postData = {
            access_token: res.accessToken,
            code: data.idToken,
    
          };
    
          let axiosConfig = {
            headers: {
              'Content-Type': 'application/json',
              "Accept": "application/json",
            }
          };
         -----
          backend api call
         -----
});

      })
      .then((user) => {
        console.log("TEST G LOGIN 1 " + JSON.stringify(user))
      })
      .catch((error) => {
        console.log("....." + JSON.stringify(error))
      });   
  }

这篇关于如何获取React Native GoogleSignIn的accessToken?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆