python - Tweepy - 获取访问 token 时出错 : "argument 2 to map() must support iteration"

标签 python django twitter tweepy

我正在使用tweepy (twitter api 的 python lib)创建 Twitter 应用程序。下面是 django View 代码,我使用消费者 key 、 secret 创建 OAuthHandler 并获取请求 token 并将其保存在 django.messages 中并将其重定向到授权 url。

稍后在回调 View 中,我检索请求 token 并创建 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)

最佳答案

错误:我怀疑您没有收到 oauth_verifier,因此它的值为None(即不是可迭代的) p>

这个问题可能有帮助: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() 获得正确的请求 token ...如果您使用 None 调用 auth.set_request_token() ,则会发生您收到的错误。在 tweepy 的 github 问题中找到了这个 https://github.com/tweepy/tweepy/issues/25

# WRONG CODE
auth.set_request_token(None, None)

关于python - Tweepy - 获取访问 token 时出错 : "argument 2 to map() must support iteration",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9944107/

相关文章:

python - 根据列的最大值删除 Pandas 数据框行

python - 如何获取 Python Pillow (PIL) 版本?

python - 为什么测试没有检测到 test.py 的语法错误?并帮我修复一个错误

移动网络的 Facebook 和推特分享

jquery - 如何使用 CSS 代码替换新 Twitter 小部件中的 100% 宽度?

ios - iOS 中使用 Twitter API 的简单 GET 请求

python - 如何在 Tkinter Combobox 中获取所选选项的索引

pythonplotly-旋转辅助X轴标签

python - 将计数字段添加到 django rest 框架序列化程序

django - 如何将已安装的 Django src 和其他 djano 应用程序移动到 Ubuntu 中的新位置(pip)?