2016-10-29 4 views
1

Ich versuche, in meiner app Anmeldung mit Google zu authentifizieren.Authentifizieren mit Google Feuerbasis Authentifizierung android

Wenn ich versuche, dies zu tun, erhalte ich die FehlerGoogle Sign In failed. Status = Status{statusCode=DEVELOPER_ERROR, resolution=null}

ich einen SHA1 Schlüssel generiert haben und haben Google Auth ist in der FireBase-Konsole aktiviert.

Ich bin ein Anfänger mit der Authentifizierungsseite von Android.

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

private FirebaseAuth mFirebaseAuth; 
private ProgressBar progressBar; 
private ProgressDialog mProgressDialog; 
private GoogleApiClient mGoogleApiClient; 
private static final String TAG = "RegisterActivity"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_register); 

    // Get FireBase mFirebaseAuth instance 
    mFirebaseAuth = FirebaseAuth.getInstance(); 

    if (mFirebaseAuth.getCurrentUser() != null) { 
     Log.e("45hh", "User logged in already..."); 
     startActivity(new Intent(RegisterActivity.this, MainActivity.class)); 
    } 


    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestScopes(new Scope(Scopes.PLUS_LOGIN)) 
      .requestScopes(new Scope(Scopes.PLUS_ME)) 
      .requestEmail() 
      .build(); 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) 
      .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
      .build(); 

    final EditText txtEmail = (EditText) findViewById(R.id.txtEmail); 
    final EditText txtPassword = (EditText) findViewById(R.id.txtPassword); 
    Button btnRegister = (Button) findViewById(R.id.btnRegister); 
    Button btnGSignin = (Button) findViewById(R.id.btnGSignin); 
    TextView txtAlreadyHaveAccount = (TextView) findViewById(R.id.txtAlreadyHaveAccount); 
    progressBar = (ProgressBar) findViewById(R.id.progressBar); 

    btnRegister.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String email = txtEmail.getText().toString().trim(); 
      String password = txtPassword.getText().toString().trim(); 

      if (TextUtils.isEmpty(email)) { 
       Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show(); 
       return; 
      } else if (TextUtils.isEmpty(password)) { 
       Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show(); 
       return; 
      } 

      if (password.length() < 6) { 
       Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show(); 
       return; 
      } 

      progressBar.setVisibility(View.VISIBLE); 
      // Create user 
      mFirebaseAuth.createUserWithEmailAndPassword(email, password) 
        .addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() { 
         @Override 
         public void onComplete(@NonNull Task<AuthResult> task) { 
          if (task.isSuccessful()) { 
           Toast.makeText(RegisterActivity.this, "Successfully registered!", Toast.LENGTH_SHORT).show(); 
           progressBar.setVisibility(View.GONE); 
           startActivity(new Intent(RegisterActivity.this, MainActivity.class)); 
           finish(); 
          } else { 
           Toast.makeText(RegisterActivity.this, "Authentication failed. " + task.getResult(), 
             Toast.LENGTH_SHORT).show(); 
          } 
         } 
        }); 
     } 
    }); 

    btnGSignin.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      signIn(); 
     } 
    }); 

    txtAlreadyHaveAccount.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(RegisterActivity.this, LoginActivity.class)); 
     } 
    }); 
} 

private void handleFirebaseAuthResult(AuthResult authResult) { 
    if (authResult != null) { 
     // Welcome the user 
     FirebaseUser user = authResult.getUser(); 
     Toast.makeText(this, "Welcome " + user.getEmail(), Toast.LENGTH_SHORT).show(); 

     // Go back to the main activity 
     startActivity(new Intent(this, MainActivity.class)); 
    } 
} 

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

@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 == 9001) { 
     GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
     if (result.isSuccess()) { 
      // Google Sign In was successful, authenticate with Firebase 
      GoogleSignInAccount account = result.getSignInAccount(); 
      firebaseAuthWithGoogle(account); 
     } else { 
      if(mProgressDialog!=null){ 
       hideProgressDialog(); 
      } 
      // Google Sign In failed 
      Log.e(TAG, "Google Sign In failed. Status = " + result.getStatus()); 
     } 
    } 
} 

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { 
    Log.d(TAG, "firebaseAuthWithGooogle:" + acct.getId()); 
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); 
    mFirebaseAuth.signInWithCredential(credential) 
      .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 
        Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); 

        // If sign in fails, display a message to the user. If sign in succeeds 
        // the auth state listener will be notified and logic to handle the 
        // signed in user can be handled in the listener. 
        if (!task.isSuccessful()) { 
         Log.w(TAG, "signInWithCredential", task.getException()); 
         Toast.makeText(RegisterActivity.this, "Authentication failed.", 
           Toast.LENGTH_SHORT).show(); 
        } else { 
         Toast.makeText(RegisterActivity.this, "Authentication Success. Please Wait", 
           Toast.LENGTH_SHORT).show(); 
         startActivity(new Intent(RegisterActivity.this, MainActivity.class)); 
         finish(); 
        } 
        hideProgressDialog(); 
       } 
      }); 
} 

public void showProgressDialog() { 
    if (mProgressDialog == null) { 
     mProgressDialog = new ProgressDialog(this); 
     mProgressDialog.setMessage("Loading..."); 
     mProgressDialog.setIndeterminate(true); 
    } 

    mProgressDialog.show(); 
} 

public void hideProgressDialog() { 
    if (mProgressDialog != null && mProgressDialog.isShowing()) { 
     mProgressDialog.dismiss(); 
    } 
} 

@Override 
public void onClick(View v) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
    // An unresolvable error has occurred and Google APIs (including Sign-In) will not 
    // be available. 
    Log.d(TAG, "onConnectionFailed:" + connectionResult); 
    Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show(); 
} 
} 

Antwort

0

Der Grund, warum dies nicht funktioniert, ist, weil die ClientID nicht angegeben wurde. Dies ist, wie es getan werden sollte:

private static final String googleClientID = "Enter-ClientID-Here"; 

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestIdToken(googleClientID) 
      .requestEmail() 
      .build(); 

Sie haben von hier aus Ihre Client-ID erhalten: (Gehen Sie auf die Feuerbasis Konsole -> Authentifizierung -> Log-in-Methode -> Google -> Hit the Web SDK Konfiguration Dropdown -> Web-Client-ID)

Google ClientID Location

PS Achten Sie darauf, die folgenden Berechtigungen in den Sie

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> 

Hoffnung manifestieren, das hilft jemand. Folgen Sie

0

einfach diese Schritte für Google SignIn:

1.) Erstellen eines Projekts bei Google Developers Console

Melden Sie sich bei Google Plus-Konto ein und klicken Sie auf Projekt erstellen. Creating google project

2.) Füllen Sie die Details nach Ihrer Wahl, dann

Jetzt brauchen wir eine Konfigurationsdatei für unsere Android app.Select App wir nur auf Entwickler-Konsole erstellt. Und schreiben Sie den Paketnamen Ihres Android Studio-Projekts, das Sie erstellt haben. Wählen Sie dann Land und klicken Sie auf Weiter.

3.) Drücken Sie diesen Befehl auf Cmd, um den Schlüssel SHA1 zu finden.

Für Fenster:

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android 

Für Linux:

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android 

Kopieren Sie die SHA1 und fügen Sie ihn in die Aktivieren Google-Services Seite.

Klicken Sie nun auf Google Signin aktivieren. Und klicken Sie auf Weiter, um Konfigurationsdateien zu generieren. Klicken Sie nun auf google-services.json, um Ihre Konfigurationsdatei zu erhalten. Komm jetzt zu Android Studio.

Kopieren Sie die JSON-Konfigurationsdatei (Sie gerade heruntergeladen) und fügen Sie sie in Ihrem Google-Login Android Studio-Projekt (Inside App/Verzeichnis). configuration file

Öffnen Sie die build.gradle-Datei auf oberster Ebene und fügen Sie die folgende Zeile in die Abhängigkeiten ein.

In build.gradle Datei fügen Sie die folgenden Abhängigkeiten und synchronisieren Sie Ihr Google-Login-Android-Projekt.

compile 'com.google.android.gms:play-services-auth:8.3.0' 
    compile 'com.mcxiaoke.volley:library-aar:1.0.0' // this is for fetching the profile image of the user 

Kommen Sie nun zu activity_main.xml.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

    <com.google.android.gms.common.SignInButton 
     android:id="@+id/sign_in_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" /> 

    <com.android.volley.toolbox.NetworkImageView 
     android:id="@+id/profileImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:text="Name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/textViewName" 
     android:textStyle="bold" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 


    <TextView 
     android:text="email" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 


    <TextView 
     android:id="@+id/textViewEmail" 
     android:textStyle="bold" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

Jetzt kommen Sie zu MainActivity.java und schreiben Sie den folgenden Code.

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.android.volley.toolbox.ImageLoader; 
import com.android.volley.toolbox.NetworkImageView; 
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 org.w3c.dom.Text; 

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

    //Signin button 
    private SignInButton signInButton; 

    //Signing Options 
    private GoogleSignInOptions gso; 

    //google api client 
    private GoogleApiClient mGoogleApiClient; 

    //Signin constant to check the activity result 
    private int RC_SIGN_IN = 100; 

    //TextViews 
    private TextView textViewName; 
    private TextView textViewEmail; 
    private NetworkImageView profilePhoto; 

    //Image Loader 
    private ImageLoader imageLoader; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //Initializing Views 
     textViewName = (TextView) findViewById(R.id.textViewName); 
     textViewEmail = (TextView) findViewById(R.id.textViewEmail); 
     profilePhoto = (NetworkImageView) findViewById(R.id.profileImage); 

     //Initializing google signin option 
     gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
       .requestEmail() 
       .build(); 

     //Initializing signinbutton 
     signInButton = (SignInButton) findViewById(R.id.sign_in_button); 
     signInButton.setSize(SignInButton.SIZE_WIDE); 
     signInButton.setScopes(gso.getScopeArray()); 

     //Initializing google api client 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) 
       .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
       .build(); 


     //Setting onclick listener to signing button 
     signInButton.setOnClickListener(this); 
    } 


    //This function will option signing intent 
    private void signIn() { 
     //Creating an intent 
     Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 

     //Starting intent for result 
     startActivityForResult(signInIntent, RC_SIGN_IN); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     //If signin 
     if (requestCode == RC_SIGN_IN) { 
      GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
      //Calling a new function to handle signin 
      handleSignInResult(result); 
     } 
    } 


    //After the signing we are calling this function 
    private void handleSignInResult(GoogleSignInResult result) { 
     //If the login succeed 
     if (result.isSuccess()) { 
      //Getting google account 
      GoogleSignInAccount acct = result.getSignInAccount(); 

      //Displaying name and email 
      textViewName.setText(acct.getDisplayName()); 
      textViewEmail.setText(acct.getEmail()); 

      //Initializing image loader 
      imageLoader = CustomVolleyRequest.getInstance(this.getApplicationContext()) 
        .getImageLoader(); 

      imageLoader.get(acct.getPhotoUrl().toString(), 
        ImageLoader.getImageListener(profilePhoto, 
          R.mipmap.ic_launcher, 
          R.mipmap.ic_launcher)); 

      //Loading image 
      profilePhoto.setImageUrl(acct.getPhotoUrl().toString(), imageLoader); 

     } else { 
      //If login fails 
      Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    public void onClick(View v) { 
     if (v == signInButton) { 
      //Calling signin 
      signIn(); 
     } 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 
} 

Zum Laden Bild von URL mir einer benutzerdefinierten Volley Request.For die benutzerdefinierten Volley Anfrage Sie die folgende Klasse erstellen müssen verwendet haben.

import android.content.Context; 
import android.graphics.Bitmap; 
import android.support.v4.util.LruCache; 

import com.android.volley.Cache; 
import com.android.volley.Network; 
import com.android.volley.RequestQueue; 
import com.android.volley.toolbox.BasicNetwork; 
import com.android.volley.toolbox.DiskBasedCache; 
import com.android.volley.toolbox.HurlStack; 
import com.android.volley.toolbox.ImageLoader; 


public class CustomVolleyRequest { 

    private static CustomVolleyRequest customVolleyRequest; 
    private static Context context; 
    private RequestQueue requestQueue; 
    private ImageLoader imageLoader; 


    private CustomVolleyRequest(Context context) { 
     this.context = context; 
     this.requestQueue = getRequestQueue(); 

     imageLoader = new ImageLoader(requestQueue, 
       new ImageLoader.ImageCache() { 
        private final LruCache<String, Bitmap> 
          cache = new LruCache<String, Bitmap>(20); 

        @Override 
        public Bitmap getBitmap(String url) { 
         return cache.get(url); 
        } 

        @Override 
        public void putBitmap(String url, Bitmap bitmap) { 
         cache.put(url, bitmap); 
        } 
       }); 
    } 

    public static synchronized CustomVolleyRequest getInstance(Context context) { 
     if (customVolleyRequest == null) { 
      customVolleyRequest = new CustomVolleyRequest(context); 
     } 
     return customVolleyRequest; 
    } 

    public RequestQueue getRequestQueue() { 
     if (requestQueue == null) { 
      Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024); 
      Network network = new BasicNetwork(new HurlStack()); 
      requestQueue = new RequestQueue(cache, network); 
      requestQueue.start(); 
     } 
     return requestQueue; 
    } 

    public ImageLoader getImageLoader() { 
     return imageLoader; 
    } 

Jetzt hinzufügen Internet-Berechtigung zu Ihrem Manifest und führen Sie Ihre Google-Login-Android-Anwendung.

Dies ist der vollständige Prozess für die G-SignIn-Authentifizierung !!!

Verwandte Themen