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

时间:2015-02-23 23:22:51

标签: python oauth tumblr

我有Python 3,我试图通过API link to API documentation发布到Tumblr。尽管感觉我正在做正确的事情,但我仍然收到错误401。 Python 2中有一个官方的API客户端,但感觉有点难以理解,所有其他提到的似乎都是PHP或Java。我也不确定在401错误之后发布的格式,因为文档没有提供除/ 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是否被破坏了?

编辑:这是打印(repr())

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

这是在尝试新代码和使用带有JSON的Steven方法后发生的事情。

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

1 个答案:

答案 0 :(得分:1)

而不是这样做:

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的评论更新我的帖子。您的代码适用于Python 3.4,requests == 2.5.2和requests-oauthlib == 0.4.2。

API.json(编辑密钥/秘密):

{
    "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的?我点击了#34;探索API&#34;在Applications developer page上。
  • 您不需要致电readAPI.close(),因为with屏幕会自动为您关闭文件(请参阅official docs)。
  • 我使用JSON存储和阅读凭据,这样我就100%确定我获得了正确的字符串。代码也更清晰。我仍然怀疑你从文件中读取行并切片的方式。
  • 尝试以与我相同的方式在代码中打印r.content。它是否为您提供了比#34; 401 Unauthorized&#34;?
  • 更具描述性的错误消息