首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.execute().update(...).execute()方法仅在用户名为双字节字符时才返回"400 Bad Request“

.execute().update(...).execute()方法仅在用户名为双字节字符时才返回"400 Bad Request“
EN

Stack Overflow用户
提问于 2013-07-01 16:35:59
回答 1查看 883关注 0票数 0

我正在使用Java的Google Drive SDK

从6月27日或28日开始,只有在Drive.files().update(...).execute()帐户的用户名是像汉字(日文字符)这样的double-byte字符的情况下,谷歌才会返回400 Bad Request

但是Drive.files().insert(...).execute()运行得很好。

在谷歌账户的用户名是ASCII字符的情况下,更新和插入都可以正常工作。

Google Drive API Service中有没有bug?或者我的代码出了什么问题?

以下是异常的StackTrace。

代码语言:javascript
复制
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
Bad Request
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:111)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:38)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:423)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
    at drivetest.Sample.main(Sample.java:71)

代码

代码语言:javascript
复制
package drivetest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;

public class Sample {
    private static String CLIENT_ID = "CLIENT_ID";
    private static String CLIENT_SECRET = "CLIENT_SECRET";

    private static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

    public static void main(String[] args) throws IOException {
        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
                .setAccessType("online")
                .setApprovalPrompt("auto").build();

        String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
        System.out.println("Please open the following URL in your browser then type the authorization code:");
        System.out.println("  " + url);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String code = br.readLine();

        GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
        GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);

        // Create a new authorized API client
        Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();

        // Insert a file
        File body = new File();
        body.setTitle("Mydocument");
        body.setDescription("A test document");
        body.setMimeType("text/plain");

        java.io.File fileContent = new java.io.File("document.txt");
        FileContent mediaContent = new FileContent("text/plain", fileContent);

        File file = service.files().insert(body, mediaContent).execute();
        System.out.println("File ID: " + file.getId());

        String text = "update!";
        ByteArrayContent overwrite = new ByteArrayContent("text/plain", text.getBytes());

        File over = service.files().update(file.getId(), file, overwrite).execute();
        System.out.println("File ID: " + over.getId());
    }
}

POM

代码语言:javascript
复制
<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-drive</artifactId>
    <version>v2-rev82-1.15.0-rc</version>
</dependency>
<dependency>
    <groupId>com.google.http-client</groupId>
    <artifactId>google-http-client-jackson</artifactId>
    <version>1.15.0-rc</version>
</dependency>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-02 15:10:33

我也遇到过这个问题,我想它可能是个bug。经过我的测试,我发现当我创建一个新文件传递给update()方法的第二个参数时,它再次正常工作,你可以尝试一下,祝你好运!

代码语言:javascript
复制
File newFile = new File().setTitle(file.getTitle()).setMimeType(file.getMimeType()).setParents(file.getParents());
Files.Update updateOperation = mDriveService.files().update(file.getId(), newFile, fileContent);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17400300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档