Python /带有多个身份验证令牌的请求POST

时间:2019-04-29 22:52:02

标签: python-3.x api python-requests

我正在尝试对多个帐户进行一次POST。我有每个帐户的身份验证令牌的列表,但似乎无法让程序循环遍历令牌以发布每个帐户。

我已经尝试过使用列表作为不记名令牌,但是它根本不向任何帐户发布任何内容。我不确定如何使会话多次运行。


token = "875152GTmm6ReRLZZJjq1cXdw2nCSMSln9Rl6dsak9Kz4oMqVecXzXCcdnPTz29zYOGO4GewawDftSc.bz-VQ459HFz9rH80l71oqhyblIi6yp8NzXZ.iEYV5zNbT8.hd"
token2 = "WzQSLogJvtZfGFX5uzgC.8aK80uqlWIb.o0MReU8wJvo-Y3INsM3DYD86bkjqynKqrdlDZLBsK9N64WC2kPNkziIPoCCMQh4ahp4.JVUz4jkfLwNgLVf9pwG2KtRDWHr"
token3 = "fV6FpCU2U70pySDimYcPZC2NpEhxfCEjJVh5XjhbQ7pBqYZRwyY2NGU3.VVd8N-clTQAqJjOa19h7zkEQ3oitTKkyvpJR8zGoBxWgdbCNuVcwwxwrnBaz-gO6Zht4Xxh"

token_list = [token2, token3]


s = requests.Session()
s.headers.update({
  "Authorization": "Bearer %s" % token_list,
  "Content-Type": "application/json"
})

payloadt1 = {
    "title": "NPS/Marketing Example",
    "from_template_id": "293"
}
payloadt2 = {
    "title": "HR - Exit Interview",
    "from_template_id": "359"
}
payloadt3 = {
    "title": "Market Research - Product Testing",
    "from_template_id": "319"
}
payloadt4 = {
    "title": "New Hire Training (Quiz)",
    "from_template_id": "1234"
}

url = "https://api.surveymonkey.com/v3/surveys"
s.post(url, json=payloadt1)
s.post(url, json=payloadt2)
s.post(url, json=payloadt3)
s.post(url, json=payloadt4)

I get no error from running the program, but no templates appear in the accounts.

2 个答案:

答案 0 :(得分:0)

由于requests.Session()包含cookie和其他身份验证元素,因此您应该为使用的每个帐户创建一个单独的会话,例如:

s1 = requests.Session()
s2 = requests.Session()
s3 = requests.Session()

希望这会有所帮助

答案 1 :(得分:0)

最终只是一个简单的解决方法:

              "fV6FpCU2U70pySDimYcPZC2NpEhxfCEjJVh5XjhbQ7pBqYZRwyY2NGU3.VVd8N-clTQAqJjOa19h7zkEQ3oitTKkyvpJR8zGoBxWgdbCNuVcwwxwrnBaz-gO6Zht4Xxh"]

for token in token_list:
    s = requests.Session()
    s.headers.update({
        "Authorization": "Bearer %s" % token,
        "Content-Type": "application/json"
    })