2017-07-17 33 views
2

我開發了一個應用程序,該應用程序使用gmail API從用戶獲取所有郵件。然後,我將這個應用程序分成了一個樣本(幾乎是空的)和一個可以完成所有任務的片段,所以我可以稍後將我的片段輕鬆地集成到團隊的項目設置中。在Firebase項目中集成Gmail連接

現在,我的片段是在其它項目中,Gmail的聯接不工作,給了我這些錯誤:

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

我認爲這個錯誤是因爲該項目採用火力點,而且已經有google- services.json文件和我的不使用。 我們在google developper門戶中添加了GMail API,並生成了一個新的json文件,但它似乎不起作用。

爲什麼我的GMail連接失敗,我該如何解決?

回答

1

好吧,我設法得到它的工作。

看起來像我在API管理器Google控制檯中創建憑證密鑰時,它不會在Firebase中項目的所有應用中添加SHA1密鑰。

我不得不這樣做(的辛苦工作一週後)爲複製粘貼塔應用在其他應用程序

我希望它可以幫助「鏈接」到谷歌API控制檯SHA1

0

使用此代碼來整合使用firebase的gmail登錄。如果已經登錄,它將導航到homeScreen,如果以前沒有登錄,它會彈出電子郵件登錄一旦登錄點擊。

添加您在此處使用string.xml server_client_id:

的getString(R.string.server_client_id)

代碼:

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> 
+0

這並不能真正回答我的問題。因此,我在第一個應用中製作的所有代碼都無法用於Firebase應用中? 我的片段應該可以在兩個應用中工作,所以我應該將所有身份驗證代碼切換到Firebase身份驗證,然後將Firebase集成到我的應用程序中? – Diiscord

+0

如果工作正常,則不需要更改以前的authenticaton代碼。在您的結果成功時,在您的handleSignInResult()方法中,然後轉到firebaseAuthWithGoogle(GoogleSignInAccount acct)方法。看我的代碼。使用您的最新google_services.json。在Api控制檯中啓用Gmail API。還要在API中添加您的調試併發布SHA1。只有調試SHA1在開發過程中也可以使用 –

+0

感謝您的回答,問題是我沒有handleSignInResult方法和GoogleSignInAccount。 我使用了這個快速入門https://developers.google.com/gmail/api/quickstart/android,所以我有一個GoogleAccountCredential – Diiscord