我使用Google 登录进行身份验证不起作用。它在迁移到Firebase之前工作正常。这是我的代码:
注意: google-service.json我的应用程序的ID与Web ID相同我不知道我是否得到正确的或者说明。
public class Authentication extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
private SignInButton signInButton;
private GoogleSignInOptions gso;
private GoogleApiClient mGoogleApiClient;
private int RC_SIGN_IN = 100;
GoogleSignInAccount acct = null;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
String TAG="AUTH";
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.PLUS_LOGIN))
.requestIdToken("******************************")
.requestEmail()
.build();
signInButton = (SignInButton) findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setScopes(gso.getScopeArray());
signInButton.setColorScheme(SignInButton.COLOR_LIGHT);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* 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();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
signInButton.setOnClickListener(this);
}
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);
System.out.println("REQUEST=======" + requestCode);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
acct = result.getSignInAccount();
firebaseAuthWithGoogle(acct);
test();
Intent accueil = new Intent(this, UpDateDB.class);
startActivity(accueil);
} else {
Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show();
}
}
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(Authentication.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onClick(View v) {
if (v == signInButton) {
if(!ConnexionInternet.isConnectedInternet(this)){
Toast.makeText(this, "Pas De Connexion", Toast.LENGTH_LONG).show();
}else{
signIn();
}
}
}
}
这些是我的依赖项:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.balysv:material-ripple:1.0.2'
compile 'com.google.android.gms:play-services-auth:9.6.1'
compile 'com.mikhaellopez:circularprogressbar:1.1.1'
compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'com.google.android.gms:play-services-identity:9.6.1'
compile 'com.ms-square:expandableTextView:0.1.4'
compile 'com.facebook.android:facebook-android-sdk:4.17.0'
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-messaging:9.6.1'
compile 'com.google.firebase:firebase-auth:9.6.1'
}
apply plugin: 'com.google.gms.google-services'