新的驱动器Api连接和授权不适用于Android

时间:2014-03-17 18:39:37

标签: android google-drive-api google-authentication google-drive-android-api

我按照以下步骤在我的应用程序中登录云端硬盘(Google Drive Android API): https://developers.google.com/drive/android/auth

我使用debug.keystore生成SHA1指纹。当我在我的设备上运行应用程序时,第一次连接失败,但hasResolution为true,并出现帐户选择对话框。问题是在我选择一个帐户之前立即调用onActivityResult方法,而resultCode当然不是OK。

private GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //some other code...

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

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

@Override
public void onConnected(Bundle bundle) {
    Log.d(TAG, "DRIVE: connected");
    CONNECTED = true;
}

@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG, "DRIVE: disconnected");
    CONNECTED = false;
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d(TAG, "DRIVE: connection failed");
    CONNECTED = false;

    if (connectionResult.hasResolution()) {
        try {
            connectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (IntentSender.SendIntentException e) {
            //mGoogleApiClient.connect(); whit or whitout is the same...
        }
    } else {
        GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show();
    }
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data){
    switch (requestCode) {
        case REQUEST_CODE_RESOLUTION:
            if (resultCode == RESULT_OK) {
                mGoogleApiClient.connect();
            }
            break;
        case REQUEST_CODE_CREATOR:
            if(resultCode != RESULT_OK)
                newNotification("Upload Fallito", "...", android.R.drawable.ic_dialog_alert);
            break;

    }
}

1 个答案:

答案 0 :(得分:0)

由于没时间分析这里的错误,我建议你抓住code I use for testing here并试一试。不漂亮,但紧凑和功能。授权迷宫在其README中被触及。但我不知道你的环境,所以它可能没用。