rocket
隆重推出
Merchant API - Content API for Shopping 的正式替代方案。
update
获取最新资讯,了解 Merchant API 的新功能、问题修复和更新。
add_alert
注意:Content API for Shopping 将于 2026 年 8 月 18 日停用。
服务账号
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
服务账号是一种特殊的 Google 账号,可供应用通过 OAuth 2.0 以编程方式访问 Google API。服务账号使用 OAuth 2.0 流程,无需人工授权。而是改为使用只有您的应用可以访问的密钥文件。本指南讨论如何使用服务账号访问 Content API for Shopping。
注意:使用服务账号进行身份验证的应用只能访问您自己的 Merchant Center 账号。如果您要编写一种第三方应用,而此应用需要访问客户的 Merchant Center 账号,请参阅向请求授权指南。
前提条件
使用服务账号访问 Content API for Shopping 的步骤
- 生成服务账号凭据或访问您已生成的公共凭据。您需要创建 OAuth 2.0 客户端 ID 并获取 *.json 私钥文件:
- 转到 Google API 控制台。
- 在页面顶部的下拉菜单中选择一个项目。如果您尚未创建任何项目,请点击新建项目创建一个。
- 如果您还没有为此项目启用 Content API for Shopping,请在 Google API 列表中搜索并启用它。
- 在左侧边栏中,选择凭据。
您可能需要点击左上角的左箭头才能看到此信息。
- 如需设置服务账号,请选择 CREATE CREDENTIALS,然后选择 Service account。
- 为新服务账号命名。这也用作服务账号 ID 的默认用户名。记录服务账号 ID(包括“@”字符后面的部分),以供日后使用。点击创建,然后完成向项目和用户授予访问权限的可选步骤,然后点击完成。
选择服务账号的角色不会影响对 Content API 进行的调用,因为对 Content API 方法的访问是由与 Merchant Center 中的服务账号 ID 相关联的角色决定的。如果您不确定要选择什么,则只需选择 Project > Viewer。
- 您将转到“服务账号”页面,在该页面中可以访问您的新服务账号。
- 要获取 JSON 私钥,请点击服务账号,然后点击密钥标签页。点击添加密钥 > 创建新密钥,然后选择 JSON 作为密钥类型。点击创建以将私钥下载到您的计算机。
- 将新服务账号作为用户添加到您的 Merchant Center 账号中。如果您是第三方开发者,则需要将您的客户端设置为自动执行此步骤。
- 前往您的 Merchant Center 账号。
- 前往 Merchant Center 账号设置中的账号访问权限。
- 点击 + 添加用户,然后将服务账号 ID 用作新用户的电子邮件地址。
如果您之前未记下服务账号 ID,请转到服务账号管理页面,然后选择您创建的项目。
- 为服务账号指定用户访问权限和电子邮件通知偏好设置。请注意,使用
Accounts
服务需要管理员访问权限。
- 点击添加用户以保存偏好设置,然后返回用户列表。其中应列出具有所选用户角色的服务账号 ID。
- 对您要添加的其他所有服务账号重复执行上述流程。
- 现在,您可以使用服务账号访问 Merchant Center 账号,方法是使用 Google 应用默认凭据流程或直接使用服务账号流程。Content API for Shopping 示例显示如何采用每种受支持的编程语言来使用两个流获取服务账号凭据。请查看代码示例以试用新服务账号,并了解在您自己的代码中使用服务账号需要做哪些更改。
常见问题解答
- 我可以使用服务账号登录 Merchant Center 界面吗?
- 不可以。服务账号不是常规 Google 账号,也无法访问 Merchant Center 界面。
- 我需要多久刷新一次服务账号访问令牌?
- 访问令牌在 Google OAuth 2.0 授权服务器签发一小时后过期。当访问令牌到期时,应用应使用客户端库来提取另一个访问令牌。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[[["\u003cp\u003eService accounts enable applications to programmatically access Google APIs, including the Content API for Shopping, using OAuth 2.0 without human intervention.\u003c/p\u003e\n"],["\u003cp\u003eTo utilize service accounts, you need to generate service account credentials, add the service account as a user in your Merchant Center account, and then access your Merchant Center account using either the Google Application Default Credentials flow or the service account flow.\u003c/p\u003e\n"],["\u003cp\u003eApplications authenticated with service accounts can only access your own Merchant Center account; for third-party applications needing access to client accounts, refer to the Authorizing Requests guide.\u003c/p\u003e\n"],["\u003cp\u003eService accounts cannot be used to sign in to the Merchant Center UI and access tokens expire after one hour, requiring the application to fetch a new one using the client library.\u003c/p\u003e\n"]]],["Service accounts allow applications to access the Content API for Shopping programmatically. Key actions include: generating service account credentials by creating an OAuth 2.0 Client ID and a JSON private key file via the Google API Console, and adding the service account as a user within the Merchant Center account, specifying access and notification preferences. To access the Merchant center, you can use Google Application Default Credentials or a service account flow, with example code available. Access tokens expire hourly, requiring renewal.\n"],null,["# Service accounts are special Google accounts that can be used by\napplications to access Google APIs programmatically via OAuth 2.0. A service\naccount uses an OAuth 2.0 flow that does not require human authorization.\nInstead, it uses a key file that only your application can access. This guide\ndiscusses how to access the Content API for Shopping with service accounts.\n\n**Note:** Applications using service accounts\nfor authentication can only access your own Merchant Center account. If you are\nwriting a third-party application that needs access to your clients' Merchant\nCenter accounts, please see the [Authorizing Requests](/shopping-content/guides/how-tos/authorizing)\nguide instead.\n\nPrerequisites\n-------------\n\n- A [Merchant Center](//merchants.google.com/) account.\n\nSteps to using a service account to access the Content API for Shopping\n-----------------------------------------------------------------------\n\n1. Generate service account credentials or access the public credentials you've already generated. You will need to create an OAuth 2.0 Client ID and obtain a \\*.json private key file:\n 1. Go to the [Google API Console](https://console.cloud.google.com/apis/library).\n 2. Select a project in the drop-down menu at the top of the page. If you do not have one yet, create one by clicking **NEW PROJECT**.\n 3. If you have not already enabled the Content API for Shopping for this project, then search for it in the list of Google APIs and enable it.\n 4. In the sidebar on the left, select **Credentials**. You may need to click the left-pointing arrow at the top left to see this.\n 5. To set up a service account, select **CREATE CREDENTIALS** , and then **Service account** . \n 6. Name the new service account. This also serves as the default username for the service account ID. Record the service account ID, including the part after the '@' character, for later use. Click **CREATE** , then go through the optional steps of granting access to project and users, and click **DONE** .\n\n The choice of role for the service account will not\n have any effect on what calls can be made to the Content API, as access to\n Content API methods is determined instead by the role associated with the\n service account ID in Merchant Center. If you are unsure what to pick, just\n pick **Project \\\u003e Viewer**.\n 7. You will be directed to the Service Accounts page where your new service account can be accessed.\n 8. To obtain a JSON private key, click on the service account, then the **KEYS** tab. Click **ADD KEY \\\u003e Create new key** and then select JSON as the key type. Click **CREATE** to download the private key to your computer.\n2. Add the new service account as a user to your Merchant Center account. If you are a third-party developer, you will need to have your client do this step for you.\n 1. Go to your [Merchant\n Center account](//merchants.google.com/).\n 2. Go to **Account Access** in the settings of your Merchant Center account.\n 3. Click **+ Add user** and use the service account ID as the email address for the new user.\n\n If you did not take note of the\n service account ID earlier, go to the [Service\n Accounts](//console.developers.google.com/iam-admin/serviceaccounts/) administration page and select the project you created.\n 4. Specify the **User access** and **Email notifications** preferences for the service account. Note that the use of the `Accounts` service requires **Admin** access.\n 5. Click **Add user** to save preferences and be returned to the list of users. The service account ID should be listed with the chosen user role.\n 6. Repeat the process for all other service accounts you want to add.\n3. Now you can access your Merchant Center account using the service account either by using the [Google Application\n Default Credentials](/identity/protocols/application-default-credentials) flow or by using the [service account flow](/identity/protocols/OAuth2ServiceAccount) directly. The Content API for Shopping [Samples](/shopping-content/guides/libraries) show how to use both flows for service account credentials in each supported programming language. Please check out the code samples to try out your new service account and to learn what changes you will need to use service accounts in your own code.\n\nFrequently asked questions\n--------------------------\n\nCan I sign in to the Merchant Center UI with my service account?\n: No, service accounts are not regular Google accounts and cannot access the\n Merchant Center UI.\n\nHow often do I need to refresh service account access tokens?\n: Access tokens expire one hour after they are issued by the Google OAuth 2.0\n Authorization Server. When an access token expires, the application should use\n the client library to fetch another access token."]]