我的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,否则或仅使用“/” autorization 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包装器停止...我开始认为它很糟糕,而且这是非常维护的库。
答案 0 :(得分:1)
response = client.create_text("**codingjester**", state="published", slug="testing-text-posts", title="Testing", body="testing1 2 3 4")
但是“codingjester”实际上是你的博客吗?否则未经授权是正确的。
答案 1 :(得分:0)
client.create_text
或client.create_photo
的第一个参数需要成为博客的标识,因此对于照片,请执行以下操作:
client.create_photo('yourblogname', 'stringlinktolocalfileorURL')
这适合我。
答案 2 :(得分:0)
我有同样的问题。我通过硬编码oauth_token和oauth_token_secret(从https://api.tumblr.com/console/calls/user/info获得)而不是使用request_token调用来修复。也许这些调用出现问题的方法可以修复,但与此同时,硬编码可以解决这个问题。