Google Drive每次登录都会失败

时间:2016-05-19 13:19:30

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

我正在尝试在Google上登录用户并尝试从Google云端硬盘上传/下载文件,这是我登录的活动

            <h2>
               {{ctrl.rejection.status}} {{ctrl.rejection.statusText}}
            </h2>
            <h3 class="error-details">
                Sorry, an error has occurred!
            </h3>
            <h3 class="error-details">
                {{ctrl.errorMessage}}
            </h3>

我唯一想到的是这个prompt一次又一次地选择帐户。 这是在logcat中打印的错误:

import android.content.Intent;
import android.content.IntentSender;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

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

public class google extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google);

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

    mGoogleApiClient.connect();
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch (requestCode) {
        case 3:
            if (resultCode == RESULT_OK) {
                mGoogleApiClient.connect();
            }
            break;
    }
}

@Override
protected void onResume() {
    super.onResume();
    if (mGoogleApiClient == null) {

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

    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i("fail", "GoogleApiClient connection failed: " + result.toString());
    if (!result.hasResolution()) {
        GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
        return;
    }

    try {
        result.startResolutionForResult(this, 3);
    } catch (IntentSender.SendIntentException e) {
        Log.e("ing", "Exception while starting resolution activity", e);
    }
}

@Override
public void onConnected(Bundle connectionHint) {
    Log.i("conn", "API client connected.");
}

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

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}
}

我确定我在活动结果或onconnectionfailed上做错了,但我无法弄清楚在哪里以及为什么......

2 个答案:

答案 0 :(得分:1)

这第三方图书馆可以提供帮助吗?它可以很好地处理身份验证,甚至可以将其他提供程序与某些代码行集成在一起。

// CloudStorage cs = new Box(context, "[clientIdentifier]", "[clientSecret]");
// CloudStorage cs = new OneDrive(context, "[clientIdentifier]", "[clientSecret]");
// CloudStorage cs = new GoogleDrive(context, "[clientIdentifier]", "[clientSecret]");
CloudStorage cs = new Dropbox(context, "[clientIdentifier]", "[clientSecret]");
new Thread() {
    @Override
    public void run() {
        cs.createFolder("/TestFolder"); // <---
        InputStream stream = null;
        try {
            AssetManager assetManager = getAssets();
            stream = assetManager.open("UserData.csv");
            long size = assetManager.openFd("UserData.csv").getLength();
            cs.upload("/TestFolder/Data.csv", stream, size); // <---
        } catch (Exception e) {
            // TODO: handle error
        } finally {
            // TODO: close stream
        }
    }
}.start();

你可以从GitHub获得它: https://github.com/CloudRail/cloudrail-si-android-sdk

答案 1 :(得分:0)

您是否尝试过关注Connecting and Authorizing the Google Drive Android API

的Google文档
  

Google Drive Android API的授权由GoogleApiClient处理。这通常是在活动的onCreate()方法中创建的。

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

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


After you create the client, you must connect it for authorization to occur.

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

尝试使用此onCreate代码而不是onResume将其实现为onStart,而不再仅仅调用mGoogleApiClient.connect();包含范围。

该文档还提供了错误处理程序,可以帮助您验证实现是否正确。

  

注意:Google Drive Android API目前仅支持drive.file和drive.appfolder授权范围。如果您的应用需要Drive Android API中尚未提供的其他权限或功能,则必须使用Google API Java客户端。

我希望这可以帮助您解决问题。