POST到Google API计算引擎可提供400 [英] POST to google apis compute engine gives 400
问题描述
我正在构建一个小型flask应用程序,以处理正在处理的另一个项目的自动部署.
I am building a small flask app to handle automatic deployment for another project I am working on.
这涉及使用googleapis设置gcloud防火墙规则.
This involves setting a gcloud firewall rule using googleapis.
我已遵循以下文档.
https://cloud.google.com/compute/docs/参考/最新/防火墙/插入
当我以以下方式进行POST呼叫时.
When I make my POST call in the following manner.
headers = {
'Authorization': 'Bearer {}'.format(access_token)
}
name = unique_identifier + "-rule"
payload = {
"kind": "compute#firewall",
"name": name,
"sourceRanges": [
"0.0.0.0/0"
],
"sourceTags": [
unique_identifier
],
"allowed": [
{
"IPProtocol": "tcp",
"ports": [
port_number
]
}
]
}
data = json.dumps(payload)
r = requests.post("https://www.googleapis.com/compute/v1/projects/apollo-rocket-chat/global/firewalls?key={MY_API_KEY}", data=data, headers=headers)
其中port_number和unique_idenifier是字符串.使用我设置的服务帐户检索access_token.我相信令牌是好的,因为我可以使用令牌对受保护的资源进行GET调用.
where port_number and unique_idenifier are strings. access_token is retrieved using a service account I have set. I am confident that the token is good, since I can make a GET call to a protected resource using the token.
我正在使用python 3.5.
I am using python 3.5.
对此POST的响应如下.
The response to this POST is the following.
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required field 'resource' not specified"
}
],
"code": 400,
"message": "Required field 'resource' not specified"
}
}
该错误消息声称我缺少一个字段,尽管我具有在下面的链接中指定的所有必填字段.
The error message is claiming that I am missing a field, although I have all the required fields that are specified at the link below.
https://cloud.google.com/compute/docs/reference/latest/firewalls/insert#request-body
我到底在做什么错?
推荐答案
弄清楚了.我删除了.
Figured it out. I removed.
data = json.dumps(payload)
并更改
r = requests.post("https://www.googleapis.com/compute/v1/projects/apollo-rocket-chat/global/firewalls?key={MY_API_KEY}", data=data, headers=headers)
到
r = requests.post("https://www.googleapis.com/compute/v1/projects/apollo-rocket-chat/global/firewalls?key={MY_API_KEY}", json=data, headers=headers)
这篇关于POST到Google API计算引擎可提供400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!