Xamarin Android Google登录从不抛出OnConnected回调

Iam试图在我的Xamarin Android应用程序中实现Google+登录。我刚得到一个活动,该活动显示用于登录的Google+按钮。但是,ApiClient不会抛出OnConnected回调。

我实现了接口:

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登录”,我会拨打以下电话:

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。我想念什么吗?我确实在开发者控制台中注册了该应用程序。对此有多个线程,但到目前为止没有任何帮助:(

寿司宿醉

尝试将范围更改为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();
        };
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Xamarin XML Android:onClick回调方法

来自分类Dev

Xamarin android应用抛出ResourceNotFoundException

来自分类Dev

Android XAMARIN:相机意图在回调中返回空数据

来自分类Dev

适用于Android Xamarin的Google帐户登录集成

来自分类Dev

在Xamarin Android上使用Google Maps

来自分类Dev

Google Drive API实施Xamarin Android

来自分类Dev

在Xamarin Android上使用Google Maps

来自分类Dev

Google Drive API实施Xamarin Android

来自分类Dev

Xamarin.Android与Android

来自分类Dev

Android(xamarin)中的DownloadManager抛出Java.Lang.IllegalArgumentException

来自分类Dev

Xamarin-MediaRouteButton的ShowDialog()方法抛出NullPointerException(Xamarin.Android.Support.v7.MediaRouter)

来自分类Dev

Xamarin-MediaRouteButton的ShowDialog()方法抛出NullPointerException(Xamarin.Android.Support.v7.MediaRouter)

来自分类Dev

Xamarin Android 在登录和 cookie 后获取当前 URL

来自分类Dev

Xamarin表格Google登录

来自分类Dev

使用 Google 登录以获取应用的 UWP 和 Xamarin.Forms (android) 版本

来自分类Dev

Xamarin表单的BackDoor(Android)

来自分类Dev

降级Xamarin.Android

来自分类Dev

Xamarin清除Android日志

来自分类Dev

Android / Xamarin代码混淆

来自分类Dev

Xamarin Android上的振动

来自分类Dev

Xamarin Android日历活动

来自分类Dev

Xamarin Android中的BindableViewPager

来自分类Dev

NAudio和Xamarin Android

来自分类Dev

Xamarin Android绑定

来自分类Dev

Xamarin Android MvvmCross入门

来自分类Dev

pushwoosh onMessageReceive xamarin android

来自分类Dev

Xamarin Android-GetApplication

来自分类Dev

Xamarin Android项目

来自分类Dev

Xamarin的Android ListView错误