【发布时间】:2018-05-22 15:20:06
【问题描述】:
我想从 Google 相册中获取我的相册列表。 我正在使用新的 REST API。
我编写了执行 GET 请求的代码:
GET
https://photoslibrary.googleapis.com/v1/albums
根据官方指南:https://developers.google.com/photos/library/guides/list
并且此代码仅返回状态为 200 的响应,但没有 json 正文:
列表:
public static void main(String[] args) throws IOException, GeneralSecurityException, ServiceException, ParseException {
GoogleCredential credential = createCredential();
if (!credential.refreshToken()) {
throw new RuntimeException("Failed OAuth to refresh the token");
}
System.out.println(credential.getAccessToken());
doGetRequest(credential.getAccessToken(), "https://photoslibrary.googleapis.com/v1/albums");
}
private static GoogleCredential createCredential() {
try {
return new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(emailAccount)
.setServiceAccountPrivateKeyFromP12File(ENCRYPTED_FILE)
.setServiceAccountScopes(SCOPE)
.setServiceAccountUser(emailAccount)
.build();
} catch (Exception e) {
throw new RuntimeException("Error while creating Google credential");
}
}
private static void doGetRequest(String accessToken, String url) throws IOException, ParseException {
logger.debug("doGetRequest, with params: url: {}, access token: {}", accessToken, url);
HttpGet get = new HttpGet(url);
get.addHeader("Authorization",
"Bearer" + " " + accessToken);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(get);
String json = EntityUtils.toString(response.getEntity());
System.out.println(json);
}
我还尝试使用其他 REST 客户端(例如 Postman),我收到的结果相同:
{}
【问题讨论】:
-
我不确定您的代码是否有问题,因为 my code 可以正常返回 GPhotos 中的用户相册列表
标签: java rest google-photos