我正在尝试将 Google+ 登录实现到我的 Xamarin Android 应用程序中。我刚收到一个显示用于登录的 Google+ 按钮的 Activity 。但是 ApiClient 永远不会抛出 OnConnected 回调。
我实现接口(interface):
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 我调用以下内容:
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();
};
}
}
关于android - Xamarin Android Google 登录 OnConnected 回调从未抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38291316/