0

我正在尝试将 Google Drive 集成到我的 Android 应用程序中,以便我可以将文件存储在用户的 Drive 中。我在 App Console 中启用了 Drive SDK 和 Drive API,并使用我的 SHA1 指纹设置了我的 Oauth 2 ClientID。据我所知,谷歌的一切看起来都很好。

这是初始化我的 Drive 对象的代码。

private boolean initializeService(String accountName) {
    if(service != null) { 
        return true;
    }
    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(mContext, DriveScopes.DRIVE); 
    mAuthIntent = null;

    if(accountName != null) {
        try {
            credential.setSelectedAccountName(accountName); 
            Log.d("DriveHelper", credential.getToken());
            service = getDriveService(credential);
        } catch(Exception ex) {
            if(ex instanceof UserRecoverableAuthException) {
                UserRecoverableAuthException authException = (UserRecoverableAuthException)ex;
                mAuthIntent = authException.getIntent();
                mErrorCode = CloudHelper.CONNECTION_AUTHENTICATE;
                ((Activity)mContext).startActivityForResult(mAuthIntent, REQUEST_AUTHORIZATION);
            } else {
                Log.e("DriveCloudHelper", "Error retrieving auth token", ex);
            }

            return false;
        } 
        getAppFolder();

        return mFileLocation != null;
    } 

    Activity activity = (Activity)mContext;
    activity.startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
    return false;
}

private Drive getDriveService(GoogleAccountCredential credential) {
    return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
    .setApplicationName("Drive Helper")
    .build();
}

这些方法在后台线程上调用并且工作正常,出现帐户选择器,出现授权窗口,一切都很好。我的getAppFolder方法会查找特定文件夹并在没有任何问题的情况下创建它(在同一后台线程上)。

但是,当我将文件上传到云端硬盘时,我在新的后台线程上尝试的第一个操作会导致UserRecoverableAuthException,但是当我调用时出现新的异常,getIntent因为没有提供 Intent ,并且详细消息是AppDownloadRequired.

这是生成异常的方法。

private String getIdFromFolder(String location, String name, boolean create) {
    try {
        String maskedName = name.replaceAll("/", "");
        String query = "title contains '" + maskedName + "' and '" + location + "' in parents";
        FileList list = service.files().list().setMaxResults(1).setQ(query).execute();
        List<File> files = list.getItems();

        if (files.isEmpty()) {
            return create ? createSubFolder(location, name) : null;
        }

        return files.get(0).getId();
    } catch (IOException e) {
        // Eat the exception here
        Log.w("DriveCloudHelper", e);
    }
    return create ? createSubFolder(location, name) : null;
}

这是堆栈跟踪:

 com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:222)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:836)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)
    at net.wishfullthinking.groceryhelper.interfaces.DriveCloudHelper.getIdFromFolder(DriveCloudHelper.java:269)
    at net.wishfullthinking.groceryhelper.interfaces.DriveCloudHelper.uploadDatabaseToCloud(DriveCloudHelper.java:405)
    at net.wishfullthinking.groceryhelper.interfaces.DriveCloudHelper.synchFileToCloud(DriveCloudHelper.java:368)
    at net.wishfullthinking.groceryhelper.interfaces.CloudHelper.mergeCloudStoreCollection(CloudHelper.java:472)
    at net.wishfullthinking.groceryhelper.interfaces.CloudHelper.access$10(CloudHelper.java:454)
    at net.wishfullthinking.groceryhelper.interfaces.CloudHelper$4.run(CloudHelper.java:444)
    at java.lang.Thread.run(Thread.java:856)
 Caused by: com.google.android.gms.auth.UserRecoverableAuthException: AppDownloadRequired
    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:192)
    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:217)
    ... 11 more
4

1 回答 1

2

尝试mContext.getApplicationContext()在您的GoogleAccountCredential.usingOAuth2通话中使用,而不是mContext因为这似乎在其他地方解决了类似问题

于 2013-07-02T17:33:49.260 回答