首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在创建员额时未授权的Pytumblr

在创建员额时未授权的Pytumblr
EN

Stack Overflow用户
提问于 2015-04-05 11:16:42
回答 3查看 426关注 0票数 0

我的pytumblr版本是0.0.6,是回购中的版本。进口正常工作。我使用的是Python2.7.8,上面写着:

我已经登录了我的帐户。我去了https://api.tumblr.com/console,我放了consumer_key & consumer_secret键,我允许它,我复制了下面的代码:

代码语言:javascript
复制
client = pytumblr.TumblrRestClient(
    'my_consumer_key',
    'my_consumer_secret',
    'my_access_token',
    'my_token_secret'
)

然后我尝试创建一个文本帖子。下一段代码来自pytumblr github自述文件页面。我刚添加了响应代码。

代码语言:javascript
复制
response = client.create_text("codingjester", state="published", slug="testing-text-posts", title="Testing", body="testing1 2 3 4")
print(response)

但是,它是这么说的..。

代码语言:javascript
复制
{u'meta': {u'status': 401, u'msg': u'Not Authorized'}, u'response': []}

,为什么?

Ps:做其他oauth调用(如client.followers("blogname") )是有效的,但当我试图发布时,我已经说过了。

编辑:我尝试使用三条腿的oauth授权。使用Selenium来自动获得oauth_verifier的http请求,然后用它获取oauth_tokenoauth_token_secretconsumer_keyconsumer_secret应该足够使用pytumblr.,但我仍然得到401个未授权的响应:(哦,我使用"http://localhost/“作为我的callback_url,否则或使用"/”自动化url不返回oauth_verifier键

下面是代码:

代码语言:javascript
复制
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包装纸..。我开始觉得它糟透了,那是一个没有维护的图书馆。

EN

回答 3

Stack Overflow用户

发布于 2015-08-10 21:05:58

代码语言:javascript
复制
response = client.create_text("**codingjester**", state="published", slug="testing-text-posts", title="Testing", body="testing1 2 3 4")

但是"codingjester“实际上是你的博客吗?否则,不经授权是正确的。

票数 1
EN

Stack Overflow用户

发布于 2015-10-23 07:15:13

client.create_textclient.create_photo的第一个参数需要是博客的blogname,所以对于照片,请做如下操作:

代码语言:javascript
复制
client.create_photo('yourblogname', 'stringlinktolocalfileorURL')

这对我有用。

票数 0
EN

Stack Overflow用户

发布于 2016-06-06 16:13:44

我也有过同样的问题。我通过硬编码oauth_token和oauth_token_secret (从https://api.tumblr.com/console/calls/user/info获得)来修正,而不是使用request_token调用。也许这些调用的任何错误都可以修复,但在此期间,硬编码可以作为一种解决办法。

摘自:http://yuluer.com/page/bgfbhjgf-python-tumblr-api-cannot-log-in-to-my-own-tumblr-to-create-posts.shtml

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29456704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档