Android Google drive SDK 2.0抛出错误400错误请求

Android Google drive SDK 2.0抛出错误400错误请求,android,google-drive-api,Android,Google Drive Api,我已经和Android的谷歌驱动API斗争了50多个小时了,但还没有接近。据我所知,有1001种方式可以访问Google drive(Google Docs API、REST和Google drive SDK v2)。我使用的是谷歌硬盘sdkv2。我想访问Google Drive以上载jpeg文件。平台,安卓2.2+ 我所尝试的: 使用最近发布的SDK: 我看过Google I/O会话,但最重要的部分(如何使用客户端ID和客户端密码创建驱动器对象)被忽略了: 我在上创建了多个关键点。我创建

我已经和Android的谷歌驱动API斗争了50多个小时了,但还没有接近。据我所知,有1001种方式可以访问Google drive(Google Docs API、REST和Google drive SDK v2)。我使用的是谷歌硬盘sdkv2。我想访问Google Drive以上载jpeg文件。平台,安卓2.2+

我所尝试的:

  • 使用最近发布的SDK:

  • 我看过Google I/O会话,但最重要的部分(如何使用客户端ID和客户端密码创建驱动器对象)被忽略了:

  • 我在上创建了多个关键点。我创建(并测试)的最后一个应用程序是使用“创建另一个客户端ID…”->“已安装的应用程序”->“Android”创建的。我使用了~/.android/debug.keystore中的密钥

  • 我还尝试为“其他”(而不是Android/iOS)安装的应用程序创建一个密钥,但这给了我一个客户端ID和客户端密码。驱动器对象似乎不接受客户端机密

  • 代码上写着“1234567890-abcdefghij123klmnop.apps.googleusercontent.com”,我尝试使用“API密钥”和“客户端ID”,两者都给出了相同的错误

我的代码:

Account account = AccountManager.get(context).getAccountsByType(
        "com.google")[0];

String token;
try {
    token = GoogleAuthUtil.getToken(context, account.name, "oauth2:"
            + DriveScopes.DRIVE_FILE);
} catch (UserRecoverableAuthException e) {
    context.startActivityForResult(e.getIntent(), ASK_PERMISSION);
    return;
} catch (IOException e) {
    return;
} catch (GoogleAuthException e) {
    return;
}

HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
final String tokenCopy = token;
b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
    public void initialize(JsonHttpRequest request) throws IOException {
        DriveRequest driveRequest = (DriveRequest) request;
        driveRequest.setPrettyPrint(true);
        driveRequest
                .setKey("1234567890-abcdefghij123klmnop.apps.googleusercontent.com");
        driveRequest.setOauthToken(tokenCopy);
    }
});

final Drive drive = b.build();
FileList files;
try {
    files = drive.files().list().setQ("mimeType=text/plain").execute();
} catch (IOException e) {
    e.printStackTrace(); // throws HTTP 400
}
我得到的错误是:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "location" : "q",
    "locationType" : "parameter",
    "message" : "Invalid Value",
    "reason" : "invalid"
  } ],
  "message" : "Invalid Value"
}

如错误消息所示,您的错误在查询参数
q
中。
q
参数的正确语法为

files = drive.files().list().setQ("mimeType='text/plain'").execute();
而不是:

files = drive.files().list().setQ("mimeType=text/plain").execute();

查看您的代码,您已通过完全身份验证,并且由于此语法错误,您的请求失败。

正如错误消息所示,您的错误在查询参数
q
中。
q
参数的正确语法为

files = drive.files().list().setQ("mimeType='text/plain'").execute();
而不是:

files = drive.files().list().setQ("mimeType=text/plain").execute();

查看您的代码,您已通过完全身份验证,并且由于此语法错误,您的请求失败。

要清楚,您是否试图通过仅基于Web应用程序的API而不是通过您自己的服务器将文件从Android添加到Google Drive?@MorrisonChang Yes,直接从Android添加到Google Drive。我正在使用以下链接中的库。那是唯一的网络应用吗?据我所知,谷歌I/O视频讨论了这个API,不是吗?从Android上传文件的推荐方式是什么?需要澄清的是,您是否试图通过仅基于Web应用程序的API而不是通过您自己的服务器将文件从Android添加到Google Drive?@MorrisonChang是的,直接从Android添加到Google Drive。我正在使用以下链接中的库。那是唯一的网络应用吗?据我所知,谷歌I/O视频讨论了这个API,不是吗?从Android上传文件的推荐方式是什么?