Google Android 驱动器 api 在安装的版本上登录失败

Google Android Drive api sign in fails on installed version

我开发了一个 android 应用程序,它使用 GoogleDrive api、

在调试或运行调试调试版本时,应用程序 工作正常,并正确验证附加的 google 帐户等。 当我构建发布版本时,(使用我的签名密钥),以及 安装 apk 文件,当我 运行, Googleapi客户端无法 "connect",使用相同的 google 在调试下工作的帐户, 在它尝试一次解决后给我一条 "Sign in Failed" 消息。

解决方案在这里https://developers.google.com/drive/android/get-started

我需要为 我的应用程序的发布版本,使用 sha1 密钥 来自我的发布密钥。

首先需要启动一个intent获取登录凭证

    if(mGoogleApiClient == null){
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .enableAutoManage(this, this)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(AppIndex.API)
        .build();
    }
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, DRIVE_SIGNIN_RESULT);

此处无需连接mGoogleApiClient 您可以根据在 onActivityResult

中收到的意向数据创建 GoogleSignInResult 对象
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case DRIVE_SIGNIN_RESULT:
                GoogleSignInResult signInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                handleSignInResult(signInResult);
                break;
        }
    }

立即连接 mGoogleApiClient

    private void handleSignInResult(GoogleSignInResult result) {
        if (result.isSuccess()) {
            GoogleSignInAccount account = result.getSignInAccount();
            mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
        }
    }