2016-12-16 64 views
15

我想盡量保持我的本地android firebase開發到最低限度,以便當我準備好移植到IOS/web時,我不會在那裏做很多事情。如何在android google登錄後初始化firebase?

現在firebase的Javascript不允許谷歌從Android登錄,這可以從插件照顧。但是我堅持的是如何基於Java Android Google登錄名初始化Firebase。

原來這就是我想要實現:

科爾多瓦調用Java的Android的原生登錄到谷歌--->在此基礎上,我將如何初始化火力?

這個插件可以讓我登錄到谷歌本身:https://www.npmjs.com/package/cordova-plugin-googleplus

但我想我需要身份驗證令牌?令牌ID?

firebase.auth().signInWithCredential(credential).catch(function(error) { 
    } else { 
    console.error(error); 
    } 
}); 

這可以給我上述要求嗎? https://developers.google.com/identity/sign-in/android/sign-in

GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 

更新1:只是想分享更多信息。當得到用戶通過谷歌在Android上登錄我有以下對象

GoogleSignInAccount 

https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInAccount

public String getIdToken() & public String getServerAuthCode()爲什麼不能將這些被用來使用JS驗證火力?

更新2:Faraz提供的答案似乎正在工作。這裏是功能參考signInWithCredentialhttps://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithCredential

謝謝你的幫助。

回答

4

使用auth.signInWithCredentialGoogleAuthProvider憑證。

下面是一個例子:

auth.signInWithCredential(firebase.auth.GoogleAuthProvider.credential(googleAccessToken)).then(function(user) { 
    // Google User is logged in 
}).catch(function(error) { 
    // Error 
}); 

Source for more infomation

1

您可以閱讀關於Firebase使用情況的this GitHub example

而且Here你可以找到 -

mFirebaseRef.authWithOAuthToken("google", token, new AuthResultHandler("google")); 

哪個(如果一切成功)來電this public void onAuthenticated(AuthData authData)

哪裏token是你getIdToken 我的意思是possble登錄到火力地堡使用谷歌,在所有情況下,Facebook,Twitter都必須將收到的令牌發送給Firebase服務器,以檢查令牌是否已經登錄。您可以用相同的方式設置自己的服務器。

0

谷歌登錄後,你必須使用身份驗證。signInWithCredential與GoogleAuthProvider憑據:

下面是一個代碼:

private void firebaseAuthWithGoogle(GoogleSignInAccount account) { Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId()); final String name = account.getDisplayName(); final String email = account.getEmail(); AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null); getAuth().signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d(TAG, "onComplete: sign in with credentials " + task.isSuccessful()); if (task.i在此處輸入代碼sSuccessful()) { Log.e(TAG, "success: sign in with credentials "); } if (!task.isSuccessful()) { Log.e(TAG, "onComplete: sign in with credentials " + task.getException()); } } }); }

+0

OP專門問了JavaScript解決方案,而不是Java的。 – Faraz