Firebase:使用Google进行身份验证,我必须在哪里获取id_token?

时间:2016-11-29 13:43:15

标签: unity3d firebase firebase-authentication

在Firebase unity3D SDK中当我尝试获取凭据时,它会向我询问 id_token access_token

对于我见过的一些例子,我猜想access_token必须为null,但我不知道如何处理这个id_token。代码示例:

这是电话:

Firebase.Auth.GoogleAuthProvider.GetCredential(string id_token,string access_token);

这是一个unity3D示例代码:

public void GoogleLogin(Action<bool> loginOK)
{
    string id_token = "90096201****-353hvgf63fecvvc3mi****s6140f98a.apps.googleusercontent.com";
    Firebase.Auth.Credential credential;
    credential = Firebase.Auth.GoogleAuthProvider.GetCredential(id_token,null);
    auth.SignInWithCredentialAsync(credential).ContinueWith (task => 
    {
        if (!task.IsCanceled && !task.IsFaulted)
        {
            loginOK(true);
        }
        else
        {
            loginOK(false);
        }

        if (task.Exception != null)
        {
            Debug.LogException(task.Exception);
        }
    });
}

我认为这将是来自谷歌控制台的Oauth 2.0令牌。但这似乎不起作用。谷歌回答告诉我下一个:

  

11-29   13:58:25.476 com.google.android.gms 2009 3225 I AuthChimeraService
  “message”:“无法解析Google id_token :   90096201 **** - 353hvgf63fecvvc3mi **** s6140f98a.apps.googleusercontent.com“

知道我做错了什么?

3 个答案:

答案 0 :(得分:1)

firebase sample for google sign in in unity应该对此有所帮助。提到的第一步之一是:

  

按照AndroidiOS的说明获取Google登录的ID令牌。

firebase文档还有manually verifying id tokens的子部分,但是他们会对此提出建议。

答案 1 :(得分:1)

是的,这是这里需要的Oauth 2.0令牌,

为此你可以使用它。

n您无法提供自己的令牌,因为它可能会因用户而改变。

private void getGoogleOAuthTokenAndLogin() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
   // Log.e("SahajLOG", "Login PREF ISSSSSSSS ONCREATE  "+prefs.getBoolean("AuthByGplus", AuthByGplus));
    if (!prefs.getBoolean("AuthByGplus", AuthByGplus)) {
        AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
            String errorMessage = null;

            @Override
            protected String doInBackground(Void... params) {
                String token = null;

                try {
                    String scope = String.format("oauth2:%s", Scopes.PLUS_LOGIN);
                    token = GoogleAuthUtil.getToken(MainActivity.this, Plus.AccountApi.getAccountName(mGoogleApiClient), scope);
                } catch (IOException transientEx) {
                /* Network or server error */
                    Log.e("SahajLOG", "Error authenticating with Google: " + transientEx);
                    errorMessage = "Network error: " + transientEx.getMessage();
                } catch (UserRecoverableAuthException e) {
                    Log.w("SahajLOG", "Recoverable Google OAuth error: " + e.toString());
                /* We probably need to ask for permissions, so start the intent if there is none pending */
                    if (!mIntentInProgress) {
                        mIntentInProgress = true;
                        Intent recover = e.getIntent();
                        startActivityForResult(recover, MainActivity.GOOGLE_SIGIN);
                    }
                } catch (GoogleAuthException authEx) {
                /* The call is not ever expected to succeed assuming you have already verified that
                 * Google Play services is installed. */
                    Log.e("SahajLOG", "Error authenticating with Google: " + authEx.getMessage(), authEx);
                    errorMessage = "Error authenticating with Google: " + authEx.getMessage();
                }

                return token;
            }

            @Override
            protected void onPostExecute(String token) {
                mGoogleLoginClicked = false;
                Intent resultIntent = new Intent();
                if (token != null) {
                    Log.e("SahajLOG", "TOKEN IS  " + token);
               //     firebaseAuthWithGoogle(token);


                    //onGoogleLoginWithToken(token);
                    resultIntent.putExtra("oauth_token", token);
                } else if (errorMessage != null) {
                    resultIntent.putExtra("error", errorMessage);
                }
                setResult(MainActivity.GOOGLE_SIGIN, resultIntent);
                finish();
            }
        };
        task.execute();
    }
    Log.e("SahajLOG", "oAuthCalled");
    /* Get OAuth token in Background */
}

答案 2 :(得分:1)

您需要使用Google登录SDK才能统一使用google登录,在烧录后,您将获得id_token以使用Firebase进行身份验证。