2

In my current project, one of my team member implemented google sign-in in Xamarin.Android app. He has added keystore and sha for release also. And it is working fine in his machine in debug mode.

But when I try to run this code from my machine I always get ResultCode Result.Canceled OnActivityResult of activity

This is my code of android renderer:

public class GoogleLoginRendere : PageRenderer, 
GoogleApiClient.IOnConnectionFailedListener, Android.Views.View.IOnClickListener
{
    GoogleApiClient mGoogleApiClient;
    MainActivity activity;
    SignInButton signInButton;
    Android.Widget.Button btn;
    public GoogleLoginRendere()
    {

    }
    public void OnConnectionFailed(ConnectionResult result)
    {
        throw new NotImplementedException();
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
    {
    base.OnElementChanged(e);
    activity = Context as MainActivity;
    if (e.OldElement != null)
    {
        activity.ActivityResult -= HandleActivityResult;
    }
    if (e.NewElement != null)
    {
        activity.ActivityResult += HandleActivityResult;
    }
    btn = new Android.Widget.Button(activity);
    btn.Text = "Google SignIn";
    signInButton = new SignInButton(activity);
    signInButton.SetSize(SignInButton.SizeStandard);
    signInButton.Visibility = ViewStates.Visible;

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
     .RequestEmail()
     .Build();

    mGoogleApiClient = new GoogleApiClient.Builder(activity)
     .EnableAutoManage(activity /* FragmentActivity */, this /* OnConnectionFailedListener */)
     .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
     .Build();

    FrameLayout.LayoutParams linLayoutParam = new FrameLayout.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent);
    linLayoutParam.Gravity = GravityFlags.Center;

    activity.AddContentView(btn, linLayoutParam);
    btn.SetOnClickListener(this);

    /* signInButton.Click += (sender, ex) =>
     {
      Console.WriteLine(string.Format("Click called"));
         Intent signInIntent = Auth.GoogleSignInApi.GetSignInIntent(mGoogleApiClient);
         activity.StartActivityForResult(signInIntent, 0);
     };*/

    MessagingCenter.Subscribe<object, string>(this, "CallLogout", (sender, msg) =>
    {
        Auth.GoogleSignInApi.SignOut(mGoogleApiClient);
        signInButton.Visibility = ViewStates.Visible;
    });
    }

    private void HandleActivityResult(object sender, ActivityResultEventArgs e)
    {
        Console.WriteLine(string.Format("renderer called"));
        if (e.RequestCode == 0)
        {
        GoogleSignInResult result = Auth.GoogleSignInApi.GetSignInResultFromIntent(e.Data);
        if (result.IsSuccess)
        {
            signInButton.Visibility = ViewStates.Invisible;
            User aUser = new User();
            aUser.Email = result.SignInAccount.Email;
            App.appUser = aUser;
            MessagingCenter.Send<object, string>(this, "callLoginWebservice", "callLoginWebservice");
        }
        else
        {
            Toast.MakeText(activity, "email not found", ToastLength.Long).Show();
            Auth.GoogleSignInApi.SignOut(mGoogleApiClient);
            signInButton.Visibility = ViewStates.Visible;
        }
        }
    }
    public void OnClick(Android.Views.View v)
    {
        var signInIntent = Auth.GoogleSignInApi.GetSignInIntent(mGoogleApiClient);
        activity.StartActivityForResult(signInIntent, 0);
        //Intent signInIntent = Auth.GoogleSignInApi.GetSignInIntent(mGoogleApiClient);
        //((MainActivity)Forms.Context).StartActivityForResult(signInIntent, 1);
        //mGoogleApiClient.Connect();
    }
}

MainActivity.cs

public class ActivityResultEventArgs : EventArgs
{
    public int RequestCode { get; set; }
    public Result ResultCode { get; set; }
    public Intent Data { get; set; }
    public ActivityResultEventArgs() : base()
    { }
}

[Activity(Label = "AppName.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 
{
    public event EventHandler<ActivityResultEventArgs> ActivityResult = delegate { };
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        App.ServiceManager = new SoapServiceManager(new SoapService());

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);

        LoadApplication(new App());
    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        Console.WriteLine(string.Format("Main activity result called"));
        GoogleSignInResult result = Auth.GoogleSignInApi.GetSignInResultFromIntent(data);
        if (result != null)
        {
            if (result.IsSuccess)
            {
                Console.WriteLine(string.Format("Activity result is {0}", result.SignInAccount.Email));
            }
            else
            {
                Console.WriteLine(string.Format("Activity result is {0}", resultCode));
            }
        }
        ActivityResult(this, new ActivityResultEventArgs
        {
            RequestCode = requestCode,
            ResultCode = resultCode,
            Data = data
        });
    }
}  

I know there is not any issue in code, there must be some configuration issue. So what I have tried yet is

=> Generated SSH-1 key and added in google sign in account and replaced google-services.json

=> Tried to create new google sign in the project in my account also. with new keystore that is generated distributing the app.

Nothing worked. Still I am getting the same error.

P.S: Don't mark it as the duplicate answer. I have searched all other questions and invested 2 days time in R&D but couldn't find solution. This ques seems like mine. but it is for java android in android studio. So not all files mentioned in it is in Xamarin.Android solution.

If anybody have idea. Help me.

Nemo
  • 2,441
  • 2
  • 29
  • 63
Neelam Prajapati
  • 3,764
  • 3
  • 28
  • 62

1 Answers1

0

My guess is that you let Google manage your app signing keys, which is an option in Google Play. So the SHA1 key you generated for your release profile is useless, as Google signs your app with a different key in production.

If that's the case, then you should go to Google Play Console -> Release Management -> App Signing, and add the SHA1 fingerprint that you find there to Firebase and generate a new google-services.json that includes that key.

I hope this helps

panosmm
  • 183
  • 1
  • 8