我正在使用tweepy(一个用于twitter api的python库)来创建一个Twitter应用程序。下面是django视图代码,我在其中创建一个OAuthHandler,其中包含使用者密钥,密码和获取请求令牌,并将其保存在django.messages中并将其重定向到授权URL。
稍后在回调视图中,我正在检索请求令牌并创建一个OAUthHandler,但我在'auth.get_access_token(v)'
上面得到以下错误TweepError at /twitter/return/
argument 2 to map() must support iteration
Traceback :
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/nzee/Downloads/uday/homepage/../homepage/users/views.py" in tw_return
80. auth.get_access_token(v)
File "/usr/local/lib/python2.6/dist-packages/tweepy-1.9-py2.6.egg/tweepy/auth.py" in get_access_token
126. raise TweepError(e)
Exception Type: TweepError at /twitter/return/ Exception Value:
argument 2 to map() must support iteration
以下Views.py代码
def tw_return(request):
v = request.GET.get('oauth_verifier')
key = settings.TWITTER_KEY
secret = settings.TWITTER_SECRET
auth = tweepy.OAuthHandler(key, secret)
mess = get_messages(request)
a=[]
for m in mess:
a.append(m)
auth.set_request_token(a[0],a[1])
auth.get_access_token(v)
api = tweepy.API(auth)
api.update_status('tweepy + oauth1!')
return HttpResponseRedirect('/')
def twitter(request):
key = settings.TWITTER_KEY
secret = settings.TWITTER_SECRET
auth = tweepy.OAuthHandler(key, secret)
try:
redirect_url = auth.get_authorization_url()
except tweepy.TweepError:
raise Http404
messages.add_message(request, messages.INFO, auth.request_token.key)
messages.add_message(request, messages.INFO, auth.request_token.secret)
return HttpResponseRedirect(redirect_url)
答案 0 :(得分:1)
错误:我怀疑你没有收到oauth_verifier,所以它的值是 None
(即不是可迭代的)
这个问题可能有所帮助:Tweepy twitter oauth authentication not returning oauth_verifier
这是一个使用tweepy的appEngine处理程序的例子,也许它会有帮助吗?
https://github.com/sclm/tweepy-examples/blob/master/appengine/oauth_example/handlers.py
我认为您没有从get_messages()获取正确的请求令牌...如果您使用auth.set_request_token()
致电None
,则会出现错误。在github的问题中找到了这个用于tweepy https://github.com/tweepy/tweepy/issues/25
# WRONG CODE
auth.set_request_token(None, None)