3

我正在尝试使用 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;
}

我究竟做错了什么 ?

4

1 回答 1

0

事实证明,您需要将访问令牌添加到 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

于 2013-08-06T07:05:17.250 回答