【问题标题】:How to get list of the user's albums using new Google Photos API如何使用新的 Google Photos API 获取用户的相册列表
【发布时间】: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


【解决方案1】:

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

您需要为 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(..) 在构建器上设置客户端密码:

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

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

documentation for the client library中还有一个更完整的例子。

【讨论】:

  • 您能否提供一些背景或链接,说明为什么 Google Photos Library API 不支持服务帐户,而其他 Google API 确实支持它们?它们似乎是某些用例的完美解决方案,例如向我网站的匿名访问者提供我最新公开共享的照片列表。我看过的任何其他身份验证方案似乎都过于复杂或完全不可能。谢谢。
猜你喜欢
  • 2021-10-11
  • 2017-01-11
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
相关资源
最近更新 更多