Bạn đang xem tài liệu cũ về API Thư viện Google Photos.
Bắt đầu sử dụng thư viện ứng dụng Java
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Để bắt đầu sử dụng API Thư viện Google Photos bằng thư viện ứng dụng Java, bạn cần phải
thiết lập thư viện ứng dụng trong môi trường phát triển của bạn.
Trước khi bạn làm việc đó, hãy định cấu hình dự án của mình bằng cách bật API
qua Bảng điều khiển API của Google và thiết lập mã ứng dụng khách OAuth 2.0.
Ứng dụng của bạn tương tác với Google Photos thay mặt cho
Người dùng Google Photos. Ví dụ: khi bạn tạo anbom trong
Thư viện Google Photos hoặc tải các mục nội dung nghe nhìn lên
Google Photos, người dùng cho phép các yêu cầu API này thông qua
Giao thức OAuth 2.0.
Mã ứng dụng khách OAuth 2.0 cho phép người dùng ứng dụng của bạn đăng nhập, xác thực
từ đó sử dụng Library API. API thư viện không hỗ trợ
tài khoản dịch vụ; Để sử dụng API này, người dùng phải đăng nhập vào
Tài khoản.
Bật API
Bạn phải bật API Thư viện cho dự án thì mới có thể sử dụng API này.
- Chuyển đến Google API Console.
- Trên thanh trình đơn, hãy chọn một dự án hoặc tạo dự án mới.
- Để mở Thư viện API của Google, từ trình đơn Điều hướng, hãy chọn
API và Dịch vụ > Thư viện.
- Tìm "Google Photos Library API" (API Thư viện Google Photos). Chọn kết quả chính xác và nhấp vào
Bật.
Yêu cầu mã ứng dụng khách OAuth 2.0
Hãy làm theo các bước bên dưới để yêu cầu mã ứng dụng khách OAuth và định cấu hình mã đó cho
. Ví dụ này sử dụng một ứng dụng trong đó toàn bộ quy trình OAuth là
đã xử lý ở phía máy chủ, chẳng hạn như
một trong các mẫu của chúng tôi. Quá trình thiết lập có thể khác nhau
cho trường hợp triển khai khác.
- Truy cập Google API Console
rồi chọn dự án của bạn.
- Trên trình đơn, hãy chọn API và Dịch vụ > Thông tin xác thực.
- Trên trang Thông tin đăng nhập, nhấp vào Tạo thông tin xác thực > Mã ứng dụng OAuth.
- Chọn Loại ứng dụng. Trong ví dụ này, loại ứng dụng là
Ứng dụng web.
Đăng ký nguồn gốc mà từ đó ứng dụng của bạn được phép truy cập vào các API của Google
như sau:
- Hãy nhập một tên để xác định mã ứng dụng khách.
Trong trường Nguồn gốc JavaScript được cho phép, hãy nhập nguồn gốc cho
. Trường này không cho phép sử dụng ký tự đại diện.
Bạn có thể nhập nhiều nguồn gốc để cho phép ứng dụng chạy trên nhiều nguồn gốc
giao thức, miền hoặc miền con. URL mà bạn nhập được phép
bắt đầu một yêu cầu OAuth.
Ví dụ sau đây thể hiện URL phát triển cục bộ (các mẫu của chúng tôi sử dụng
localhost:8080
) và một URL phát hành chính thức.
http://localhost:8080
https://myproductionurl.example.com
Trường URI chuyển hướng được phép là điểm cuối nhận
phản hồi từ máy chủ OAuth 2.0. Thông thường, trong đó có
môi trường phát triển và trỏ tới một đường dẫn trong ứng dụng của bạn.
http://localhost:8080/auth/google/callback
https://myproductionurl.example.com/auth/google/callback
Nhấp vào Tạo.
-
Trong hộp thoại ứng dụng OAuth thu được, hãy tải tệp JSON xuống
chứa cấu hình ứng dụng của bạn. Thông tin chi tiết về khách hàng của bạn bao gồm
sau:
- Client ID
- Mật khẩu ứng dụng khách
Tệp JSON này sẽ được dùng sau để thiết lập
thư viện Xác thực của Google cho Java hoạt động với thư viện ứng dụng này.
Trước khi bạn có thể khởi chạy một ứng dụng công khai có truy cập vào API Thư viện,
ứng dụng của bạn phải được Google xem xét. "Ứng dụng chưa được xác minh" xuất hiện trên
khi kiểm thử ứng dụng cho đến khi ứng dụng
xác minh.
Thiết lập thư viện ứng dụng
Thư viện ứng dụng Java xử lý tất cả các lệnh gọi API phụ trợ cho bạn và hiển thị
các đối tượng dễ sử dụng, bao gồm cả mã mẫu cho một số tác vụ API phổ biến.
Trước tiên, hãy tải xuống và cài đặt thư viện ứng dụng API Thư viện Google Photos cho Java cùng
với các phần phụ thuộc trên GitHub.
Sau đó, hãy thiết lập thông tin đăng nhập OAuth2 cho Java.
Tùy chọn tải xuống
Dưới đây là một số tuỳ chọn để tải thư viện ứng dụng xuống:
Phần phụ thuộc Gradle:
Để sử dụng thư viện này với Gradle, hãy thêm phần phụ thuộc sau vào
Tệp build.gradle
.
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.photos.library:google-photos-library-client:1.7.3'
}
Phần phụ thuộc Maven:
Để sử dụng thư viện này với Maven, hãy thêm nội dung sau đây vào pom.xml
Maven
.
<dependency>
<groupId>com.google.photos.library</groupId>
<artifactId>google-photos-library-client</artifactId>
<version>1.7.3</version>
</dependency>
Tải bản phát hành xuống:
Trang bản phát hành
chứa các cấu phần phần mềm khác nhau cho mỗi bản phát hành thư viện, bao gồm cả các tệp jar.
Nhân bản kho lưu trữ:
Sử dụng phương thức này nếu bạn muốn thay đổi hoặc đóng góp cho thư viện này, cho
ví dụ như gửi yêu cầu lấy dữ liệu hoặc nếu bạn muốn thử các mẫu của chúng tôi. Khi
sao chép kho lưu trữ, thì tất cả tệp trong kho lưu trữ này sẽ được tải xuống.
- Chạy
git clone https://github.com/google/java-photoslibrary.git
ở
dấu nhắc lệnh.
- Bạn sẽ nhận được một thư mục
java-photoslibrary
. Chuyển đến đó bằng cách chạy
cd java-photoslibrary
.
- Mở tệp
build.gradle
trong IDE hoặc chạy ./gradlew assemble
tại
lời nhắc lệnh để tạo dự án. Truy cập ./gradlew tasks
để xem
các nhiệm vụ hiện có.
Thiết lập thông tin đăng nhập OAuth2 cho Java
Thư viện ứng dụng này hoạt động với Thư viện xác thực của Google cho
Java. Để biết thêm thông tin,
tham khảo bài viết Sử dụng OAuth 2.0 với Thư viện ứng dụng API của Google để
Java.
Chỉ định cấu hình OAuth của ứng dụng trong CredentialsProvider
khi
tạo PhotoLibrarySettings
cho đối tượng PhotosLibraryClient
.
Thử một số mẫu
Hãy thử mã bên dưới để thực hiện lệnh gọi API đầu tiên bằng cách sử dụng thư viện ứng dụng Java.
// 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
}
Có thêm mẫu
trên GitHub để bạn dùng thử.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-29 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-29 UTC."],[[["\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."]]