您正在查看 Google Photos Library API 的旧版文档。
REST 使用入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
要开始使用 Google Photos Library 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 客户端对话框中,复制以下内容:
您的应用可以使用这些值访问已启用的 Google API。
在启动访问 Library API 的公共应用之前,
您的应用必须经过 Google 审核。“未经验证的应用”消息出现在
测试您的应用时,该屏幕会展示,直到
经过验证。
试用示例
现在,您已将项目配置为使用 Library API,
查看 GitHub 上的示例应用。示例应用
演示了如何使用 Library API 构建智能相框模式。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[[["\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."]]