3

Android の Google ドライブにファイルをダウンロードしようとしていて、 https://developers.google.com/drive/manage-downloads#examplesのサンプルに従っていました。

if (entry.getDownloadUrl() != null && entry.getDownloadUrl().length() > 0) 
{
    try
    {
        LogUtils.xi(this, "start opening url:", entry.getDownloadUrl());
        /* HttpRequestFactory: Thread-safe light-weight HTTP request factory layer on top of the HTTP */
        HttpResponse resp = manager.getDrive().getRequestFactory()
                        .buildGetRequest(new GenericUrl(entry.getDownloadUrl()))
                        .execute();
        return resp.getContent();
    } 
    catch (IOException e) 
    {
        LogUtils.xe(this, e.toString());
        e.printStackTrace();
        throw ExceptionUtils.getIOException("Can't open inputstream.", e);
    }
}
else 
{
    LogUtils.xw(this, "The file doesn't have any content stored on Drive.");
    return null;
}

しかし、エラー 302 が表示されます。ここで何が起こっているか知っている人はいますか?

03-29 14:56:07.316 E/XXGoogleDrive(25539): [25566][DriveDLFutureTask] [getInputStream]com.google.api.client.http.HttpResponseException: **302 Moved Temporarily**
03-29 14:56:07.326 W/System.err(25539): com.google.api.client.http.HttpResponseException: 302 Moved Temporarily
03-29 14:56:07.326 W/System.err(25539):     at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1062)
4

1 に答える 1

0

「一時的に移動しました」は、ファイル サイズが 25 MB を超えた場合にユーザー認証ページへのリダイレクトまたはウイルス スキャンを試行していたため発生しました

「alt=media」クエリ パラメータでファイル取得リクエストを使用して動作させました。動作コードは以下のとおりです。

private static byte[] getBytes(Drive drive, String accessToken, String fileId, long position, long byteCount) {
        byte[] receivedByteArray = null;
        String downloadUrl = "https://www.googleapis.com/drive/v3/files/" + fileId + "?alt=media&access_token="
                + accessToken;
        if (downloadUrl != null && downloadUrl.length() > 0) {
            try {
                com.google.api.client.http.HttpRequest httpRequestGet = drive.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl));
                httpRequestGet.getHeaders().setRange("bytes=" + position + "-" + (position + byteCount - 1));
                com.google.api.client.http.HttpResponse response = httpRequestGet.execute();
                InputStream is = response.getContent();
                receivedByteArray = IOUtils.toByteArray(is);
                response.disconnect();
                System.out.println("google-http-client-1.18.0-rc response: [" + position + ", " + (position + receivedByteArray.length - 1) + "]");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return receivedByteArray;
    }
于 2017-05-18T11:19:42.253 に答える