谷歌驱动SDK 2.0抛出错误400错误请求 [英] Google drive SDK 2.0 throws error 400 Bad Request

查看:238
本文介绍了谷歌驱动SDK 2.0抛出错误400错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在争取与谷歌云端硬盘API为Android超过50小时,还没有来一英寸更近。从我的理解,也有1001的方式访问谷歌驱动器(谷歌文档API,REST和放大器;谷歌驱动器SDK V2)。我使用谷歌驱动器的SDK V2。我想想要访问谷歌云端硬盘上传JPEG文件。平台的Andr​​oid 2.2 +

我已经试过:

我的code:

 帐户的帐户= AccountManager.get(上下文).getAccountsByType(
        com.google)[0];字符串标记;
尝试{
    令牌= GoogleAuthUtil.getToken(背景下,account.name的oauth2:
            + DriveScopes.DRIVE_FILE);
}赶上(UserRecoverableAuthException E){
    context.startActivityForResult(e.getIntent(),ASK_PERMISSION);
    返回;
}赶上(IOException异常五){
    返回;
}赶上(GoogleAuthException E){
    返回;
}HttpTransport httpTransport =新NetHttpTransport();
JacksonFactory jsonFactory =新JacksonFactory();
Drive.Builder B =新Drive.Builder(httpTransport,jsonFactory,NULL);
最后弦乐tokenCopy =记号。
b.setJsonHtt prequestInitializer(新JsonHtt prequestInitializer(){
    公共无效初始化(JsonHtt prequest要求)抛出IOException
        DriveRequest driveRequest =(DriveRequest)请求;
        driveRequest.set prettyPrint(真);
        driveRequest
                .setKey(1234567890-abcdefghij123klmnop.apps.googleusercontent.com);
        driveRequest.setOauthToken(tokenCopy);
    }
});最终的驱动器驱动器= b.build();
文件清单文件;
尝试{
    文件= drive.files()名单()setQ(mime类型= text / plain的)的execute()。;
}赶上(IOException异常五){
    e.printStackTrace(); //抛出HTTP 400
}

我得到的错误是:

  com.google.api.client.googleapis.json.GoogleJsonResponseException:400错误的请求
{
  code:400,
  错误:[{
    域:全局,
    位置:Q,
    的locationType:参数,
    消息:无效值
    原因:无效
  }],
  消息:无效值
}


解决方案

由于错误信息显示,你的错误是在查询参数。为你的参数的正确语法是

 文件= drive.files()名单()setQ(mime类型=text / plain的')执行();

而不是:

 文件= drive.files()名单()setQ(mime类型= text / plain的)的execute()。;

看你的code,你完全验证,你的要求是因为这个语法错误而失败。

I've been fighting with Google Drive API for Android for more than 50 hours now, and have not come one inch closer. From my understanding, there are 1001 ways to access Google drive (Google Docs API, REST & Google Drive SDK v2). I'm using Google Drive SDK v2. I want want to access Google Drive to upload jpeg files. Platform, Android 2.2+.

What I've tried:

  • Using the recently released SDK: http://code.google.com/p/google-api-java-client/wiki/APIs#Drive_API

  • I've watched the Google I/O sesssion, but the most important part (how to create a Drive object using your Client ID & Client Secret) was left out: https://developers.google.com/events/io/sessions/gooio2012/705/

  • I have created multiple keys on https://code.google.com/apis/console. The last one I've created (and tested with) was created using "Create another client ID..." -> "Installed Application" -> "Android". I've used the key in the ~/.android/debug.keystore.

  • I've also tried to create a key for an "Other" (instead of Android/iOS) installed app, but this gives me a Client ID and Client secret. It seems like the Drive object does not accept a client secret.

  • Where the code says "1234567890-abcdefghij123klmnop.apps.googleusercontent.com", I've tried to use both "API key" and the "Client ID", both gave the same error.

My code:

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
}

The error I'm getting is:

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"
}

解决方案

As the error message suggests, your error is in the query parameter q. The correct syntax for your q parameter is

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

and not :

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

Looking at your code, you are fully authenticated and your request is failing because of this syntax error.

这篇关于谷歌驱动SDK 2.0抛出错误400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆