【问题标题】:Google Drive REST API Resumable upload returnin 400 Bad RequestGoogle Drive REST API 可恢复上传返回 400 错误请求
【发布时间】:2018-02-13 05:20:42
【问题描述】:

我正在尝试使用 REST API v3 在 Google Drive 中上传 256 KB 的块。我可以成功获取上传 ID,但是当我使用此上传 ID 上传块时,我收到 400 Bad Request 而不是 308。我在下面发布代码。 getUploadID() 方法启动了一个可恢复的上传会话,而 putFileWithUploadID() 方法应该上传文件的一部分,但其中似乎存在问题。我已经按照官方guide写了代码。

String mUploadID;
private String getUploadID(Uri fileUri, String token) {
    String upload_id = "";
    java.io.File fileContent = new java.io.File(fileUri.getPath());
    String fileName = fileContent.getName();
    String mimeType = "audio/mpeg";
    try {
        String url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable";
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setRequestProperty("Authorization", "Bearer " + token);
        con.setRequestProperty("X-Upload-Content-Type", mimeType);
        con.setRequestProperty("X-Upload-Content-Length", String.format(Locale.ENGLISH, "%d", fileContent.length()));
        con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        String body = "{\"name\": \"" + fileName + "\", \"parents\": [\"" + "0B7ypsm4HGZhCS3FXcldPZnFPNkE" + "\"]}";
        con.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", body.getBytes().length));
        OutputStream outputStream = con.getOutputStream();
        outputStream.write(body.getBytes());
        outputStream.close();
        con.connect();
        String location = con.getHeaderField("Location");
        if (location.contains("upload_id")) {
            String[] uploadParameters = location.split("upload_id");
            upload_id = uploadParameters[1].replace("=", "");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return upload_id;
}

private void putFileWithUploadID(Uri fileUri, String token, long range) {
    java.io.File fileContent = new java.io.File(fileUri.getPath());
    String fileName = fileContent.getName();
    String contentLength = String.valueOf(fileContent.length());
    String mimeType = "audio/mpeg";
    long totalBytesFromDataInputStream = 0;
    long uploadedBytes = 0;
    long chunkStart = 0;
    long chunkSize = 262144;
    do {
        try {
            String url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=" + mUploadID;
            URL obj = new URL(url);
            HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
            con.setRequestMethod("PUT");
            con.setDoOutput(true);
            con.setConnectTimeout(10000);
            con.setRequestProperty("Content-Type", mimeType);
            uploadedBytes = chunkSize;
            if (chunkStart + uploadedBytes > fileContent.length()) {
                uploadedBytes = (int) fileContent.length() - chunkStart;
            }
            con.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", uploadedBytes));
            con.setRequestProperty("Content-Range", "bytes " + chunkStart + "-" + (chunkStart + uploadedBytes - 1) + "/" + fileContent.length());
            byte[] buffer = new byte[(int) uploadedBytes];
            FileInputStream fileInputStream = new FileInputStream(fileContent);
            fileInputStream.getChannel().position(chunkStart);
            if (fileInputStream.read(buffer, 0, (int) uploadedBytes) == -1) {
                break;
            }
            fileInputStream.close();
            OutputStream outputStream = con.getOutputStream();
            outputStream.write(buffer);
            outputStream.close();
            con.connect();
            int responseCode = con.getResponseCode();
            String rangeHeader = con.getHeaderField("Range");
            if (rangeHeader!=null) {
                chunkStart = Long.parseLong(rangeHeader.substring(rangeHeader.lastIndexOf("-") + 1, rangeHeader.length())) + 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } while ((chunkStart+chunkSize)<fileContent.length());
}

【问题讨论】:

    标签: java android rest file-upload google-drive-api


    【解决方案1】:

    基本上,400: Bad Request 表示未提供必填字段或参数,提供的值无效,或提供的字段组合无效。

    尝试将重复的父项添加到云端硬盘项目时可能会引发此错误。尝试添加会在目录图中创建循环的父级时也可能会抛出它。

    您也可以查看此related SO post,该related SO post 建议正确使用Android 特定API 进行可恢复上传。见creating files

    【讨论】:

    • 我没有使用 Android 特定的 API,因为它没有提供获取上传进度的方法,并且在我上面的代码中,我不知道我没有提供什么或提供了无效的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 2012-08-21
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多