Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Klien Google API (misalnya Python)
Berikut beberapa contoh yang menggunakan library klien Google API. Contoh-contoh ini
ditulis dalam python, tetapi mereka mungkin mirip dalam bahasa
pemrograman lain,
contohnya PHP.
Untuk menggunakan klien Google API, Anda akan memerlukan
API key dan layanan
dokumen akun Google, seperti yang dijelaskan di bagian Izin dalam
panduan memulai
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
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-07-25 UTC.
[null,null,["Terakhir diperbarui pada 2025-07-25 UTC."],[[["\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"]]