Google云端硬盘REST API可恢复的上传返回400错误请求 [英] Google Drive REST API Resumable upload returnin 400 Bad Request

查看:183
本文介绍了Google云端硬盘REST API可恢复的上传返回400错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用REST API v3在Google云端硬盘上传256 KB的区块。我可以成功获取上传ID,但是当我使用这个上传ID来上传一个块时,我得到了一个400错误的请求,而不是308.我发布了下面的代码。方法getUploadID()启动一个可恢复的上传会话,方法putFileWithUploadID()应该上传一个文件块,但似乎有问题。我已经根据官方的指南编写了代码。

I am trying to uploading chunks of 256 KB in Google Drive using REST API v3. I can successfully get the upload ID but when I use this upload ID to upload a chunk, I get a 400 Bad Request instead of 308. I am posting the code below. The method getUploadID() initiates a resumable upload session and the method putFileWithUploadID() should upload a chunk of the file but there seems to be a problem in it. I have written the code according to the official 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());
}


推荐答案

基本上, 400:错误请求表示未提供必填字段或参数,所提供的值是无效的,或提供的字段的组合是无效的。

Basically, 400: Bad Request means that a required field or parameter has not been provided, the value supplied is invalid, or the combination of provided fields is invalid.


当试图添加一个重复的父一个云端硬盘项目当试图添加一个可以在目录图中创建一个循环的父项时,也会抛出它。

This error can be thrown when trying to add a duplicate parent to a Drive item. It can also be thrown when trying to add a parent that would create a cycle in the directory graph.

您也可以检查<一个href =https://stackoverflow.com/questions/24455422/android-resumable-upload-to-google-drive/24459799#24459799>相关的SO帖子,建议正确使用Android特定的API做可恢复的上传。请参阅创建文件

You may also check this related SO post which suggested to properly use the Android-specific API to do resumable upload. See creating files.

这篇关于Google云端硬盘REST API可恢复的上传返回400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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