在 api 请求中将字符串参数传递给用户输入

Pass string parameter to user input in an api request

我正在使用 python 向 google api 之一发出请求。

我使用的是有效的代码。

from oauth2client.service_account import ServiceAccountCredentials
import httplib2

SCOPES = [ "https://www.googleapis.com/auth/url-api" ]
ENDPOINT = "url-endpoint"

JSON_KEY_FILE = "route json"

credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)

http = credentials.authorize(httplib2.Http())

content = """{
  "url": "domain.com",
  "type": "TYPE-NOTIFICATION"
}"""

response, content = http.request(ENDPOINT, method="POST", body=content)
print(response)

我正在尝试使构建变量“content”的参数作为用户输入工作。

如您所见,现在“content”变量包含一个字符串文字。这导致我无法将这些参数中的每一个转换为输入。

我该如何解决这个问题?我的目标是用户可以输入 url 的值。与“类型”参数相同。

我尝试删除字符串并为值添加输入,但这不起作用。

将用户输入构建到字典中。如下所示。

内容={}

内容["url"] = input("输入URL")

内容["类型"] = input("输入类型")

我猜,body 参数可以作为字典传递。所以你可以将上面的“content”变量直接传递给调用。

如果只有字符串有效,请将变量转换为字符串。

内容=海峡(内容)