您正在查看 Google Photos Library API 的旧版文档。
Java 客户端库使用入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
要开始通过 Java 客户端库使用 Google Photos Library API,您需要
在开发环境中设置客户端库。
在此之前,请先启用 API 来配置您的项目
通过 Google API 控制台设置 OAuth 2.0 客户端 ID
您的应用会代表
Google 相册用户。例如,如果您在用户的
Google 相册媒体库或将媒体内容上传到用户的媒体库
Google 相册账号,用户便可通过
OAuth 2.0 协议。
OAuth 2.0 客户端 ID 可让您的应用用户登录账号、进行身份验证
从而使用 Library API。Library API 不支持
服务账号;要使用此 API,用户必须登录到有效的 Google
账号。
启用 API
您必须先为项目启用 Library API,然后才能使用该 API。
- 转到 Google API 控制台。
- 从菜单栏中选择一个项目或创建一个新项目。
- 要打开 Google API 库,请在导航菜单中选择
API 和服务 >媒体库。
- 搜索“Google Photos Library API”。选择正确的结果并点击
启用。
请求 OAuth 2.0 客户端 ID
请按照以下步骤申请 OAuth 客户端 ID 并为其配置 OAuth 客户端 ID
应用。本示例使用的应用将整个 OAuth 流程
由服务器端处理,如我们的示例中所示设置流程可能会有所不同
适用于其他实现场景。
- 转到 Google API 控制台
并选择您的项目。
- 在菜单中选择 API 和服务 >凭据。
- 在凭据页面上,点击创建凭据 >OAuth 客户端 ID。
- 选择您的应用类型。在此示例中,应用类型是
Web 应用。
注册允许您的应用从哪些来源访问 Google API
如下所示:
- 如需识别客户端 ID,请输入名称。
在已获授权的 JavaScript 来源字段中,输入您
应用。此字段不允许使用通配符。
您可以输入多个源,以便让您的应用在不同平台上运行
协议、网域或子网域您输入的网址可用于
发起 OAuth 请求。
以下示例显示了一个本地开发网址(我们的示例使用
localhost:8080
)和一个正式版网址。
http://localhost:8080
https://myproductionurl.example.com
已获授权的重定向 URI 字段是接收
OAuth 2.0 服务器发出的响应请求通常情况下,这包括您的
开发环境,并指向应用中的路径。
http://localhost:8080/auth/google/callback
https://myproductionurl.example.com/auth/google/callback
点击创建。
-
在随即显示的 OAuth 客户端对话框中,下载 JSON 文件
包含您的客户端配置客户详细信息包括
以下:
此 JSON 文件稍后将用于设置
适用于 Java 的 Google Auth 库,可以与此客户端库结合使用。
在启动访问 Library API 的公共应用之前,
您的应用必须经过 Google 审核。“未经验证的应用”消息出现在
测试应用的界面,直到应用
经过验证。
设置客户端库
Java 客户端库会为您处理所有后端 API 调用,并公开
对象,包括一些常见 API 任务的代码示例。
首先,下载并安装适用于 Java 的 Google Photos Library API 客户端库
与 GitHub 中的依赖项搭配使用。
然后,设置适用于 Java 的 OAuth2 凭据。
下载选项
您可以通过以下几种方式下载客户端库:
Gradle 依赖项:
要将此库与 Gradle 结合使用,请将以下依赖项添加到您的
build.gradle
文件。
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.photos.library:google-photos-library-client:1.7.3'
}
Maven 依赖项:
如需将此库与 Maven 搭配使用,请将以下内容添加到您的 Maven pom.xml
中。
文件。
<dependency>
<groupId>com.google.photos.library</groupId>
<artifactId>google-photos-library-client</artifactId>
<version>1.7.3</version>
</dependency>
下载版本:
版本页面
包含每个库版本的不同工件,包括 jar 文件。
克隆代码库:
如果您想更改此库或为此库做贡献,请使用此方法,
提交拉取请求,或者试用我们的示例。当您
克隆代码库,系统就会下载此代码库中的所有文件。
- 在以下位置运行
git clone https://github.com/google/java-photoslibrary.git
:
命令提示符。
- 您会获得一个
java-photoslibrary
目录。运行以下命令即可转到该标签页
cd java-photoslibrary
。
- 在 IDE 中打开
build.gradle
文件,或在以下位置运行 ./gradlew assemble
:
命令提示符以构建项目。查看 ./gradlew tasks
即可查看
任务。
设置 Java 版 OAuth2 凭据
此客户端库可与 Google Auth 库结合使用,
Java。如需更多信息
请参阅结合使用 OAuth 2.0 和 Google API 客户端库
Java。
在以下情况下,请在 CredentialsProvider
中指定您的客户端 OAuth 配置:
为 PhotosLibraryClient
对象创建 PhotoLibrarySettings
。
试用一些示例
请尝试使用以下代码,使用 Java 客户端库进行首次 API 调用。
// Set up the Photos Library Client that interacts with the API
PhotosLibrarySettings settings =
PhotosLibrarySettings.newBuilder()
.setCredentialsProvider(
FixedCredentialsProvider.create(/* Add credentials here. */))
.build();
try (PhotosLibraryClient photosLibraryClient =
PhotosLibraryClient.initialize(settings)) {
// Create a new Album with at title
Album createdAlbum = photosLibraryClient.createAlbum("My Album");
// Get some properties from the album, such as its ID and product URL
String id = album.getId();
String url = album.getProductUrl();
} catch (ApiException e) {
// Error during album creation
}
更多示例
供您试用。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[[["\u003cp\u003eThe Google Photos Library API allows your application to interact with Google Photos on behalf of a user, requiring user authorization through OAuth 2.0.\u003c/p\u003e\n"],["\u003cp\u003eBefore using the API, you must enable it in the Google API Console and obtain an OAuth 2.0 client ID for your application.\u003c/p\u003e\n"],["\u003cp\u003eThe Java client library simplifies API interaction, and you can install it using Gradle, Maven, or by downloading a release from GitHub.\u003c/p\u003e\n"],["\u003cp\u003eTo authenticate your application, set up OAuth2 credentials using the Google Auth Library for Java and specify your client configuration in the \u003ccode\u003eCredentialsProvider\u003c/code\u003e.\u003c/p\u003e\n"]]],["To use the Google Photos Library API with Java, first enable the API in the Google API Console and create an OAuth 2.0 client ID, configuring authorized JavaScript origins and redirect URIs. Next, download the Java client library using Gradle, Maven, a direct download, or by cloning the GitHub repository. Finally, set up OAuth2 credentials using the Google Auth Library for Java, providing these in the `PhotosLibrarySettings` when initializing the `PhotosLibraryClient`. Then try out some samples.\n"],null,["# Get started with Java client library\n\nTo start using the Google Photos Library API with the Java client library, you will need to\n[set up the client library](#get-library) in your development environment.\nBefore you do that, [configure your project](#configure-app) by enabling the API\nvia the Google API Console and setting up an OAuth 2.0 client ID.\n\nYour application interacts with Google Photos on behalf of a\nGoogle Photos user. For instance, when you create albums in a user's\nGoogle Photos library or upload media items to a user's\nGoogle Photos account, the user authorizes these API requests via the\n[OAuth 2.0](/identity/protocols/OAuth2) protocol.\n\nThe OAuth 2.0 client ID allows your application users to sign in, authenticate,\nand thereby use the Library API. The Library API does not support\nservice accounts; to use this API, users must be signed in to a valid Google\nAccount.\n\nConfigure your app\n------------------\n\n### Enable the API\n\nBefore you can use the Library API, you must enable it for your project.\n\n1. Go to the [Google API Console](https://console.developers.google.com/apis/library).\n2. From the menu bar, select a project or create a new project.\n3. To open the Google API Library, from the Navigation menu, select **APIs \\& Services \\\u003e Library**.\n4. Search for \"Google Photos Library API\". Select the correct result and click **Enable**.\n\n### Request an OAuth 2.0 client ID\n\nFollow the steps below to request an OAuth client ID and configure it for your\napplication. This example uses an application where the entire OAuth flow is\nhandled server-side, such as the one in our samples. The setup process may vary\nfor other [implementation scenarios](/identity/protocols/OAuth2).\n\n1. Go to the [Google API Console](https://console.developers.google.com/apis/library) and select your project.\n2. From the menu, select **APIs \\& Services \\\u003e Credentials**.\n3. On the **Credentials** page, click **Create Credentials \\\u003e OAuth client ID**.\n4. Select your **Application type** . In this example, the application type is **Web application**.\n5. Register the origins from which your app is allowed to access the Google APIs\n as follows:\n\n 1. To identify the client ID, enter a name.\n 2. In the **Authorized JavaScript origins** field, enter the origin for your\n app. This field doesn't allow wildcards.\n\n You can enter multiple origins to allow your app to run on different\n protocols, domains, or subdomains. The URLs you enter are allowed to\n start an OAuth request.\n\n The following example shows a local development URL (our samples use\n `localhost:8080`) and a production URL. \n\n http://localhost:8080\n https://myproductionurl.example.com\n\n 3. The **Authorized redirect URI** field is the endpoint that receives\n responses from the OAuth 2.0 server. Typically, this includes your\n development environment and points to a path in your application.\n\n http://localhost:8080/auth/google/callback\n https://myproductionurl.example.com/auth/google/callback\n\n 4. Click **Create**.\n\n\u003c!-- --\u003e\n\n6. From the resulting OAuth client dialog, download the JSON file\n containing your client configuration. You client details consist of the\n following:\n\n - Client ID\n - Client secret\n\n This JSON file will be used later to [set up](#get-library)\n the Google Auth library for Java which works with this client library.\n\nBefore you can launch a public application that accesses the Library API,\nyour app must be reviewed by Google. An \"Unverified app\" message appears on the\nscreen when you test your application, until it is\n[verified](https://support.google.com/cloud/answer/7454865).\n\nSet up the client library\n-------------------------\n\nThe Java client library handles all the backend API calls for you, and exposes\nfriendly objects to work with, including code samples for some common API tasks.\nFirstly, download and install the Google Photos Library API client library for Java along\nwith the dependencies from [GitHub](https://github.com/google/java-photoslibrary).\nThen, set up your OAuth2 credentials for Java.\n\n### Download options\n\nHere are some options to download the client library:\n\n- **Gradle dependency:**\n\n To use this library with Gradle, add the following dependency to your\n `build.gradle` file. \n\n repositories {\n mavenCentral()\n }\n dependencies {\n compile 'com.google.photos.library:google-photos-library-client:1.7.3'\n }\n\n- **Maven dependency:**\n\n To use this library with Maven, add the following to your Maven `pom.xml`\n file. \n\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.photos.library\u003c/groupId\u003e\n \u003cartifactId\u003egoogle-photos-library-client\u003c/artifactId\u003e\n \u003cversion\u003e1.7.3\u003c/version\u003e\n \u003c/dependency\u003e\n\n- **Download a release:**\n\n The [releases page](https://github.com/google/java-photoslibrary/releases)\n contains different artifacts for each library release, including jar files.\n- **Clone the repository:**\n\n Use this method if you want to alter or contribute to this library, for\n example, submitting pull requests, or if you wish to try our samples. When you\n clone the repository, **all** files in this repository will be downloaded.\n 1. Run `git clone https://github.com/google/java-photoslibrary.git` at the command prompt.\n 2. You'll get a `java-photoslibrary` directory. Navigate to it by running `cd java-photoslibrary`.\n 3. Open the `build.gradle` file in your IDE or run `./gradlew assemble` at the command prompt to build the project. See `./gradlew tasks` to see available tasks.\n\n### Set up your OAuth2 credentials for Java\n\nThis client library works with the [Google Auth Library for\nJava](https://github.com/google/google-auth-library-java). For more information,\nrefer to [Using OAuth 2.0 with the Google API Client Library for\nJava](https://developers.google.com/api-client-library/java/google-api-java-client/oauth2).\n\nSpecify your client OAuth configuration in the `CredentialsProvider` when\ncreating the `PhotoLibrarySettings` for a `PhotosLibraryClient` object.\n\nTry out some samples\n--------------------\n\nTry the code below to make your first API call using the Java client library. \n\n // Set up the Photos Library Client that interacts with the API\n PhotosLibrarySettings settings =\n PhotosLibrarySettings.newBuilder()\n .setCredentialsProvider(\n FixedCredentialsProvider.create(/* Add credentials here. */)) \n .build();\n\n try (PhotosLibraryClient photosLibraryClient =\n PhotosLibraryClient.initialize(settings)) {\n\n // Create a new Album with at title\n Album createdAlbum = photosLibraryClient.createAlbum(\"My Album\");\n\n // Get some properties from the album, such as its ID and product URL\n String id = album.getId();\n String url = album.getProductUrl();\n\n } catch (ApiException e) {\n // Error during album creation\n }\n\nThere are more [samples](https://github.com/google/java-photoslibrary/tree/master/sample)\non GitHub for you to try."]]