2017-07-17 10 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 

私はプロジェクトがfirebase使用しているため、このエラーがあると思い、すでにgoogle-を持っていますservices.jsonファイルとmineは使用されません。 google developperポータルにGMail APIを追加して新しいjsonファイルを生成しましたが、動作していないようです。

Gmailの接続が失敗し、どうすれば解決できますか?

答えて

1

[OK]を、私はそれが働いて得ることができました。

APIマネージャのGoogleコンソールで資格情報キーを作成すると、FirebaseのプロジェクトのすべてのアプリケーションにSHA1キーが追加されません。

私は(ハードワークの週後)しなければならなかったすべては

が、私はそれに役立つことを願って、他のアプリでのGoogle APIコンソールに「リンク」thaのアプリから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

正常に動作していれば、以前の認証コードを変更する必要はありません。結果が得られたら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

関連する問題