2017-02-15 111 views
0

我学习Xamarin Android,我想实现Google SignIn ...但我不能这样做。我只需要使用客户端ID?我在互联网上找到一些例子,但没有任何作品......有人可以举个例子吗?或者一步一步地如何在Xamarin中做到这一点?Xamarin Android谷歌加登录登录

谢谢!

我的代码:

using Android.App; 
using Android.Widget; 
using Android.OS; 
using Android.Gms.Common.Apis; 
using Android.Gms.Common; 
using System; 
using Android.Gms.Plus; 
using Android.Content; 
using Android.Runtime; 
using Android.Gms.Plus.Model.People; 

namespace LoginGoogle 
{ 
    [Activity(Label = "LoginGoogle", MainLauncher = true, Icon = "@drawable/icon")] 
    public class MainActivity : Activity, GoogleApiClient.IConnectionCallbacks, 
     GoogleApiClient.IOnConnectionFailedListener 

{ 
    private GoogleApiClient googleApiClient; 
    private SignInButton btnGooglePlus; 
    private ConnectionResult connectionResult; 
    private bool intentProgress; 
    private bool signInClick; 
    private bool infoPopulated; 

    private TextView lblName; 
    private TextView lblTagLine; 
    private TextView lblBraggingRights; 
    private TextView lblGender; 
    private TextView lblRelationship; 

    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     // Set our view from the "main" layout resource 
     SetContentView (Resource.Layout.Main); 

     btnGooglePlus = FindViewById<SignInButton>(Resource.Id.btnGooglePlus); 

     btnGooglePlus.Click += btnGooglePlus_Click; 

     lblName = FindViewById<TextView>(Resource.Id.lblName); 
     lblTagLine = FindViewById<TextView>(Resource.Id.lblTagLine); 
     lblBraggingRights = FindViewById<TextView>(Resource.Id.lblBraggingRights); 
     lblGender = FindViewById<TextView>(Resource.Id.lblGender); 
     lblRelationship = FindViewById<TextView>(Resource.Id.lblRelationship); 

     GoogleApiClient.Builder builder = new GoogleApiClient.Builder(this); 
     builder.AddConnectionCallbacks(this); 
     builder.AddOnConnectionFailedListener(this); 
     builder.AddApi(PlusClass.API); 
     builder.AddScope(PlusClass.ScopePlusProfile); 
     builder.AddScope(PlusClass.ScopePlusLogin); 
     //Build our GoogleApiClient 
     googleApiClient = builder.Build(); 

    } 

    void btnGooglePlus_Click(object sender, EventArgs e) 
    { 
     if (!googleApiClient.IsConnecting) 
     { 
      signInClick = true; 
      resolveSigInError(); 
     } 
    } 

    private void resolveSigInError() 
    { 
     if (!googleApiClient.IsConnected) 
     { 
      //No need to resolve errors 
      return; 
     } 

     if (connectionResult.HasResolution) 
     { 
      try 
      { 
       intentProgress = true; 
       StartIntentSenderForResult(connectionResult.Resolution.IntentSender, 0, null, 
        0, 0, 0); 
      } catch (Android.Content.IntentSender.SendIntentException e) 
      { 
       intentProgress = false; 
       googleApiClient.Connect(); 
      } 

     } 
    } 

    protected override void OnActivityResult(int requestCode, 
     [GeneratedEnum] Result resultCode, Intent data) 
    { 
     if(requestCode == 0) 
     { 
      if(resultCode != Result.Ok) 
      { 
       signInClick = false; 
      } 

      intentProgress = false; 
      if (googleApiClient.IsConnecting) 
      { 
       googleApiClient.Connect(); 
      } 
     } 
    } 

    protected override void OnStart() 
    { 
     base.OnStart(); 
     googleApiClient.Connect(); 
    } 

    protected override void OnStop() 
    { 
     base.OnStop(); 
     if (googleApiClient.IsConnected) 
     { 
      googleApiClient.Disconnect(); 
     } 

    } 

    public void OnConnected(Bundle connectionHint) 
    { 
     //Successful login 
     signInClick = false; 

     if (PlusClass.PeopleApi.GetCurrentPerson(googleApiClient) != null) 
     { 
      IPerson plusUser = PlusClass.PeopleApi.GetCurrentPerson(googleApiClient); 

      if (plusUser.HasDisplayName) 
      { 
       lblName.Text += plusUser.DisplayName; 
      } 

      if (plusUser.HasTagline) 
      { 
       lblTagLine.Text += plusUser.Tagline; 
      } 

      if (plusUser.HasBraggingRights) 
      { 
       lblBraggingRights.Text += plusUser.BraggingRights; 
      } 

      { 
       switch (plusUser.RelationshipStatus) 
       { 
        case 0: 
         lblRelationship.Text += "Single"; 
         break; 

        case 1: 
         lblRelationship.Text += "In a relationship"; 
         break; 

        case 2: 
         lblRelationship.Text += "Engaged"; 
         break; 

        case 3: 
         lblRelationship.Text += "Married"; 
         break; 

        case 4: 
         lblRelationship.Text += "It's complicated"; 
         break; 

        case 5: 
         lblRelationship.Text += "In an open relationship"; 
         break; 

        case 6: 
         lblRelationship.Text += "Widowed"; 
         break; 

        case 7: 
         lblRelationship.Text += "In a domestic partnership"; 
         break; 

        case 8: 
         lblRelationship.Text += "In a civil union"; 
         break; 

        default: 
         lblRelationship.Text += "Unknown"; 
         break; 
       } 
      } 

      if (plusUser.HasGender) 
      { 
       switch (plusUser.Gender) 
       { 
        case 0: 
         lblGender.Text += "Male"; 
         break; 

        case 1: 
         lblGender.Text += "Female"; 
         break; 

        case 2: 
         lblGender.Text += "Other"; 
         break; 

        default: 
         lblGender.Text += "Unknown"; 
         break; 
       } 

       infoPopulated = true; 
      } 
     } 



    } 

    public void OnConnectionSuspended(int cause) 
    { 
     throw new NotImplementedException(); 
    } 

    public void OnConnectionFailed(ConnectionResult result) 
    { 
     if (intentProgress) 
     { 
      //Store the ConnectionResult so that we can use it later when the user 
      //clicks 'sign in' 
      connectionResult = result; 

      if (signInClick) 
      { 
       //The user has already clicked 'signin' so we attempt to resolve all 
       //errors until the user is signed in 
       resolveSigInError(); 
      } 

     } 


    } 
} 

}

enter code here 

回答

1

请参阅xamarin sample在谷歌的标志在此示例中你不需要客户端ID。欲了解更多谷歌登录信息,请查看谷歌发展document

我赶上一些例子在互联网,但没有工作......

我想你可能没有授权的应用程序。

下载示例并确保您在使用前已在Google Developers Console中授权该应用。

授权该应用

  1. 打开link

  2. 创建您的项目名称(无论您写什么)并为其添加包名称。在此示例中,软件包名称是com.xamarin.signinquickstart

  3. 获取您的SHA-1请参考此link

  4. 构建并部署演示应用程序并使用Google帐户登录。

屏幕截图:

enter image description here

,但我没有能够做到这一点。我只需要使用客户端ID?

如何使用客户端ID登录上

根据谷歌document。我想你可以尝试用客户端ID登录方式:

GoogleSignInOptions gso = new  GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn) 
      .RequestIdToken("YOUR_CLIENT_ID") 
      .RequestEmail() 
      .Build(); 
mGoogleApiClient = new GoogleApiClient.Builder (this) 
    .EnableAutoManage(mLoginFragment, failedHandler) 
    .AddApi (Auth.GOOGLE_SIGN_IN_API,gso) 
    .Build() 
+0

还有一个视频教程,可以帮助你 - [Xamarin的Android教程36登入Google+在第1部分(https://www.youtube.com/watch?v = 3wH-g59JfFY)和[Xamarin Android Tutorial 37 Google+登录第2部分](https://www.youtube.com/watch?v=GKoXQTFCWuE)。希望这可以帮助。 –