【问题标题】:GoogleApiClient connection failed: ConnectionResult{statusCode=SIGN_IN_REQUIRED,GoogleApiClient 连接失败:ConnectionResult{statusCode=SIGN_IN_REQUIRED,
【发布时间】:2017-02-11 19:26:16
【问题描述】:

我正在尝试将 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 控制台中注册了您的开发/发布密钥?
  • 已按照所有步骤创建和注册密钥。
  • @lightkeeper 使用您的包和 SHA 密钥在开发者控制台中制作 AuthKey,您就可以开始了...
  • 请记住,Studio 的 Instant Run 不会创建签名的 APK ... 除非您在应用的 build.gradle 中放置了签名信息。

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


【解决方案1】:

基于文档 - SIGN_IN_REQUIRED

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

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

希望这些信息对您有所帮助。

【讨论】:

  • 我按照所有步骤创建密钥并在 API 控制台中注册它。 API 在控制台中显示为活动。这就是我被卡住的原因。
【解决方案2】:

我有一个类似的问题,在我确保每个 APK 构建都由我在执行“获取 Android 证书并注册您的应用程序”时使用的同一密钥库签名后得到解决,如 here 所述

在使用 Studio 时,我按照here 中所述的“配置构建过程以自动签署您的 APK”中描述的步骤执行“自动签名”。显然,使用与注册应用程序时使用的相同的密钥库(上图)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 2014-07-14
    • 2016-04-25
    相关资源
    最近更新 更多