Bạn đang xem tài liệu cũ về API Thư viện Google Photos.
Làm quen với REST
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, hãy định cấu hình dự án bằng cách bật API thông qua
Bảng điều khiển API của Google rồi 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 Các 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 hiện ra, hãy sao chép nội dung sau:
- Client ID
- Mật khẩu ứng dụng khách
Ứng dụng của bạn có thể truy cập vào các API của Google đã bật bằng cách sử dụng các giá trị 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.
Dùng thử mẫu
Bây giờ, bạn đã định cấu hình dự án để sử dụng với API Thư viện,
hãy xem ứng dụng mẫu trên GitHub. Ứng dụng mẫu
minh hoạ cách tạo một khung ảnh thông minh bằng API Thư viện.
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.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-29 UTC."],[[["\u003cp\u003eTo use the Google Photos Library API, you must enable the API and set up an OAuth 2.0 client ID through the Google API Console.\u003c/p\u003e\n"],["\u003cp\u003eUsers must be signed in to a valid Google Account to use the Library API as it requires user authorization via OAuth 2.0 for actions like creating albums or uploading media.\u003c/p\u003e\n"],["\u003cp\u003eBefore launching publicly, your application needs to be reviewed and verified by Google to remove the "Unverified app" message during testing.\u003c/p\u003e\n"]]],["To utilize the Google Photos Library API, first enable it for your project in the Google API Console. Then, create an OAuth 2.0 client ID, specifying the application type, authorized JavaScript origins, and redirect URIs. Obtain the client ID and secret from the resulting dialog. Ensure your application is reviewed by Google before public launch. Note, only valid Google Account users can use this API as service accounts aren't supported.\n"],null,["# Get started with REST\n\nTo start using the Google Photos Library API, configure your project by enabling the API via\nthe 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, copy the following:\n\n - Client ID\n - Client secret\n\n Your app can access the enabled Google APIs using these values.\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\nTry out the sample\n------------------\n\nNow that you've configured your project for use with the Library API,\ncheck out the [sample app](/photos/library/samples) on GitHub. The sample app\ndemonstrates how you can build a smart photo frame using the Library API."]]