【问题标题】:Play services 11.6: OAuth consent screen not showing when publishedPlay services 11.6:发布时未显示 OAuth 同意屏幕
【发布时间】:2017-11-08 09:26:43
【问题描述】:

尽管从官方示例应用中复制了代码,但在通过 Play 商店发布应用时未显示身份验证同意屏幕。我已经在 Google Api Developer Console 上正确生成了两个 OAuth 2.0 凭据,一个用于发布(使用我的私有密钥库),一个用于调试(使用 Android Studio 调试密钥库)。

更新:在调试模式下在较旧的 4.4 模拟器上安装我的应用程序我注意到在较新的设备上发布应用程序的行为相同。没有出现同意屏幕,Logcat 显示此消息:

W/GooglePlayServicesUtil:Google Play 服务已过期。需要 11717000 但找到了 11509030

可能是较新的GoogleApi 界面无法提示用户安装/更新 PlayServices,即使官方文档这么说?

这是我的代码:

build.gradle(应用程序)

compile 'com.google.android.gms:play-services-auth:11.6.0'
compile 'com.google.android.gms:play-services-drive:11.6.0'

AndroidManifest.xml

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

DriveActivity.java

private static final int REQUEST_CODE_SIGN_IN = 0;

protected GoogleSignInClient mGoogleSignInClient;
protected DriveClient mDriveClient;
protected DriveResourceClient mDriveResourceClient;

protected abstract void onDriveConnected();

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_SIGN_IN:
                if (resultCode == RESULT_OK) {
                    Task<GoogleSignInAccount> getAccountTask = GoogleSignIn.getSignedInAccountFromIntent(data);
                    if (getAccountTask.isSuccessful()) {
                        initializeDriveClient(getAccountTask.getResult());
                    }
                    else {
                        Toast.makeText(this, "Sign-in failed.", Toast.LENGTH_LONG).show();
                    }
                }
                break;
        }
    }

    protected void signIn() {
        GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this);
        if (signInAccount != null && signInAccount.getGrantedScopes().contains(Drive.SCOPE_FILE)) {
            initializeDriveClient(signInAccount);
        }
        else {
            GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestScopes(Drive.SCOPE_FILE)
                    .build();
            mGoogleSignInClient = GoogleSignIn.getClient(this, signInOptions);
            startActivityForResult(mGoogleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN);
        }
    }

    private void initializeDriveClient(GoogleSignInAccount signInAccount) {
        mDriveClient = Drive.getDriveClient(this, signInAccount);
        mDriveResourceClient = Drive.getDriveResourceClient(this, signInAccount);
        onDriveConnected();
    }

【问题讨论】:

    标签: android google-drive-api google-play-services google-oauth google-drive-android-api


    【解决方案1】:

    我回答我自己的问题。问题是 Google Play 服务没有更新,并且最近的 GoogleApi 接口没有检查(可能是一个错误?)。 因此,您必须在尝试身份验证之前进行检查:

    protected void signIn() {
        if (checkPlayServices()) {
            ...
        }
    }
    
    private boolean checkPlayServices() {
        GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
        int result = googleAPI.isGooglePlayServicesAvailable(this);
        if (result != ConnectionResult.SUCCESS) {
            if (googleAPI.isUserResolvableError(result)) {
                googleAPI.getErrorDialog(this, result, PLAY_SERVICES_RESOLUTION_REQUEST).show();
            }
            else {
                Toast.makeText(this, R.string.message_unsupported_device, Toast.LENGTH_LONG).show();
            }
            return false;
        }
        return true;
    }
    

    希望对其他人有所帮助!

    【讨论】:

    • 如果您将 Activity 传递给 GoogleSignIn.getClient(),您应该会看到一个对话框,提示您更新 Google Play 服务。你能在物理设备上试试吗?对于没有 Play 商店的模拟器,行为略有不同(即仅显示在日志中的不可恢复错误)。
    • 嗯...这不是预期的。如果 Google Play 服务早于 SDK,getClient(Activity, options) 应该会触发一个对话框。那么您为 DriveActivity.java 粘贴的内容是从您的 Activity 代码中直接复制的吗?并且您的物理设备具有比 11.6 更旧的 Play 服务?
    • 是的,没错。我之前只是通过明确检查播放服务来解决它......
    • 我明白了。您的目标是 api 级别 26?这种组合存在一个已知问题。
    • 是的,targetSdkVersion 是 26。
    【解决方案2】:

    对于其他人,如果需要,GoogleSignIn.getClient(this /* activity */, signInOptions) 通常应该自动提示 Play 服务更新。

    但在最新的 11.6 SDK 中,此功能在 targetSdkVersion 26 中暂时中断。请考虑在这些情况下暂时使用 GoogleApiAvailability.showErrorDialogFragment(),直到在将来的版本中修复它。

    【讨论】:

      猜你喜欢
      • 2018-03-01
      • 2020-05-02
      • 1970-01-01
      • 2020-10-10
      • 2019-08-15
      • 2019-03-02
      • 2021-08-25
      • 2018-09-04
      • 2019-12-07
      相关资源
      最近更新 更多