1

我正在尝试使用 python 客户端创建一个 VM。我正在打的电话是

import googleapiclient.discovery

compute = googleapiclient.discovery.build('compute', 'v1')
compute.instances().insert(
    project='my-project',
    zone='us-central1-c',
    body=config).execute()

(config 是一个 json 字符串,可在此处获得

响应是

<HttpError 400 when requesting https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/instances?alt=json
returned "Required field 'resource' not specified">

这个论坛帖子这个 stackexchange question看来,问题出在 REST API 标头上。然而,据我所知,标头并未暴露在 python 客户端中。

这是一个错误还是我可能做错了什么?

编辑

错误返回后googleapiclient.http.HttpRequest,看起来HttpRequest生成的对象build()具有标头

{ 'accept': 'application/json',
  'accept-encoding': 'gzip, deflate',
  'content-length': '2299',
  'content-type': 'application/json',
  'user-agent': 'google-api-python-client/1.7.7 (gzip)' }

我尝试添加'resource': 'none'到标题并收到相同的响应。

看了一会儿之后,我怀疑 REST API 期望指定一个 Compute Engine 资源。但是,在官方文档中搜索“资源”一词会产生 546 个结果。

编辑2

创建了 GitHub 问题

4

1 回答 1

0

使用请求正文(requestBody)”而不是资源。

于 2020-08-20T05:34:52.650 回答