在指定帐户中登录时出错。请选择其他帐户

时间:2015-06-02 11:04:39

标签: android google-drive-api

我试图与谷歌驱动器建立连接并将所有文件选择到列表活动中。我已成功使用谷歌教程和解决方案步骤。 为了您更统一的参考,让我告诉您,我主要是按照以下链接: https://github.com/googledrive/android-demos 使用“BaseDemoActivity”和“PickFileWithOpenerActivity”类,应用程序在开发环境中工作得非常好。毕竟运作良好,我已经使应用程序成为一个apk文件。当我试图将该apk运行到另一台设备或同一设备时,它向我提供了一条消息,“在指定帐户中登录时出错。请选择其他帐户。”有时它会提供不同的信息,例如:“Google Play服务的未知问题”。我真的不明白为什么我会收到这样的消息,我犯了哪些错误。但请相信我,在开发环境中一切都很好。在这里,我将为您提供完整的代码,以便您更好地理解。

LIB:

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.Drive;

BaseDemoActivity.class

public abstract class BaseDemoActivity extends Activity implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

private static final String TAG = "BaseDriveActivity";

/**
 * DriveId of an existing folder to be used as a parent folder in
 * folder operations samples.
 */
public static final String EXISTING_FOLDER_ID = "0B2EEtIjPUdX6MERsWlYxN3J6RU0";

/**
 * DriveId of an existing file to be used in file operation samples..
 */
public static final String EXISTING_FILE_ID = "0ByfSjdPVs9MZTHBmMVdSeWxaNTg";

/**
 * Extra for account name.
 */
protected static final String EXTRA_ACCOUNT_NAME = "account_name";

/**
 * Request code for auto Google Play Services error resolution.
 */
protected static final int REQUEST_CODE_RESOLUTION = 1;

/**
 * Next available request code.
 */
protected static final int NEXT_AVAILABLE_REQUEST_CODE = 2;

/**
 * Google API client.
 */
private GoogleApiClient mGoogleApiClient;

/**
 * Called when activity gets visible. A connection to Drive services need to
 * be initiated as soon as the activity is visible. Registers
 * {@code ConnectionCallbacks} and {@code OnConnectionFailedListener} on the
 * activities itself.
 */
@Override
protected void onResume() {
    super.onResume();
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    mGoogleApiClient.connect();
}

/**
 * Handles resolution callbacks.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
        mGoogleApiClient.connect();
    }
}

/**
 * Called when activity gets invisible. Connection to Drive service needs to
 * be disconnected as soon as an activity is invisible.
 */
@Override
protected void onPause() {
    if (mGoogleApiClient != null) {
        mGoogleApiClient.disconnect();
    }
    super.onPause();
}

/**
 * Called when {@code mGoogleApiClient} is connected.
 */
@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "GoogleApiClient connected");
}

/**
 * Called when {@code mGoogleApiClient} is disconnected.
 */
@Override
public void onConnectionSuspended(int cause) {
    Log.i(TAG, "GoogleApiClient connection suspended");
}

/**
 * Called when {@code mGoogleApiClient} is trying to connect but failed.
 * Handle {@code result.getResolution()} if there is a resolution is
 * available.
 */
@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
    if (!result.hasResolution()) {
        // show the localized error dialog.
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
        return;
    }
    try {
        result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (SendIntentException e) {
        Log.e(TAG, "Exception while starting resolution activity", e);
    }
}

/**
 * Shows a toast message.
 */
public void showMessage(String message) {
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}

/**
 * Getter for the {@code GoogleApiClient}.
 */
public GoogleApiClient getGoogleApiClient() {
  return mGoogleApiClient;
 }
}

PickFileWithOpenerActivity.class

public class PickFileWithOpenerActivity extends BaseDemoActivity {

private static final String TAG = "PickFileWithOpenerActivity";

private static final int REQUEST_CODE_OPENER = 1;

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);
    IntentSender intentSender = Drive.DriveApi
            .newOpenFileActivityBuilder()
            .setMimeType(new String[] { "text/plain", "text/html" })
            .build(getGoogleApiClient());
    try {
        startIntentSenderForResult(
                intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0);
    } catch (SendIntentException e) {
      Log.w(TAG, "Unable to send intent", e);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == 1 && data != null){
        switch (requestCode) {
            case REQUEST_CODE_OPENER:
                if (resultCode == RESULT_OK) {
                    DriveId driveId = (DriveId) data.getParcelableExtra(
                            OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
                    showMessage("Selected file's ID: " + driveId);
                }
                finish();
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }
  }
 }

的Manifest.xml

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity android:name="com.example.googledrivetestandroidapplication.HomeActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".PickFileWithOpenerActivity" />
</application>

注意:我维护了[Google Developers Console] [2],项目名称包含我开发的项目名称,包名称为

package="com.example.googledrivetestandroidapplication"
在manifest.xml中的

,在其他信息中我使用了build.gradle。

compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.google.android.gms:play-services:7.5.0'

我也尝试过不同的方式,比如;来自Silvana L的参考。不幸的是,它对我不起作用。我该怎么办?

清除Google Play服务缓存(设置&gt;应用程序管理器&gt; Google Play服务&gt;清除缓存)。 关闭/启用Google Play服务(设置&gt;应用程序管理器&gt; Google Play服务&gt;关闭)。在此之后,您可能需要再次通过Google Play进行更新。 通过手机退出/退出您的Google帐户。

2 个答案:

答案 0 :(得分:1)

我希望我理解你的问题。

请按照步骤在google api控制台上注册应用: -

  1. 在Api控制台GoogleApiConsole
  2. 中创建新项目
  3. API&amp; auth 标签---&gt; 凭证 ---&gt; 创建新的客户ID
  4. 选择已安装的应用并选择 Android
  5. 使用您的签名证书指纹(SHA1)
  6. 在此注册您的应用( com.example.googledrivetestandroidapplication

答案 1 :(得分:1)

您已使用android密钥创建了凭据,您需要使用oauth 2.0创建凭证。

转到GoogleApiConsole,打开项目,然后转到

API&amp; auth tab ---&gt;证书---&gt;添加凭证---&gt; Oauth 2.0客户端ID

enter image description here 然后输入申请信息