[英]How to invoke Google Drive REST API with Jersey?
I am trying to use Java Jersey instead of Google client libraries to access the Google File API, but I keep getting returned a response status of "401 Unauthorized". 我正在尝试使用Java Jersey而不是Google客户端库来访问Google File API,但我一直收到“401 Unauthorized”的响应状态。 Prior to invoking the call, I have obtained an access token from Google, using Oauth:
在调用呼叫之前,我已经使用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;
}
What am I doing wrong ? 我究竟做错了什么 ?
Turns out you need to add your access token to the http request header. 事实证明,您需要将访问令牌添加到http请求标头中。
In the Google developer's guide: "uploading a file" 在Google开发人员指南中:“上传文件”
https://developers.google.com/drive/manage-uploads https://developers.google.com/drive/manage-uploads
You need to send a post request like this: 你需要发送一个这样的帖子请求:
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
note that in the header you need to put a your_auth_token 请注意,在标题中,您需要输入your_auth_token
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.