【发布时间】:2018-11-17 00:19:21
【问题描述】:
在过去的两天里,我一直在尝试使用 Python 向 Google Search Console API 发送批处理请求,但我不确定文档以及如何继续。
我不确定要遵循哪些文件。
https://developers.google.com/api-client-library/python/guide/batch
https://developers.google.com/webmaster-tools/v3/how-tos/batch
第一个文件说要使用这种格式:
from apiclient.http import BatchHttpRequest
def insert_animal(request_id, response, exception):
if exception is not None:
# Do something with the exception
pass
else:
# Do something with the response
pass
service = build('farm', 'v2')
batch = service.new_batch_http_request(callback=insert_animal)
batch.add(service.animals().insert(name="sheep"))
batch.add(service.animals().insert(name="pig"))
batch.add(service.animals().insert(name="llama"))
batch.execute(http=http)
但第二个帖子说要这样做:
POST /batch HTTP/1.1
Authorization: Bearer your_auth_token
Host: www.googleapis.com
Content-Type: multipart/mixed; boundary=batch_foobarbaz
Content-Length: total_content_length
--batch_foobarbaz
Content-Type: application/http
Content-ID: <item1:12930812@barnyard.example.com>
GET /farm/v1/animals/pony
--batch_foobarbaz
Content-Type: application/http
Content-ID: <item2:12930812@barnyard.example.com>
PUT /farm/v1/animals/sheep
Content-Type: application/json
Content-Length: part_content_length
If-Match: "etag/sheep"
{
"animalName": "sheep",
"animalAge": "5"
"peltColor": "green",
}
--batch_foobarbaz
Content-Type: application/http
Content-ID: <item3:12930812@barnyard.example.com>
GET /farm/v1/animals
If-None-Match: "etag/animals"
--batch_foobarbaz--
但我不确定最后一个要我处理这段代码吗?
另外,当尝试在 Request 包中发出 Post 请求时,我在哪里可以输入 body 参数?
谢谢。
【问题讨论】:
-
你有什么收获吗?
-
第一个 url 重定向到 github repo。我认为这是批处理文档迁移到 github.com/googleapis/google-api-python-client/blob/master/docs/… 的地方
标签: python google-api