Integrating gmail connection in Firebase project

醉酒当歌 提交于 2020-01-13 19:42:11

问题


I developped an application that uses gmail api to get all the mails from the user. Then I divided this app in a sample (almost empty) and a fragment that does everything, so I can later integrate my fragment easily in my team's project's settings.

Now that my fragment is in the other project, the gmail connexion doesn't work and gives me these errors :

E/Async Task: com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException
E/Google log in: failed

I think this error is because the project uses firebase and already have a google-services.json file and mine isn't used. We added the GMail API in the google developper portal and generated a new json file but it doesn't seem to work.

Why does my GMail connexion fails and how can I solve it ?


回答1:


Ok, I managed to get it working.

Looks like when I create credentials keys in the API manager Google console, it doesn't add the SHA1 keys in all the apps of the project in Firebase.

All I had to do (after a week of hard work) was to copy paste the SHA1 from tha app "linked" to the Google API console in the other app

I hope it can help




回答2:


Use this code to integrate gmail login using firebase . If already login it will navigate to homeScreen and if not logined previously it will pop up email to login once sign in clicked.

Add you server_client_id in string.xml used here :

getString(R.string.server_client_id)

Code :

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
import smart.works.android.bharat.com.codeplay.ui.homeScreen.CodePlayActivity;
import smart.works.android.bharat.com.codeplay.R;
import smart.works.android.bharat.com.codeplay.Utils.AppConstants;
import smart.works.android.bharat.com.codeplay.Utils.PreferenceUtils;

public class MainActivity extends AppCompatActivity
    implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {

  private static final int RC_SIGN_IN = 124;
  private static final String TAG = "MainActivity";
  private GoogleApiClient mGoogleApiClient;
  private FirebaseAuth mAuth;
  private ProgressBar mProgressBar;
  private SignInButton mSignInButton;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    initUi();
    setupUi();
  }

  private void setupUi() {
    GoogleSignInOptions gso =
        new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestIdToken(
            getString(R.string.server_client_id)).requestEmail().build();
    mGoogleApiClient =
        new GoogleApiClient.Builder(this).enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
    // Set the dimensions of the sign-in button.
    mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
    mSignInButton.setSize(SignInButton.SIZE_STANDARD);
    findViewById(R.id.sign_in_button).setOnClickListener(this);
    mAuth = FirebaseAuth.getInstance();
  }

  private void initUi() {
    mProgressBar = (ProgressBar) findViewById(R.id.sign_in_progress);
    mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
  }

  @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.sign_in_button:
        signIn();
        break;
    }
  }

  private void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
  }

  private void showProgress() {
    mProgressBar.setVisibility(View.VISIBLE);
    mSignInButton.setEnabled(false);
  }

  private void cancelProgress() {
    mProgressBar.setVisibility(View.GONE);
    mSignInButton.setEnabled(true);
  }

  @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) {
      showProgress();
      GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
      handleSignInResult(result);
    }
  }

  private void handleSignInResult(GoogleSignInResult result) {
    Log.d(TAG, "handleSignInResult:" + result.isSuccess());
    if (result.isSuccess()) {
      // Google Sign In was successful, authenticate with Firebase
      GoogleSignInAccount account = result.getSignInAccount();
      firebaseAuthWithGoogle(account);

      //updateUI(true);
    } else {
      cancelProgress();
      Toast.makeText(MainActivity.this, "sign in failed ! Try Again ", Toast.LENGTH_SHORT).show();
      // Signed out, show unauthenticated UI.
      //updateUI(false);
    }
  }

  @Override public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser != null) {
      forwardToHomeScreen(currentUser);
    }

    //updateUI(currentUser);

  }


  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) {
            cancelProgress();
            if (task.isSuccessful()) {
              // Sign in success, update UI with the signed-in user's information
              Log.d(TAG, "signInWithCredential:success");
              FirebaseUser user = mAuth.getCurrentUser();
              forwardToHomeScreen(user);
            } else {
              // If sign in fails, display a message to the user.
              Log.w(TAG, "signInWithCredential:failure", task.getException());
              Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT)
                  .show();
              //updateUI(null);
            }

            // ...
          }
        });
  }

  private void forwardToHomeScreen(FirebaseUser user) {
    PreferenceUtils preferenceUtils = new PreferenceUtils(this);
    preferenceUtils.getPrefEditor().putString(AppConstants.USER_ID, user.getUid()).apply();
    Intent i = new Intent(this, CodePlayActivity.class);
    i.putExtra(AppConstants.USER_NAME, user.getDisplayName());
    i.putExtra(AppConstants.USER_EMAIL, user.getEmail());
    i.setData(user.getPhotoUrl());
    startActivity(i);
    finish();
  }
}

actvity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/nav_bg"
    android:orientation="vertical"
    >

  <ImageView
      android:id="@+id/icon"
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:layout_alignParentTop="true"
      android:layout_centerInParent="true"
      android:layout_marginTop="100dp"
      android:contentDescription="@string/app_icon"
      android:src="@mipmap/ic_launcher"
      />
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/icon"
      android:layout_centerInParent="true"
      android:layout_marginBottom="60dp"
      android:text="@string/app_name"
      android:textColor="#FFFFFF"
      android:textSize="16sp"
      android:textStyle="bold"
      />
  <ProgressBar
      android:id="@+id/sign_in_progress"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:indeterminate="true"
      android:visibility="gone"
      />
  <com.google.android.gms.common.SignInButton
      android:id="@+id/sign_in_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerInParent="true"
      android:layout_marginBottom="100dp"
      />


</RelativeLayout>


来源:https://stackoverflow.com/questions/45147320/integrating-gmail-connection-in-firebase-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!