Xamarin Android Google登录OnConnected Callback永远不会抛出

时间:2016-07-10 11:19:58

标签: android xamarin xamarin.android google-plus

Iam尝试在我的Xamarin Android应用程序中实施Google+登录。我刚刚获得了一个显示Google + -Button for SignIn的活动。但是ApiClient永远不会抛出OnConnected Callback。

我实现了接口:

GoogleApiClient.IConnectionCallbacks, GoogleApiClient.IOnConnectionFailedListener

在OnCreate中,我初始化Google-Stuff:

var googleLogin = FindViewById<SignInButton>(Resource.Id.google_login);
googleLogin.SetSize(SignInButton.SizeWide);
googleLogin.Click += GoogleLogin_Click;

_googleApiClient = new GoogleApiClient.Builder(this)
.AddConnectionCallbacks(this)
.AddOnConnectionFailedListener(this)
.AddApi(PlusClass.API)
.AddScope(new Scope(Scopes.Profile))
.Build();

如果有人点击Google-SignIn i,请致电以下网址:

private void GoogleLogin_Click(object sender, System.EventArgs e)
{
   _googleApiClient.Connect();
}

然后我想我可以通过这种方式获取用户信息:

public void OnConnected(Bundle connectionHint)
{
  var people = PlusClass.PeopleApi.GetCurrentPerson(_googleApiClient);
}

提示:如果有人知道如何以现代方式解决这个问题,请将GetCurrentPerson标记为已弃用,请告诉我:)

在OnStop()中,我只是断开客户端。但我的问题是OnConnected永远不会被调用。我错过了什么吗?我肯定在开发者控制台中注册了应用程序。这有几个主题,但到目前为止没有任何帮助:(

1 个答案:

答案 0 :(得分:1)

尝试将范围更改为DriveClass.ScopeFile

_googleApiClient = new GoogleApiClient.Builder(Application.Context)
    .UseDefaultAccount()
    .AddConnectionCallbacks(this)
    .AddOnConnectionFailedListener(this)
    .AddApi(PlusClass.API)
    .AddScope(PlusClass.ScopePlusLogin)
    .Build();

Connect拨打OnConnectionFailed来回拨打ConnectionResult来回拨打OnConnected,然后在用户选择帐户后登录public class MainActivity : Activity, GoogleApiClient.IConnectionCallbacks, GoogleApiClient.IOnConnectionFailedListener { GoogleApiClient client; bool _resolvingError; const string TAG = "MyGPlus"; const int REQUEST_RESOLVE_ERROR = 999; public void OnConnected(Bundle connectionHint) { Log.Debug(TAG, "OnConnected"); } public void OnConnectionFailed(ConnectionResult result) { Log.Debug(TAG, "OnConnectionFailed"); if (_resolvingError) return; if (result.HasResolution) { try { _resolvingError = true; result.StartResolutionForResult(this, REQUEST_RESOLVE_ERROR); } catch (IntentSender.SendIntentException e) { Log.Debug(TAG, e.Message); client.Connect(); } } else { ShowErrorDialog(result.ErrorCode); } } void ShowErrorDialog(int errorCode) { var dialogFragment = new ErrorDialogFragment(); var args = new Bundle(); args.PutInt("dialog_error", errorCode); dialogFragment.Arguments = args; dialogFragment.Show(FragmentManager, "errordialog"); } public void OnConnectionSuspended(int cause) { Log.Debug(TAG, "OnConnectionSuspended"); } protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data) { base.OnActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_RESOLVE_ERROR) { if (resultCode == Result.Ok) { if (!client.IsConnecting && !client.IsConnected) { client.Connect(); } } else if (resultCode == Result.Canceled) { Log.Debug(TAG, "Result.Canceled"); } } } protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Main); Button button = FindViewById<Button>(Resource.Id.myButton); button.Click += delegate { client = new GoogleApiClient.Builder(Application.Context) .UseDefaultAccount() .EnableAutoManage() .AddConnectionCallbacks(this) .AddOnConnectionFailedListener(this) .AddApi(PlusClass.API) .AddScope(PlusClass.ScopePlusLogin) .Build(); _resolvingError = false; client.Connect(); }; } } 即可。称为...

更新

手动管理连接示例:

App