Android onConnectionFailed 给出 SIGN_IN_REQUIRED(4)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23736137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:22:54  来源:igfitidea点击:

onConnectionFailed giving SIGN_IN_REQUIRED(4)

androidgoogle-playgoogle-play-services

提问by DCoder

I am developing an Android application where I want to use the Google API. For that I have imported the google-play-service-libproject.

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

I am following this linkto initialize GoogleApiClient object.

我正在关注此链接以初始化 GoogleApiClient 对象。

My code:

我的代码:

1) In the onCreate()method I am building the GoogleApiClientobject:

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

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

2) In onStart(), I call mGoogleApiClient.connect().

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

3) My activity implements ConnectionCallbacksand OnConnectionFailedListener.

3) 我的活动实现了 ConnectionCallbacksOnConnectionFailedListener。

4) My onConnectionFailed()method looks like:

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) My onActivityResult()method contains:

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

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

When I run my app I get a Toastthat says that an internal error popped up. I did create the project in the Google console.

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

回答by user2642219

I had the same problem.

我有同样的问题。

From documentation: The client may choose to continue without using the API or it may call startResolutionForResult(Activity, int) to prompt the user to sign in.

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

So you should try to sign in by using startResolutionForResult() function

所以你应该尝试使用 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;
}

回答by Naskov

Just follow google's instructionss

只需按照谷歌的说明

AND CAUTION

小心

Since you are still in development mode, check if you have added your testing email address in the GAME DETAILScenter before publishing the game.

由于您仍处于开发模式,请在发布游戏前检查您是否在游戏详情中心添加了您的测试电子邮件地址。

回答by Michael Chinen

Check that you are signing the app with a keystore that is in the apk uploaded to Google Play Developer's Console (if testing, you can upload as alpha and publish while keeping it private).

检查您是否使用上传到 Google Play 开发者控制台的 apk 中的密钥库对应用程序进行签名(如果进行测试,您可以以 alpha 的形式上传并发布,同时保持其私密性)。

If not this, it could be other things. Make sure your account's email address is listed in testers in the Account details page (on the settings menu with a gear icon), and the licensed response is set to LICENSED, NOT to RESPOND_NORMALLY

如果不是这个,它可能是其他的东西。确保您的帐户的电子邮件地址在帐户详细信息页面(在带有齿轮图标的设置菜单上)的测试人员中列出,并且许可响应设置为 LICENSED,而不是 RESPOND_NORMALLY

回答by challenger

Drive api should be enabled from the console page.

Drive api 应该从控制台页面启用。

回答by user6627139

Not sure if this is the bestanswer but it worked for me. I copied onConnectionFailed() from the BasicSamples TakeANumber MainActivity. It calls BaseGameUtils to resolve. Of course that implies you have the BaseGameUtils library included in your project and that's another can of worms. But maybe you can get by with the one method so I copied it below.

不确定这是否是最好的答案,但它对我有用。我从 BasicSamples TakeANumber MainActivity 复制了 onConnectionFailed()。它调用 BaseGameUtils 来解决。当然,这意味着您的项目中包含 BaseGameUtils 库,这是另一种蠕虫。但也许你可以通过一种方法来解决,所以我在下面复制了它。

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
   logger.info( "onConnectionFailed() *play* : attempting to resolve");
    if (mResolvingConnectionFailure) {
       logger.info( "onConnectionFailed(): already resolving");
        return;
    }

    if (mSignInClicked || mAutoStartSignInFlow) {
        mAutoStartSignInFlow = false;
        mSignInClicked = false;
        mResolvingConnectionFailure = true;
        if (!BaseGameUtils.resolveConnectionFailure(this, mGoogleApiClient, connectionResult,
                RC_SIGN_IN, getString(R.string.signin_other_error))) {
            mResolvingConnectionFailure = false;
        }
    }
}

From Google BasicSamples BaseGameUtils.java:

来自 Google BasicSamples BaseGameUtils.java:

 public static boolean resolveConnectionFailure(Activity activity,
                                               GoogleApiClient client, ConnectionResult result, int requestCode,
                                               String fallbackErrorMessage) {

    if (result.hasResolution()) {
        try {
            result.startResolutionForResult(activity, requestCode);
            return true;
        } catch (IntentSender.SendIntentException e) {
            // The intent was canceled before it was sent.  Return to the default
            // state and attempt to connect to get an updated ConnectionResult.
            client.connect();
            return false;
        }
    } else {
        // not resolvable... so show an error message
        int errorCode = result.getErrorCode();
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(errorCode,
                activity, requestCode);
        if (dialog != null) {
            dialog.show();
        } else {
            // no built-in dialog: show the fallback error message
            showAlert(activity, fallbackErrorMessage);
        }
        return false;
    }
}

回答by Hounshell

This error indicates that the user needs to authorize your app. There's a full workflow for this, following the tutorial at https://developers.google.com/+/mobile/android/getting-startedlook for "onConnectionFailed"

此错误表示用户需要授权您的应用程序。有一个完整的工作流程,按照https://developers.google.com/+/mobile/android/getting-started 上的教程查找“onConnectionFailed”

回答by Max

if you are not using debug keys, push app to google play as alpha, add yourself as a tester and MOST important - follow the "Opt-in" link on Aplha page and CONFIRM that you are the tester.

如果您不使用调试密钥,请将应用程序作为 alpha 推送到 google play,将自己添加为测试人员,最重要的是 - 按照 Aplha 页面上的“选择加入”链接并确认您是测试人员。

回答by Jonathan

I'd like to share my experience with this. My case was when using Google Play Services for Google Play Games. I was also getting the onConnectionFailed giving SIGN_IN_REQUIRED error. Finally I realize I had not "Published" my Game settings in the developer console. Not to be mistaken for publishing an "alpha" or "beta" version of your apk. I mean the actual Google Play Games "Game" you create and link to the game's APK.

我想分享我在这方面的经验。我的情况是在为 Google Play 游戏使用 Google Play 服务时。我也得到了 onConnectionFailed 给出 SIGN_IN_REQUIRED 错误。最后我意识到我没有在开发者控制台中“发布”我的游戏设置。不要被误认为发布了您的 apk 的“alpha”或“beta”版本。我的意思是您创建并链接到游戏 APK 的实际 Google Play 游戏“游戏”。

回答by Anjali

I had Same problem and solved with this solution , I actually had very old version google play-services library so I updated It with latest google play-service library to compile 'com.google.android.gms:play-services:11.0.4'from my previous version library to support your android SDK and maintain compileSDKVersion and targetSDKVersion in gradle:app. add google drive enabled API_KEY in manifest.and try again

我有同样的问题并用这个解决方案解决了,我实际上有非常旧版本的谷歌播放服务库,所以我用最新的谷歌播放服务库更新它来编译'com.google.android.gms:play-services:11.0.4 '来自我以前的版本库以支持您的 android SDK 并在 gradle:app 中维护 compileSDKVersion 和 targetSDKVersion。在清单中添加启用谷歌驱动器的 API_KEY。然后再试一次

回答by Kirill Kuzyk

In my case the problem was in sensitive scopes added and not verified:

在我的情况下,问题出在敏感范围内,但未经过验证:

https://console.developers.google.com/apis/credentialsCredentials -> OAuth consent screen -> Scopes for Google APIs

https://console.developers.google.com/apis/credentials凭据 -> OAuth 同意屏幕 -> Google API 的范围

Make sure you have no unverified scopes.

确保您没有未经验证的范围。