java - 如何使用新的 Google Photos API 获取用户相册列表

标签 java rest google-photos

我想从 Google 相册中获取我的相册列表。 我正在使用新的 REST API。

我写了执行 GET 请求的代码:

GET
https://photoslibrary.googleapis.com/v1/albums

根据官方指南:https://developers.google.com/photos/library/guides/list
并且此代码仅返回状态为 200 的响应,但没有 json 主体:

list :

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),我收到的结果相同:

{}

最佳答案

看起来您正在使用服务帐户访问 API。 Service accounts Google Photos Library API 不支持。

您需要为 Web 应用程序 设置 OAuth 2.0,如 here 所述:

  • 转到 Google Developers console并打开你的项目
  • 转到 Credentials Page从菜单
  • 点击创建凭据> OAuth 客户端 ID
  • 将应用程序类型设置为Web 应用程序 并填写表格。还要为您的应用程序指定一个重定向 URI,以接收来自 OAuth 请求的回调和您的应用程序的 URI

然后,您将使用此页面上返回的 client Idclient secret 作为请求的一部分。如果您需要离线访问,这意味着当用户不在浏览器中时访问,您还可以请求 offline access_type 并使用 refresh tokens 以保持访问权限。

看起来您正在使用 Google API Java 客户端,它也支持此流程。通过调用 setClientSecrets(..) 在构建器上设置客户端 secret 像这样:

 return new GoogleCredential.Builder()
                .setTransport(HTTP_TRANSPORT)
                .setJsonFactory(JSON_FACTORY)
                .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
                .build();

您还需要在您的应用程序中处理来自 OAuth 请求的回调,您将在开发者控制台中配置的回调 URL 收到访问 token 。

documentation for the client library 中还有一个更完整的示例.

关于java - 如何使用新的 Google Photos API 获取用户相册列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50471240/

相关文章:

google-photos - 从 Google Photos API 下载全分辨率视频

android - 启动相机拍照或录制视频

Java EE 安全性 - 使用哪种方法?

java - MySQL now 方法与时间戳

java - REST - 下载用户列表

python - 使用 REST API 获取 Azure 事件中心指标?

android - 从新的 Google+(加号)照片应用程序下载图片

java - ArrayList 矩形

java - 如何在java中的不同类中使用一个对象值

python - 我需要通过 FastAPI 发送文件和数据。我正在尝试发送请求但无法正常工作