I’m trying the Android app using Firebase.
I need to authenticate by Google Client API in order to use Firebase.
But, I can’t authenticate by Google Client API.
In following code, GoogleSignInResult.isSuccess() returns false.
GoogleSignInResult.getState().getStateMessage() returns null. So, I don’t know why Google authentication failed.
Is there something you have to do when using Google authentication?
Thanks in advance.
Note:
I can authenticate by Google Client API when I install release.apk by the command “add install release.apk”.
But I cannot authenticate when I install via Google Play Store( not BETA but RELEASE).
Screenshot:
After login button pressed(login button:lower right button)
Code:
In onActivityResult, GoogleSignInResult.isSuccess() returns false.
public class LoginActivity extends AppCompatActivity {
private static final int REQUEST_CODE_SIGN_IN = 9001;
private FirebaseAuth firebaseAuth;
public static GoogleApiClient googleAPIClient;
private DatabaseReference usersRef;
private ProgressDialog progressDialog;
private GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Toast.makeText(LoginActivity.this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("*** LoginActivity.onCreate - start");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
try {
progressDialog = new ProgressDialog(this);
GoogleSignInOptions options = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
googleAPIClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, onConnectionFailedListener)
.addApi(Auth.GOOGLE_SIGN_IN_API, options)
.build();
firebaseAuth = FirebaseAuth.getInstance();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("*** LoginActivity.onCreate - start");
}
#Override
public void onStop() {
System.out.println("LoginActivity.onStop - start");
if (null != usersRef) {
usersRef.removeEventListener(valueEventlistener);
}
super.onStop();
}
public void onCancelButtonClick(View view) {
finish();
}
public void onLoginButtonClick(View view) {
System.out.println("*** LoginActivity.onLoginButtonClick - start");
Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleAPIClient);
startActivityForResult(intent, REQUEST_CODE_SIGN_IN);
System.out.println("*** LoginActivity.onLoginButtonClick - end");
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("*** LoginActivity.onActivityResult - start");
if (requestCode == REQUEST_CODE_SIGN_IN) {
System.out.println("*** LoginActivity.onActivityResult - in if");
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
System.out.println("*** LoginActivity.onActivityResult - in if success");
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
System.out.println("*** LoginActivity.onActivityResult - in if error");
System.out.println("*** LoginActivity.onActivityResult - in if error state:" + result.getStatus().getStatusMessage());
Toast.makeText(LoginActivity.this, "Error:" + result.getStatus().getStatusMessage(), Toast.LENGTH_SHORT).show();
}
}
System.out.println("*** LoginActivity.onActivityResult - end");
}
public void firebaseAuthWithGoogle(GoogleSignInAccount account) {
// firebase authentication
}
Have you added your Release version SHA1 Fingerprint in the Firebase Project Setting?
Related
I was developing an android app which uses google sign-in process (which was already integrated into my app). It worked for me some months ago but doesn't work now(google updates some policies or whatever!). Can anyone suggest what is going on here? Thanks in advance.
Here's my code for integrating google sign-in process:
public class login extends AppCompatActivity {
private SignInButton signInButton;
private TextView textView, signT;
private static final int SIGNIN_CODE = 1;
private GoogleApiClient mGoogleApiClient;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private final static String TAG = "Login Info:";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("Val", "That i need");
startActivity(i);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
finish();
}
}
};
signInButton = findViewById(R.id.SignIn);
signT = (TextView) signInButton.getChildAt(0);
signT.setText("Sign in with Google");
textView = findViewById(R.id.negativetext);
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
textView.setText(getString(R.string.negativetext));
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
finish();
}
});
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
signInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
signIn();
}
});
}
#Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, SIGNIN_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SIGNIN_CODE) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Authenicate with my Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
ProgressDialog dialog = new ProgressDialog(this); //ProgressDialog.show(this, null, "Please wait while we load sign in..", true);
dialog.setMessage("Please wait while we load sign in..");
dialog.setIndeterminate(true);
Drawable drawable = new ProgressBar(this).getIndeterminateDrawable().mutate();
drawable.setColorFilter(ContextCompat.getColor(this, R.color.progress),
PorterDuff.Mode.SRC_IN);
dialog.setIndeterminateDrawable(drawable);
dialog.show();
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed?
Log.w(TAG, "Google sign in failed", e);
}
}
}
private void firebaseAuthWithGoogle(final GoogleSignInAccount acct) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
//Debugging :(
//Toast.makeText(getApplicationContext(), "" + task.isSuccessful(), Toast.LENGTH_LONG).show();
if (task.isSuccessful()) {
//spinner.setVisibility(ProgressBar.INVISIBLE);
Log.i(TAG, "signInWithCredential:success");
} else {
Toast.makeText(getApplicationContext(), "We couldn't load sign in", Toast.LENGTH_LONG).show();
}
}
});
}}
Use below code, i already used it and it works perfectely for me:
private void configureGoogleSignIn() {
GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(BuildConfig.DEFAULT_WEB_CLIENT_ID)
.requestEmail()
.build();
googleSignInClient = GoogleSignIn.getClient(context, googleSignInOptions);
}
Intent signInIntent = googleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
and onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> signInAccountTask = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(signInAccountTask);
} else {
}
}
For more help you can go through my below sample link:
https://github.com/rishavsingla/FirebaseAuthenticationSample
I don't know what problem occurred and gave me error code 12500, I visited google documentation & found no solution for error code 12500. So, I deleted my previous firebase authentication entry in console.firebase.google.com and then re-pasted the newly created google-services.json file into the app directory, and the rebuild.
That finally fixed my horrible issues!
I want to integrate Google sign in to my game. But i don't want to make sign in process with a button I want it happen once user opens application.
Whenever MenuActivity is created it asks to choose an account to sign in. But I want it to choose account only once (first time) and remember every time. Here is code:
public class MenuActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
private static final String TAG = MainActivity.class.getSimpleName();
private static final int RC_SIGN_IN = 007;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
if(!mGoogleApiClient.isConnected()){
signIn();
}
}
public void startGame(View view){
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
#Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
#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) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
#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);
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
} else {
}
}
}
First create SharedPreferences
public void saveUser (String key, String value ) {
SharedPreferences pref = getSharedPreferences("YourPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString(key, value);
editor.commit();
}
public String getUser (String key) {
SharedPreferences pref = getSharedPreferences("YourPref", MODE_PRIVATE);
return pref.getString(key, "");
}
Check user's email if it is empty add SharedPreferences
if(!mGoogleApiClient.isConnected() && getUser("email").isEmpty() ){
signIn();
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
saveUser("email", acct.getEmail());
saveUser("name", acct.getDisplayName());
} else {
}
}
I have used Google sign-in feature in my android app. I have used their standard code for login and have tested it by deploying it on my mobile device in debug mode. Everything works fine this way.
I have now moved my app to Google Play Store for closed beta testing. When I install my app from Play Store it fails at the login step as I noticed it returns null for user's email, id and displayName fields. I have no idea what is going wrong after installing it from Play Store.
The sign-in code is given below:
private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
private GoogleSignInAccount acct;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setScopes(gso.getScopeArray());
#Override
public void onStart() {
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
isFirstLogin = false;
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
isFirstLogin = true;
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
#Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
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) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
// [END onActivityResult]
// [START handleSignInResult]
private void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
acct = result.getSignInAccount();
mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
updateUI(true);
final AsyncTask<Integer, Void, HelloUserResponse> addUser =
new AsyncTask<Integer, Void, HelloUserResponse>()
{
#Override
protected HelloUserResponse doInBackground(Integer... integers) {
// Retrieve service handle.
Helloworld apiServiceHandle = AppConstants.getApiServiceHandle();
try {
HelloUser heUser = new HelloUser();
heUser.setUseremail(acct.getEmail());
heUser.setUserid(acct.getId());
heUser.setUsername(acct.getDisplayName());
Helloworld.Greetings.AddUser addUserCommand = apiServiceHandle.greetings().addUser(heUser);
HelloUserResponse helloUserRsp = addUserCommand.execute();
return helloUserRsp;
} catch (IOException e) {
Toast.makeText(SignInActivity.this, "Something went wrong",
Toast.LENGTH_SHORT).show();
}
return null;
}
#Override
protected void onPostExecute(HelloUserResponse helloUserRsp) {
if(helloUserRsp.getResult()!=0L)
{
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
SharedPreferences myprefs = getApplicationContext().getSharedPreferences("user", MODE_WORLD_READABLE);
myprefs.edit().putString("user_name", acct.getDisplayName()).commit();
myprefs.edit().putString("user_email", acct.getEmail()).commit();
myprefs.edit().putLong("user_id", helloUserRsp.getResult()).commit();
startActivity(myIntent);
}
else {
updateUI(false);
}
}
};
if(isFirstLogin)
addUser.execute(0);
else
{
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(myIntent);
}
}
else {
// Signed out, show unauthenticated UI.
updateUI(false);
}
}
I'm trying to use authentication via google's firebase but facing a problem
In the code below "GoogleSignInResult result"'s value is always false, What should I do?
I've configured the project properly in firebase console, added the JSON file in app directory
#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) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
Log.d("FALSE", String.valueOf(result.isSuccess()));
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed, update UI appropriately
// [START_EXCLUDE]
updateUI(null);
// [END_EXCLUDE]
}
}
}
You have check your generated SHA1 key is same as in OAuth 2.0 client ID's Signing-certificate fingerprint. Both keys are must be same
This is a working code, you may try this:
GoogleApiClient mGoogleApiClient;
private static final int RC_SIGN_IN = 9001;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed:" + connectionResult);
}
} /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
assert signInButton != null;
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
}
}
});
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d("Login Google", "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
firebaseAuthWithGoogle(acct);
Toast.makeText(this, "User name: " + acct.getDisplayName(), Toast.LENGTH_SHORT).show();
} else {
// Signed out, show unauthenticated UI.
}
}
// Firebase with Google
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(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
#Override
protected 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) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
Step 1:
In Firebase project you can add SHA1 debug.keystore
or if it’s for production add the SHA1 keystore release
Step 2:
In the console google developer add an « id clients OAuth » for debug or for release it depends on step 1
I hope it help you
I am trying to integrate Facebook and Google authentication for my mobile application. I have followed Google and Facebook tutorials on how to do it and both projects work fine when they are separated. In my current solution signing in with Google works ok but Facebook gives me following error msg:
Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
E/Parcel: Class not found when unmarshalling: com.facebook.login.LoginClient$Request
Caused by: java.lang.NoClassDefFoundError: com/facebook/login/LoginClient$Request
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.facebook.login.LoginClient$Request" on path
Here is my LoginAvtivity class I use for authentication:
public class ActivityLogin extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
private static final String TAG = "SignInActivity";
private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
private ProgressDialog mProgressDialog;
private LoginButton facebookButton;
private CallbackManager facebookCallbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
facebookCallbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_login);
// Button listeners
findViewById(R.id.google_button).setOnClickListener(this);
facebookButton = (LoginButton)findViewById(R.id.facebook_button);
// [START configure_signin]
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken(getString(R.string.server_client_id))
.build();
// [START build_client]
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
// [START customize_button]
// Customize sign-in button. The sign-in button can be displayed in
// multiple sizes and color schemes. It can also be contextually
// rendered based on the requested scopes. For example. a red button may
// be displayed when Google+ scopes are requested, but a white button
// may be displayed when only basic profile is requested. Try adding the
// Scopes.PLUS_LOGIN scope to the GoogleSignInOptions to see the
// difference.
SignInButton signInButton = (SignInButton) findViewById(R.id.google_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setScopes(gso.getScopeArray());
setGooglePlusButtonText(signInButton, "Sign in with Google");
facebookButton.registerCallback(facebookCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Intent intent = new Intent(ActivityLogin.this, MainActivity.class);
startActivity(intent);
}
#Override
public void onCancel() {
Log.d("TOKEN", "Canceled!!!!!!!!!!");
}
#Override
public void onError(FacebookException error) {
Log.d("TOKEN", "CHUUUUUUJ WIELKI!!!!!!!!!!!!");
}
});
}
#Override
public void onStart() {
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d(TAG, "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
#Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
// [START onActivityResult]
#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) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
// [END onActivityResult]
// [START handleSignInResult]
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
//mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
//updateUI(true);
Intent intent = new Intent(ActivityLogin.this, MainActivity.class);
startActivity(intent);
String idToken = acct.getIdToken();
if (idToken != null) {
Log.d("TOKEN", idToken);
//sendEmail(idToken);
} else {
Log.d("TOKEN", "CHUUUUUUJ WIELKI!");
}
} else {
// Signed out, show unauthenticated UI.
//updateUI(false);
}
}
// [END handleSignInResult]
// [START signIn]
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
// [END signIn]
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
// be available.
Log.d(TAG, "onConnectionFailed:" + connectionResult);
}
private void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage(getString(R.string.loading));
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
private void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.hide();
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.google_button:
signIn();
break;
case R.id.tutorial:
Intent intent = new Intent(this, TutorialActivity.class);
startActivity(intent);
break;
// case R.id.disconnect_button:
// revokeAccess();
// break;
}
}
protected void setGooglePlusButtonText(SignInButton signInButton, String buttonText) {
// Find the TextView that is inside of the SignInButton and set its text
for (int i = 0; i < signInButton.getChildCount(); i++) {
View v = signInButton.getChildAt(i);
if (v instanceof TextView) {
TextView tv = (TextView) v;
tv.setText(buttonText);
return;
}
}
}
}
And here is the xml layout for this class:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context=".ActivityLogin"
android:background="#drawable/background_gradient">
<com.facebook.login.widget.LoginButton
android:id="#+id/facebook_button"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_above="#+id/google_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp" />
<com.google.android.gms.common.SignInButton
android:id="#+id/google_button"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp"
/>
It seems that I am missing something imporant here but I cannot find what it is. The first error message says that I am missing permissions but according to both google and facebook tutorials this particular permission is not required.
You are not handling the Facebook result in your onActivityResult() method.
Add this to your method -
#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) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
else{
//Add this to handle Facebook result
facebookCallbackManager.onActivityResult(requestCode, resultCode, data);
}
}