Python 3 请求尝试使用 Tumblr API,我收到错误 401?

Python 3 with Requests trying to use Tumblr API, I get error 401?

我有 Python 3,我正在尝试通过 API link to API documentation post 到 Tumblr。尽管感觉我做的一切都是正确的,但我一直收到错误 401。 Python 2 中有一个官方 API 客户端,但感觉有点难以理解,所有其他提到它的地方似乎都在 PHP 或 Java 中。我也不确定 401 错误后 post 的格式,因为文档没有给出除 /post 之外的任何明确示例。我的代码:

import requests
from requests_oauthlib import OAuth1
#variables for later
client_key=""
client_secret=""
oauth_token=""
oauth_token_secret=""

#gets the values for the variables
with open("API.txt", 'r') as readAPI:
    readAPI.readline()
    client_key=readAPI.readline()[23:]
    client_secret=readAPI.readline()[23:]
    oauth_token=readAPI.readline()[23:]
    oauth_token_secret=readAPI.readline()[23:]
readAPI.close()

#prints them to double check they are being read correctly
print(client_key)
print(client_secret)
print(oauth_token)
print(oauth_token_secret)

#sets oauth for the connection
oauth = OAuth1(client_key,
               client_secret,
               oauth_token,
               oauth_token_secret)

#check post that should return various blog stats
r = requests.get("http://api.tumblr.com/v2/user/info" ,auth=oauth)

print(r)

我 100% 确定我获得了正确的客户端密钥、客户端密码、oauth 令牌和 oauth 令牌密码。我仔细检查过,oauth 令牌都放在手动读取的文件中,并在连接尝试之前打印出来。我 100% 确定它是正确的。我想知道 Tumblr 的 API 是不是坏了?

编辑:这是用 print(repr())

'client_key'
'client_secret'
'oauth_token'
'oauth_token_secret'
{"meta":{"status":401,"msg":"Not Authorized"},"response":[]}

这是在尝试新代码并使用 Steven 的方法 JSON 后发生的情况。

b'{"meta":{"status":401,"msg":"Not Authorized"},"response":[]}'

而不是这样做:

print(client_key)

这个输出是什么?

print(repr(client_key))

您正在使用 readline,它在每行末尾包含一个换行符:

$ cat foo.txt
key
secret
blabla

$ python3.4
>>> f = open("foo.txt")
>>> print(repr(f.readline()))
'key\n'
>>> print(repr(f.readline()))
'secret\n'
>>> print(repr(f.readline()))
'blabla\n'

您是否尝试过去掉每行的换行符?


编辑:根据@user2853325 的评论更新我的post。您的代码适用于 Python 3.4、requests==2.5.2 和 requests-oauthlib==0.4.2.

API.json(编辑了keys/secrets):

{
    "client_key": "XXXXXXXXXXXXXXXXXXXXdG7zXIMcDidwQ5pMHuQTbxyhNINrCE",
    "client_secret": "XXXXXXXXXXXXXXXXXXXX72A5HQO1axydP5nlOWCTQx4ECfXfyX",
    "oauth_token": "XXXXXXXXXXXXXXXXXXXX8WAnqMBWaAdnGhnc4gWhJ4j6cufK1W",
    "oauth_token_secret": "XXXXXXXXXXXXXXXXXXXX8Kf82k65JzIcMU7QUp54ssPEzJd7my"
}

tumblr.py:

import json

import requests
from requests_oauthlib import OAuth1

#gets the values for the variables
with open("API.json") as f:
    credentials = json.load(f)

#prints them to double check they are being read correctly
print(credentials)

#sets oauth for the connection
oauth = OAuth1(
    credentials['client_key'],
    credentials['client_secret'],
    credentials['oauth_token'],
    credentials['oauth_token_secret']
)

#check post that should return various blog stats
r = requests.get("http://api.tumblr.com/v2/user/info", auth=oauth)
print(r)
print(r.content)

输出(编辑了 oauth 内容):

$ bin/python tumblr.py
{'oauth_token_secret': 'XXXXXXXXXXXXXXXXXXXX8Kf82k65JzIcMU7QUp54ssPEzJd7my', 'client_secret': 'XXXXXXXXXXXXXXXXXXXX72A5HQO1axydP5nlOWCTQx4ECfXfyX', 'client_key': 'XXXXXXXXXXXXXXXXXXXXdG7zXIMcDidwQ5pMHuQTbxyhNINrCE', 'oauth_token': 'XXXXXXXXXXXXXXXXXXXX8WAnqMBWaAdnGhnc4gWhJ4j6cufK1W'}
<Response [200]>
b'{"meta":{"status":200,"msg":"OK"},"response":{"user":{"name":"lost-theory","likes":0,"following":2,"default_post_format":"html","blogs":[{"title":"Untitled","name":"lost-theory","posts":0,"url":"http:\/\/lost-theory.tumblr.com\/","updated":0,"description":"","is_nsfw":false,"ask":false,"ask_page_title":"Ask me anything","ask_anon":false,"followed":false,"can_send_fan_mail":true,"share_likes":true,"likes":0,"twitter_enabled":false,"twitter_send":false,"facebook_opengraph_enabled":"N","tweet":"N","facebook":"N","followers":0,"primary":true,"admin":true,"messages":0,"queue":0,"drafts":0,"type":"public"}]}}}'

现在我已经亲自测试了您的代码:

  • 你是怎么得到 oauth_tokenoauth_token_secret 的?我通过单击 Applications developer page 上的 "Explore API" 得到了我的。
  • 您不需要调用 readAPI.close(),因为 with 块会自动为您关闭文件(请参阅 official docs)。
  • 我使用 JSON 来存储和读取凭据,这样我 100% 确定我得到了正确的字符串。代码也更清晰。我仍然怀疑你从文件中读取行并将它们切片的方式。
  • 尝试像我一样在代码中打印 r.content。它给你的错误信息是否比“401 Unauthorized”更具描述性?