GoogleApiClient连接失败:ConnectionResult{statusCode=SIGN_IN_REQUIRED,

5

我正在尝试将Google Drive集成到我的应用程序中,但成功率很低。使用Quick Start存储库中的代码,每次选择用户帐户登录时,我都会收到以下错误消息:

I/DriveDemo: GoogleApiClient connection failed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{867e070: android.os.BinderProxy@13cdfe9}, message=null}

我已经按照以下链接的指示操作:

  1. https://developers.google.com/drive/android/get-started
  2. https://github.com/googledrive/android-quickstart

同时我还观看了以下视频:

  1. https://youtu.be/RezC1XP6jcs

我也查阅了许多关于Stack Overflow的问题和答案,但仍然不知道我做错了什么。

manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lightkeeper54.chuck.drivedemo">

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

代码:

protected void onResume() {
    super.onResume();
    if (mGoogleApiClient == null) {
        // Create the API client and bind it to an instance variable.
        // We use this instance as the callback for connection and connection
        // failures.
        // Since no account name is passed, the user is prompted to choose.
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    // Connect the client. Once connected, the camera is launched.
    mGoogleApiClient.connect();
}

@Override
protected void onPause() {
    if (mGoogleApiClient != null) {
        mGoogleApiClient.disconnect();
    }
    super.onPause();
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch (requestCode) {
        case REQUEST_CODE_CAPTURE_IMAGE:
            // Called after a photo has been taken.
            if (resultCode == Activity.RESULT_OK) {
                // Store the image data as a bitmap for writing later.
                mBitmapToSave = (Bitmap) data.getExtras().get("data");
            }
            break;
        case REQUEST_CODE_CREATOR:
            // Called after a file is saved to Drive.
            if (resultCode == RESULT_OK) {
                Log.i(TAG, "Image successfully saved.");
                mBitmapToSave = null;
                // Just start the camera again for another photo.
                startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
                        REQUEST_CODE_CAPTURE_IMAGE);
            }
            break;
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    // Called whenever the API client fails to connect.
    Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
    if (!result.hasResolution()) {
        // show the localized error dialog.
        GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
        return;
    }
    // The failure has a resolution. Resolve it.
    // Called typically when the app is not yet authorized, and an
    // authorization
    // dialog is displayed to the user.
    try {
        result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (IntentSender.SendIntentException e) {
        Log.e(TAG, "Exception while starting resolution activity", e);
    }
}

@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "API client connected.");
    if (mBitmapToSave == null) {
        // This activity has no UI of its own. Just start the camera.
        startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
                REQUEST_CODE_CAPTURE_IMAGE);
        return;
    }
    saveFileToDrive();
}

@Override
public void onConnectionSuspended(int cause) {
    Log.i(TAG, "GoogleApiClient connection suspended");
}

你在Google API控制台中注册了开发/发布密钥吗? - Cheok Yan Cheng
所有创建和注册密钥的步骤都已按照要求完成。 - lightkeeper
@lightkeeper在开发者控制台中使用您的软件包和SHA密钥创建AuthKey,就可以开始了... - user3809705
请注意,Studio的Instant Run不会创建已签名APK文件......除非您将签名信息放置在应用程序的build.gradle文件中。 - RudyF
2个回答

0

根据文档 - SIGN_IN_REQUIRED

客户端尝试连接服务,但用户未登录。客户端可以选择继续使用API而不进行登录。或者,如果hasResolution()返回true,则客户端可以调用startResolutionForResult(Activity, int)提示用户进行登录。在登录活动以RESULT_OK返回后,进一步的尝试应该成功。

您也可以查看@Cheok的问题,这是一个SO post,建议按要求正确添加您的SHA-1和包名,并在项目凭据全部设置后启用API。

希望这些信息能够帮助到您。


我按照所有步骤创建了密钥并在API控制台中注册了它。API在控制台上显示为活动状态。这就是我卡住的原因。 - lightkeeper

0

我遇到了类似的问题,后来解决了,原因是每个 APK 构建都由相同的密钥库签名,这与我在“获取 Android 证书并注册应用程序”时使用的密钥库相同,详情请参见此处

在使用 Studio 时,我按照“配置构建过程以自动签名您的 APK”的步骤执行了“自动签名”,详情请参见此处。显然,要使用与注册应用程序时使用的密钥库相同的密钥库。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接