我的代码有问题,我不知道我还能做什么来解决这个问题,或者更确切地说,找出原因。
我已经开始修改Tumblr Python API Pytumblr (https://github.com/tumblr/pytumblr),以支持来自Tumblr (https://www.tumblr.com/docs/npf)的Neue Post格式。所以我想在我的Tumblr账户上发布一张照片草稿。总而言之,它做了它应该做的事情。但它确实发布了两次。它在我的草稿中创建了两次相同的帖子,我不知道为什么。
从这里开始:
tumblr_client.create_photo("my blog name", state="draft", tags=["my", "tags"], format="markdown", content=["URL to my photo"])在另一个模块中,我的内容被创建为JSON对象(content_json),如下所示:
{
"content": [
{
"type": "image",
"media": [
{
"url": "my url"
}
]
}
],
"state": "draft",
"tags": "my,tags",
"format": "markdown"}现在我们进入request模块。
我的OAUTH和HTTP请求头配置:
def __init__(self, consumer_key, consumer_secret="", oauth_token="", oauth_secret="", host="https://api.tumblr.com"):
self.host = host
self.oauth = OAuth1(
consumer_key,
client_secret=consumer_secret,
resource_owner_key=oauth_token,
resource_owner_secret=oauth_secret,
signature_type='auth_header'
)
self.consumer_key = consumer_key
self.headers = {
"User-Agent": "pytumblr/" + self.__version,
"Content-Type": "application/json"
}这是POST函数。
def post(self, content_json, url, params={}, files=[]):
"""
Issues a POST request against the API, allows for multipart data uploads
:param url: a string, the url you are requesting
:param params: a dict, the key-value of all the parameters needed
in the request
:param files: a list, the list of tuples of files
:returns: a dict parsed of the JSON response
"""
url = self.host + url
try:
if files:
return self.post_multipart(content_json, url, params, files)
else:
#data = urllib.parse.urlencode(params)
#if not PY3:
# data = str(data)
resp = requests.post(url, data=content_json, headers=self.headers, auth=self.oauth)
return self.json_parse(resp)
except HTTPError as e:
return self.json_parse(e.response)一旦程序运行到
resp = requests.post(url, data=content_json, headers=self.headers, auth=self.oauth)它做两个草稿帖子。
如果我用Postman发送我的内容,它只做一次发布--就像它应该做的那样。
你能给我一个提示吗?问题可能出在哪里?你需要更多的代码吗?
谢谢您:)
发布于 2020-10-06 04:16:44
我可以自己解决它!:)
我实现了一个请求会话处理,就像这里描述的那样,它完成了这个任务。https://requests.readthedocs.io/en/master/user/advanced/
https://stackoverflow.com/questions/64214511
复制相似问题