使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Google API 客户端(例如 Python)
以下是一些使用 Google API 客户端库的示例。这些示例
是用 Python 编写的,但在其他编程语言中可能类似,
例如 PHP
要使用 Google API 客户端,您同时需要
API 密钥和服务
请参阅 AdWords 帮助中心的“权限”部分
快速入门
requirements.txt:
google-api-python-client>=2.98.0
insert.py:
from googleapiclient.discovery import build
from google.oauth2 import service_account
# This must be a valid service json document
SERVICE_ACCOUNT_FILE="/.../google.....json"
SCOPES=["https://www.googleapis.com/auth/content"]
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
url="https://css.googleapis.com/$discovery/rest?version=v1"
# This must be a valid API key
key="..."
with build(serviceName= 'css', version= 'v1', discoveryServiceUrl=url,
developerKey=key,
credentials=credentials) as service:
# Add more parameters
# Use your CSS domain ID
request = service.accounts().cssProductInputs().insert(parent="accounts/1234567")
response = request.execute()
print(f"{response}")
list.py:
# Code from above
with build(serviceName= 'css', version= 'v1', discoveryServiceUrl=url,
developerKey=key,
credentials=credentials) as service:
# Use your CSS domain ID
request = service.accounts().cssProducts().list(parent="accounts/1234567")
response = request.execute()
print(f"---\nResponse: {response}")
# Use your CSS domain ID
# the id is built using your country/language (it in this example) and the
# id you gave when uploading the item.
request = service.accounts().cssProducts().get(name="accounts/1234567/cssProducts/it~IT~myproductid")
response = request.execute()
print(f"---\nResponse: {response}")
```language=python
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eThis documentation provides Python examples of using the Google API Client library for interactions with Google services.\u003c/p\u003e\n"],["\u003cp\u003eUsers need an API key and a service account document for authentication, as detailed in the linked quickstart guide.\u003c/p\u003e\n"],["\u003cp\u003eCode snippets illustrate how to build a service object using credentials and an API key to perform actions like inserting and listing data with Google services.\u003c/p\u003e\n"],["\u003cp\u003eThe provided \u003ccode\u003einsert.py\u003c/code\u003e and \u003ccode\u003elist.py\u003c/code\u003e examples demonstrate creating, executing, and printing responses from Google API requests.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples may be adapted for other programming languages with similar concepts, as noted with the PHP example.\u003c/p\u003e\n"]]],[],null,["Google API Client (e.g. Python)\n-------------------------------\n\nHere are some samples which use the Google API client library. These examples\nare written in python, but they may be similar in other programming languages,\nsuch as PHP.\n\nFor using the Google API clients, you will need both an\n[API key](https://cloud.google.com/docs/authentication/api-keys) and a service\naccount document, as described in the [Permissions section in\nquickstart](/comparison-shopping-services/api/guides/quickstart)\n\nrequirements.txt:\n\n`google-api-python-client\u003e=2.98.0`\n\ninsert.py: \n\n from googleapiclient.discovery import build\n from google.oauth2 import service_account\n\n # This must be a valid service json document\n SERVICE_ACCOUNT_FILE=\"/.../google.....json\"\n SCOPES=[\"https://www.googleapis.com/auth/content\"]\n\n credentials = service_account.Credentials.from_service_account_file(\n SERVICE_ACCOUNT_FILE, scopes=SCOPES)\n\n url=\"https://css.googleapis.com/$discovery/rest?version=v1\"\n # This must be a valid API key\n key=\"...\"\n\n with build(serviceName= 'css', version= 'v1', discoveryServiceUrl=url,\n developerKey=key,\n credentials=credentials) as service:\n # Add more parameters\n # Use your CSS domain ID\n request = service.accounts().cssProductInputs().insert(parent=\"accounts/1234567\")\n response = request.execute()\n print(f\"{response}\")\n\nlist.py: \n\n # Code from above\n with build(serviceName= 'css', version= 'v1', discoveryServiceUrl=url,\n developerKey=key,\n credentials=credentials) as service:\n # Use your CSS domain ID\n request = service.accounts().cssProducts().list(parent=\"accounts/1234567\")\n response = request.execute()\n print(f\"---\\nResponse: {response}\")\n\n # Use your CSS domain ID\n # the id is built using your country/language (it in this example) and the\n # id you gave when uploading the item.\n request = service.accounts().cssProducts().get(name=\"accounts/1234567/cssProducts/it~IT~myproductid\")\n response = request.execute()\n print(f\"---\\nResponse: {response}\")\n ```language=python"]]