gpt4 book ai didi

android - onConnectionFailed 给出 SIGN_IN_REQUIRED(4)

转载 作者:行者123 更新时间:2023-11-29 01:25:35 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我想在其中使用 Google API。为此,我导入了 google-play-service-lib 项目。

我正在关注这个 link初始化 GoogleApiClient 对象。

我的代码:

1) 在 onCreate() 方法中,我构建了 GoogleApiClient 对象:

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API, null)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();

2) 在 onStart() 中,我调用了 mGoogleApiClient.connect()

3) 我的 Activity 实现ConnectionCallbacksOnConnectionFailedListener。

4) 我的 onConnectionFailed() 方法如下所示:

public void onConnectionFailed(ConnectionResult result) {
//in dubug result looks like : ConnectionResult{
//statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent
// {41f8ca70: android.os.BinderProxy@41f8ca10}}
try {
if(!mIntentInProgress && result.hasResolution())
{
mIntentInProgress=true;
result.startResolutionForResult(mActivity, RC_SIGN_IN);
}

} catch (SendIntentException e) {
e.printStackTrace();
}
}

5) 我的 onActivityResult() 方法包含:

if (requestCode == RC_SIGN_IN) {
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}

当我运行我的应用程序时,我得到一个 Toast 提示弹出了一个内部错误。我确实在 Google 控制台中创建了该项目。

最佳答案

我遇到了同样的问题。

来自文档:客户端可以选择不使用 API 继续,也可以调用 startResolutionForResult(Activity, int) 来提示用户登录。

因此您应该尝试使用 startResolutionForResult() 函数登录

@Override
public void onConnectionFailed(ConnectionResult result) {
if (result.hasResolution()) {
try {
// !!!
result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
} catch (SendIntentException e) {
mPlusClient.connect();
}
}

mConnectionResult = result;
}

关于android - onConnectionFailed 给出 SIGN_IN_REQUIRED(4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34288726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com