ไคลเอ็นต์ Google API (เช่น Python)
ต่อไปนี้เป็นตัวอย่างที่ใช้ไลบรารีของไคลเอ็นต์ Google API ตัวอย่างเหล่านี้ เขียนด้วยภาษา Python แต่อาจเหมือนกันในภาษาโปรแกรมอื่นๆ เช่น PHP
สำหรับการใช้ไคลเอ็นต์ Google API คุณจะต้องมีทั้ง คีย์ API และบริการ ดังที่อธิบายไว้ในส่วนสิทธิ์ใน คู่มือเริ่มต้นฉบับย่อ
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