【发布时间】:2023-03-06 11:05:01
【问题描述】:
我的 pytumblr 版本是 0.0.6,即 repo 中的版本。导入工作正常。我正在使用 Python 2.7.8,也就是说:
我已登录我的帐户。
我去了https://api.tumblr.com/console
我已经把 consumer_key & consumer_secret 键
我已经允许它并且
我已经复制了这段代码:
client = pytumblr.TumblrRestClient(
'my_consumer_key',
'my_consumer_secret',
'my_access_token',
'my_token_secret'
)
然后我尝试创建一个文本帖子。下一个代码取自 pytumblr github 自述文件页面。我刚刚添加了响应代码。
response = client.create_text("codingjester", state="published", slug="testing-text-posts", title="Testing", body="testing1 2 3 4")
print(response)
但是,这就是它所说的......
{u'meta': {u'status': 401, u'msg': u'Not Authorized'}, u'response': []}
¿ 为什么?
Ps: 像client.followers("blogname") 这样的其他oauth 调用有效,但在我上面所说的尝试发帖时却不行。
编辑:我尝试使用三足 oauth 授权。使用 Selenium 自动执行 http 请求以获取 oauth_verifier,然后通过此获取 oauth_token 和 oauth_token_secret,consumer_key 和 consumer_secret 应该足以使用 pytumblr ... 但我'我仍然收到 401 Not Authorized 响应 :( 哦,我使用“http://localhost/”作为我的 callback_url,否则或仅使用“/”,自动化 url 不会返回 oauth_verifier 密钥
代码如下:
import urlparse
import oauth2 as oauth
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
consumer_key = 'my_consumer_key'
consumer_secret = 'my_consumer_secret'
callback_url = 'http://localhost/'
request_token_url = 'http://www.tumblr.com/oauth/request_token'
access_token_url = 'http://www.tumblr.com/oauth/access_token'
authorize_url = 'http://www.tumblr.com/oauth/authorize'
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
# Step 1: Get a request token. This is a temporary token that is used for
# having the user authorize an access token and to sign the request to obtain
# said access token.
resp, content = client.request(request_token_url, "GET")
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])
request_token = dict(urlparse.parse_qsl(content))
print "Request Token:"
print " - oauth_token = %s" % request_token['oauth_token']
print " - oauth_token_secret = %s" % request_token['oauth_token_secret']
print
# Step 2: HERE's WHAT I HAVE MODIFIED. I USE SELENIUM TO GET THE oauth_verifier
driver = webdriver.Firefox()
driver.get("https://www.tumblr.com/login")
wait1 = WebDriverWait(driver, 10)
u = wait1.until(EC.element_to_be_clickable((By.XPATH, "//input[@type='email']")))
driver.execute_script("arguments[0].value = 'my_username';", u)
p = driver.find_element_by_xpath("//input[@type='password']")
driver.execute_script("arguments[0].value = 'my_password';", p)
p.submit()
time.sleep(10)
driver.get("http://www.tumblr.com/oauth/authorize?oauth_token=" + request_token['oauth_token'])
time.sleep(5)
allow = driver.find_element_by_xpath("(//button)[2]")
driver.execute_script("arguments[0].click();", allow)
time.sleep(5)
a = driver.current_url
a = a.replace(callback_url + '?oauth_token=' + request_token['oauth_token'] + "&oauth_verifier=", "")
a = a.replace("#_=_", "")
print(a)
oauth_verifier = a
# Step 3: Once the consumer has redirected the user back to the oauth_callback
# URL you can request the access token the user has approved. You use the
# request token to sign this request. After this is done you throw away the
# request token and use the access token returned. You should store this
# access token somewhere safe, like a database, for future use.
token = oauth.Token(request_token['oauth_token'],
request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)
resp, content = client.request(access_token_url, "POST")
access_token = dict(urlparse.parse_qsl(content))
print "Access Token:"
print " - oauth_token = %s" % access_token['oauth_token']
print " - oauth_token_secret = %s" % access_token['oauth_token_secret']
print
print "You may now access protected resources using the access tokens above."
print
client = pytumblr.TumblrRestClient(
consumer_key,
consumer_secret,
access_token['oauth_token'],
access_token['oauth_token_secret'],
)
response = client.create_text("codingjester", state="published", slug="testing-text-posts", title="Testing", body="testing1 2 3 4")
print(response)
学习如何使用端点发出纯 oauth 请求可能会更好......并停止使用 pytumblr 包装器......我开始认为它很糟糕,而且这真的是无人维护的库。
【问题讨论】: