Android 播放服务11.6:OAuth同意屏幕在发布时不显示

Android 播放服务11.6:OAuth同意屏幕在发布时不显示,android,google-drive-api,google-play-services,google-oauth,google-drive-android-api,Android,Google Drive Api,Google Play Services,Google Oauth,Google Drive Android Api,尽管从官方示例应用程序复制了代码,但当应用程序通过Play Store发布时,认证同意屏幕不会显示。我在GoogleAPI开发者控制台上正确地生成了两个OAuth2.0凭据,一个用于发布(使用我的私有密钥库),另一个用于调试(使用AndroidStudio调试密钥库) 更新:在调试模式下在旧的4.4仿真器上安装我的应用程序时,我注意到在新设备上发布的应用程序也有相同的行为。不显示同意屏幕,Logcat显示以下消息: W/GooglePlayServicesUtil:GooglePlayServi

尽管从官方示例应用程序复制了代码,但当应用程序通过Play Store发布时,认证同意屏幕不会显示。我在GoogleAPI开发者控制台上正确地生成了两个OAuth2.0凭据,一个用于发布(使用我的私有密钥库),另一个用于调试(使用AndroidStudio调试密钥库)

更新:在调试模式下在旧的4.4仿真器上安装我的应用程序时,我注意到在新设备上发布的应用程序也有相同的行为。不显示同意屏幕,Logcat显示以下消息:

W/GooglePlayServicesUtil:GooglePlayServices过时了。要求 1171700,但发现11509030

可能是新的界面无法提示用户安装/更新PlayServices,即使官方文档中有这样的说明

这是我的代码:

build.gradle(应用程序)

AndroidManifest.xml


DriveActivity.java

private static final int REQUEST\u code\u SIGN\u IN=0;
受保护的GoogleSignInClient mGoogleSignInClient;
受保护的驱动器客户端mDriveClient;
受保护的驱动器资源客户端mDriveResourceClient;
受保护的抽象无效OnDriveConnect();
@凌驾
受保护的void onActivityResult(最终int请求代码、最终int结果代码、最终意图数据){
super.onActivityResult(请求代码、结果代码、数据);
开关(请求代码){
案例请求、代码、登录:
if(resultCode==RESULT\u OK){
任务getAccountTask=GoogleSignIn.GetSignedAccountFromIntent(数据);
if(getAccountTask.isSuccessful()){
initializeDriveClient(getAccountTask.getResult());
}
否则{
Toast.makeText(这是“登录失败”,Toast.LENGTH_LONG.show();
}
}
打破
}
}
受保护的无效签名(){
GoogleSignInAccount signInAccount=GoogleSignIn.GetLastSignedAccount(此);
if(signInAccount!=null&&signInAccount.getGrantedScopes().contains(Drive.SCOPE_文件)){
初始化驱动客户机(signInAccount);
}
否则{
GoogleSignenOptions SignenOptions=新建GoogleSignenOptions.Builder(GoogleSignenOptions.DEFAULT\u登录)
.requestScopes(驱动器.SCOPE\u文件)
.build();
mGoogleSignInClient=GoogleSignIn.getClient(这是signInOptions);
startActivityForResult(mGoogleSignInClient.getSigningent(),请求\代码\登录);
}
}
private void InitializedDriveClient(谷歌签名帐户签名帐户){
mDriveClient=Drive.getDriveClient(此,signInAccount);
mDriveResourceClient=Drive.getDriveResourceClient(此,signInAccount);
OnDriveConnect();
}

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

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;
}

希望它能帮助别人

对于其他人,GoogleSignIn.getClient(此/*活动*/,signInOptions)通常应在需要时自动提示播放服务更新


但在最新的11.6 SDK中,targetSdkVersion 26暂时中断了此功能。考虑使用GooGeLAPiValue.StErrReReDraceFraceType()在这些条件下,直到在未来版本中修复它。

如果将一个活动传递给GoGoSeLigIn。GeCclipse(),您应该会看到一个对话框提示谷歌Play服务更新。你能试试物理设备吗?对于没有Play Store的模拟器,行为略有不同(即,仅在日志中显示的不可恢复错误)。。。这是预料不到的。如果Google Play服务比SDK旧,则getClient(活动、选项)应该触发一个对话框。那么,您为DriveActivity.java粘贴的内容是活动代码的直接副本?您的物理设备具有比11.6更旧的播放服务?是的,没错。我之前只是通过显式检查播放服务解决了这个问题…我明白了。您的目标是api级别26吗?这种组合存在一个已知的问题。是的,targetSdkVersion是26。
<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
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();
    }
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;
}