簡體   English   中英

如何使用Jersey調用Google Drive REST API?

[英]How to invoke Google Drive REST API with Jersey?

我正在嘗試使用Java Jersey而不是Google客戶端庫來訪問Google File API,但我一直收到“401 Unauthorized”的響應狀態。 在調用呼叫之前,我已經使用Oauth從Google獲得了訪問令牌:

public static String getGoogleFileResource(final String fileId,
        final String accessToken) {
    //projection

    ClientConfig cc = new DefaultClientConfig();
    cc.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
    Client client = Client.create(cc);

    String url = String
            .format("https://www.googleapis.com/drive/v2/files/%s?fields=downloadUrl&key=%s",
                    fileId, GoogleClientConstants.GOOGLE_api_key);
    WebResource webResource = client.resource(url);

    String response = webResource
            .accept(MediaType.APPLICATION_JSON_TYPE,
                    MediaType.APPLICATION_XML_TYPE)
            .header("Authorization", "Bearer " + accessToken)
            .get(String.class);

    logger.info("Authorization - " + "Bearer " + accessToken);
    logger.info(" reponse " + response);
    return response;
}

我究竟做錯了什么 ?

事實證明,您需要將訪問令牌添加到http請求標頭中。

在Google開發人員指南中:“上傳文件”

https://developers.google.com/drive/manage-uploads

你需要發送一個這樣的帖子請求:

POST /upload/drive/v2/files?uploadType=media HTTP/1.1
Host: www.googleapis.com
Content-Type: image/jpeg
Content-Length: number_of_bytes_in_JPEG_file
Authorization: your_auth_token

JPEG data

請注意,在標題中,您需要輸入your_auth_token

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM