我正在尝试使用Firebase集成Google-signIn,但登录无效。 它要求选择一个帐户,但没有登录。
这是我的MainActivity代码(SignIn Activity):
public class MainActivity extends AppCompatActivity{
private static final String TAG = "SignInActivity";
private static final int RC_SIGN_IN = 0 ;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private com.google.android.gms.common.SignInButton signInButton;
private GoogleApiClient mGoogleApiClient;
private Button signOutButton;
private TextView nameTextView;
private TextView emailTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signInButton = (com.google.android.gms.common.SignInButton)findViewById(R.id.sign_in_button);
signOutButton = (Button)findViewById(R.id.sign_out_button);
nameTextView = (TextView)findViewById(R.id.name_text_view);
emailTextView = (TextView)findViewById(R.id.email_text_view);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this , new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
} /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
signInButton.setVisibility(View.GONE);
signOutButton.setVisibility(View.VISIBLE);
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
if(user.getDisplayName() != null)
nameTextView.setText("HI " + user.getDisplayName().toString());
emailTextView.setText(user.getEmail().toString());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
signOutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
signInButton.setVisibility(View.VISIBLE);
signOutButton.setVisibility(View.GONE);
emailTextView.setText(" ".toString());
nameTextView.setText(" ".toString());
}
});
}
// ..
});
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
}
}
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
这是我的MainActivity布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="100dp"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:gravity="center_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello User"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/email_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginTop="10dp"
android:layout_marginBottom="50dp"/>
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/sign_out_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Sign Out"/>
</LinearLayout>
这是我的logcat:
06-11 03:57:14.769 30651-30916/silive.in.friendlyshopping I/OpenGLRenderer: Initialized EGL, version 1.4
06-11 03:57:14.769 30651-30916/silive.in.friendlyshopping D/mali_winsys: new_window_surface returns 0x3000, [1080x1920]-format:1
06-11 03:57:14.809 30651-30651/silive.in.friendlyshopping W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
06-11 03:57:14.819 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
06-11 03:57:14.819 30651-30916/silive.in.friendlyshopping D/libGLESv1: STS_GLApi : DTS is not allowed for Package : silive.in.friendlyshopping
06-11 03:57:14.909 30651-30864/silive.in.friendlyshopping D/FA: Connected to remote service
06-11 03:57:14.909 30651-30651/silive.in.friendlyshopping I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@31a96d9 time:45321280
06-11 03:57:15.039 30651-30917/silive.in.friendlyshopping W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/silive.in.friendlyshopping/shared_prefs/com.google.android.gms.appid.xml.bak
06-11 03:57:18.619 30651-30651/silive.in.friendlyshopping I/Timeline: Timeline: Activity_launch_request id:silive.in.friendlyshopping time:45324994
06-11 03:57:18.639 30651-30864/silive.in.friendlyshopping D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=4018, firebase_screen_class(_sc)=Splash, firebase_screen_id(_si)=-8152285565647586081}]
06-11 03:57:18.679 30651-30651/silive.in.friendlyshopping W/ResourcesManager: Resource getTopLevelResources for package com.google.android.gmsoverlayDirs =Null
06-11 03:57:18.739 30651-30651/silive.in.friendlyshopping W/System: ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000009/n/armeabi-v7a
06-11 03:57:18.739 30651-30651/silive.in.friendlyshopping W/System: ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000009/n/armeabi
06-11 03:57:18.739 30651-30651/silive.in.friendlyshopping W/ResourcesManager: Resource getTopLevelResources for package com.google.android.gmsoverlayDirs =Null
06-11 03:57:18.799 30651-30651/silive.in.friendlyshopping D/AutoManageHelper: starting AutoManage for client 0 false null
06-11 03:57:18.809 30651-30651/silive.in.friendlyshopping D/AutoManageHelper: onStart true {0=com.google.android.gms.internal.zzbcg$zza@f2773d3}
06-11 03:57:18.819 30651-30651/silive.in.friendlyshopping D/SecWifiDisplayUtil: Metadata value : SecSettings2
06-11 03:57:18.819 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{b6b7a2f I.E...... R.....ID 0,0-0,0}
06-11 03:57:18.819 30651-30864/silive.in.friendlyshopping D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=Splash, firebase_previous_id(_pi)=-8152285565647586081, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-8152285565647586080}]
06-11 03:57:18.829 30651-30651/silive.in.friendlyshopping D/SignInActivity: onAuthStateChanged:signed_out
06-11 03:57:18.829 30651-30651/silive.in.friendlyshopping W/ActivityThread: AppLock checkAppLockState isAppLocked = false pkgName = silive.in.friendlyshopping showWhenLocked = false
06-11 03:57:18.849 30651-30916/silive.in.friendlyshopping D/mali_winsys: new_window_surface returns 0x3000, [1080x1920]-format:1
06-11 03:57:18.889 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
06-11 03:57:18.909 30651-30651/silive.in.friendlyshopping I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@4ff000e time:45325288
06-11 03:57:19.149 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: #3 mView = null
06-11 03:57:24.809 30651-30864/silive.in.friendlyshopping D/FA: Logging event (FE): session_start(_s), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-8152285565647586080}]
06-11 03:57:24.829 30651-30864/silive.in.friendlyshopping D/FA: Connected to remote service
06-11 03:57:32.459 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: ViewPostImeInputStage processPointer 0
06-11 03:57:32.929 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: ViewPostImeInputStage processPointer 1
06-11 03:57:34.869 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: ViewPostImeInputStage processPointer 0
06-11 03:57:34.969 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: ViewPostImeInputStage processPointer 1
06-11 03:57:34.969 30651-30651/silive.in.friendlyshopping D/FirebaseAuth: Notifying id token listeners about a sign-out event.
06-11 03:57:34.969 30651-30651/silive.in.friendlyshopping D/FirebaseAuth: Notifying auth state listeners about a sign-out event.
06-11 03:57:34.979 30651-30850/silive.in.friendlyshopping W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/silive.in.friendlyshopping/shared_prefs/com.google.firebase.auth.api.Store.W0RFRkFVTFRd+MTo5OTY5MjQ1MzQ2NzQ6YW5kcm9pZDo1ZDU2ZTIwMzk4ZGUwZGZk.xml.bak
06-11 03:57:34.989 30651-30850/silive.in.friendlyshopping W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/silive.in.friendlyshopping/shared_prefs/com.google.android.gms.signin.xml.bak
06-11 03:57:34.989 30651-30651/silive.in.friendlyshopping D/FirebaseApp: Notifying auth state listeners.
06-11 03:57:34.989 30651-30651/silive.in.friendlyshopping D/FirebaseApp: Notified 0 auth state listeners.
06-11 03:57:34.989 30651-30651/silive.in.friendlyshopping D/SignInActivity: onAuthStateChanged:signed_out
06-11 03:57:39.409 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: ViewPostImeInputStage processPointer 0
06-11 03:57:39.539 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: ViewPostImeInputStage processPointer 1
06-11 03:57:39.539 30651-30651/silive.in.friendlyshopping I/Timeline: Timeline: Activity_launch_request id:silive.in.friendlyshopping time:45345918
06-11 03:57:39.579 30651-30864/silive.in.friendlyshopping D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=20753, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-8152285565647586080}]
06-11 03:57:39.609 30651-30864/silive.in.friendlyshopping D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=MainActivity, firebase_previous_id(_pi)=-8152285565647586080, firebase_screen_class(_sc)=SignInHubActivity, firebase_screen_id(_si)=-8152285565647586079}]
06-11 03:57:39.619 30651-30651/silive.in.friendlyshopping D/SecWifiDisplayUtil: Metadata value : SecSettings2
06-11 03:57:39.619 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{242bce9 I.E...... R.....I. 0,0-0,0}
06-11 03:57:39.639 30651-30916/silive.in.friendlyshopping D/mali_winsys: new_window_surface returns 0x3000, [1080x1920]-format:1
06-11 03:57:39.659 30651-30651/silive.in.friendlyshopping W/ActivityThread: AppLock checkAppLockState isAppLocked = false pkgName = silive.in.friendlyshopping showWhenLocked = false
06-11 03:57:39.659 30651-30864/silive.in.friendlyshopping D/FA: Connected to remote service
06-11 03:57:39.669 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
06-11 03:57:39.669 30651-30651/silive.in.friendlyshopping I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@25a6904 time:45346048
06-11 03:57:43.549 30651-30864/silive.in.friendlyshopping D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=SignInHubActivity, firebase_previous_id(_pi)=-8152285565647586079, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-8152285565647586080}]
06-11 03:57:43.579 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: #3 mView = null
06-11 03:57:43.599 30651-30651/silive.in.friendlyshopping W/ActivityThread: AppLock checkAppLockState isAppLocked = false pkgName = silive.in.friendlyshopping showWhenLocked = false
06-11 03:57:43.599 30651-30651/silive.in.friendlyshopping I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@4ff000e time:45349974
06-11 03:58:09.649 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: ViewPostImeInputStage processPointer 0
06-11 03:58:09.899 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: ViewPostImeInputStage processPointer 1
06-11 03:58:13.839 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: ViewPostImeInputStage processPointer 0
06-11 03:58:13.969 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: ViewPostImeInputStage processPointer 1
06-11 03:58:13.969 30651-30651/silive.in.friendlyshopping I/Timeline: Timeline: Activity_launch_request id:silive.in.friendlyshopping time:45380343
06-11 03:58:13.989 30651-30864/silive.in.friendlyshopping D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=30437, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-8152285565647586080}]
06-11 03:58:14.009 30651-30864/silive.in.friendlyshopping D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=MainActivity, firebase_previous_id(_pi)=-8152285565647586080, firebase_screen_class(_sc)=SignInHubActivity, firebase_screen_id(_si)=-8152285565647586078}]
06-11 03:58:14.009 30651-30651/silive.in.friendlyshopping D/SecWifiDisplayUtil: Metadata value : SecSettings2
06-11 03:58:14.019 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{f639ccc I.E...... R.....I. 0,0-0,0}
06-11 03:58:14.029 30651-30916/silive.in.friendlyshopping D/mali_winsys: new_window_surface returns 0x3000, [1080x1920]-format:1
06-11 03:58:14.029 30651-30864/silive.in.friendlyshopping D/FA: Connected to remote service
06-11 03:58:14.049 30651-30651/silive.in.friendlyshopping W/ActivityThread: AppLock checkAppLockState isAppLocked = false pkgName = silive.in.friendlyshopping showWhenLocked = false
06-11 03:58:14.059 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
06-11 03:58:14.069 30651-30651/silive.in.friendlyshopping I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@7f43e1e time:45380440
06-11 03:58:16.419 30651-30651/silive.in.friendlyshopping D/ViewRootImpl: #3 mView = null
06-11 03:58:16.439 30651-30864/silive.in.friendlyshopping D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=SignInHubActivity, firebase_previous_id(_pi)=-8152285565647586078, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-8152285565647586080}]
06-11 03:58:16.439 30651-30651/silive.in.friendlyshopping I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@4ff000e time:45382817
06-11 03:58:16.439 30651-30651/silive.in.friendlyshopping W/ActivityThread: AppLock checkAppLockState isAppLocked = false pkgName = silive.in.friendlyshopping showWhenLocked = false
06-11 03:58:57.009 30651-32456/silive.in.friendlyshopping D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=40582, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-8152285565647586080}]
06-11 03:58:57.059 30651-32456/silive.in.friendlyshopping D/FA: Connected to remote service
06-11 04:02:14.709 30651-30843/silive.in.friendlyshopping W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
提前致谢。
答案 0 :(得分:0)
进行此更改并检查其是否已记录。
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
Log.i("ErrorSignIn", result.getStatus().getStatusMessage());
}
}