来自python-oauth2的错误:“渲染时出现TypeError:必须是字符串或缓冲区,而不是None”

时间:2011-05-12 19:28:10

标签: python django twitter oauth-2.0

我正在尝试将我的Twitter时间线放在我正在Django上制作的网站上。为此,我在http://www.pixeldonor.com/blog/2010/aug/15/create-simple-twitter-app-django/上找到了一段很好的代码,我在网站的本地开发副本上实现了这段代码。本地一切都很完美!这些推文都显示出来,我没有任何错误。

现在我已经在网上部署了我的网站,我似乎遇到了一个我无法解决的错误。

这是我得到的错误:


TemplateSyntaxError at /
Caught TypeError while rendering: must be string or buffer, not None

Request Method: GET
Request URL:    http://marcostrijker.com/
Django Version: 1.3
Exception Type: TemplateSyntaxError
Exception Value: Caught TypeError while rendering: must be string or buffer, not None
Exception Location: /home/mstrijker/lib/python2.7/oauth2-1.5.170py2.7.egg/oauth2/__init__.py in sign_request, line 493
Python Executable:  /usr/local/bin/python  
Python Version: 2.7.1
Python Path:    
['/home/mstrijker/webapps/django/vorm4ufolio',
 '/home/mstrijker/webapps/django',
 '/home/mstrijker/webapps/django/lib/python2.7',
 '/home/mstrijker/lib/python2.7/oauth2-1.5.170-py2.7.egg',
 '/home/mstrijker/lib/python2.7/httplib2-0.6.0-py2.7.egg',
 '/home/mstrijker/lib/python2.7/pip-1.0.1-py2.7.egg',
 '/home/mstrijker/lib/python2.7',
 '/usr/local/lib/python27.zip',
 '/usr/local/lib/python2.7',
 '/usr/local/lib/python2.7/plat-linux2',
 '/usr/local/lib/python2.7/lib-tk',
 '/usr/local/lib/python2.7/lib-old',
 '/usr/local/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/site-packages/PIL']
Server time:    Thu, 12 May 2011 21:05:17 +0200

之后显示以下内容:

  

模板错误

     

在模板中   /home/mstrijker/webapps/django/vorm4ufolio/templates/index.html,error   在第85行

     

渲染时捕获TypeError:必须是字符串或缓冲区,而不是无

<div id="twitter">
    <a href="http://twitter.com/#!/Octopixell"><img src="{{ STATIC_URL }}images/twitter_icon.png" alt="twitter_logo"></a>
    <div id="tweetcontainer">
        {% get_tweet_list 5 as tweets %}
        {% for tweet in tweets %}
            <div class="tweettop"></div>
                <div class="tweet">
                    {{ tweet.text|safe|twitterize }} <br /> {{ tweet.created_at|date:"M d, Y" }}
                </div>
            <div class="tweetbottom"></div>
        {% endfor %}
    </div>
</div>

{% get_tweet_list 5 as tweets %}是第85行


此处还有错误的追溯:

Traceback:
File "/home/mstrijker/webapps/django/lib/python2.7/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/mstrijker/webapps/django/vorm4ufolio/portfolio/views.py" in index
  17.     return render_to_response('index.html', {'new_list': new_list,}, context_instance=RequestContext(request))
File "/home/mstrijker/webapps/django/lib/python2.7/django/shortcuts/__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/home/mstrijker/webapps/django/lib/python2.7/django/template/loader.py" in render_to_string
  188.         return t.render(context_instance)
File "/home/mstrijker/webapps/django/lib/python2.7/django/template/base.py" in render
  123.             return self._render(context)
File "/home/mstrijker/webapps/django/lib/python2.7/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/home/mstrijker/webapps/django/lib/python2.7/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/home/mstrijker/webapps/django/lib/python2.7/django/template/debug.py" in render_node
  73.             result = node.render(context)
File "/home/mstrijker/lib/python2.7/oauth2-1.5.170-py2.7.egg/oauth2/__init__.py" in request
  662.         req.sign_request(self.method, self.consumer, self.token)
File "/home/mstrijker/lib/python2.7/oauth2-1.5.170-py2.7.egg/oauth2/__init__.py" in sign_request
  493.             self['oauth_body_hash'] = base64.b64encode(sha(self.body).digest())

Exception Type: TemplateSyntaxError at /
Exception Value: Caught TypeError while rendering: must be string or buffer, not None

我希望这能为您提供帮助我解决此问题所需的必要信息。如果你们需要更多的信息或代码,请问我,我会提供。

2 个答案:

答案 0 :(得分:1)

我在帖子中浏览了oauth2的代码,我怀疑oauth_req签名中post_body的默认值应该是'',而不是None。

def oauth_req(url, http_method="GET", post_body=None, http_headers=None):

试试吧。

答案 1 :(得分:0)

此问题是python-oauth2模块中存在一个错误,当请求的主体(或后续请求)为None时,会引发TypeError。以下是oauth2/\_\_init\_\_.py中的sign_request函数:

def sign_request(self, signature_method, consumer, token):
    """Set the signature parameter to the result of sign."""

    if not self.is_form_encoded:            
        # according to
        # http://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/oauth-bodyhash.html
        # section 4.1.1 "OAuth Consumers MUST NOT include an
        # oauth_body_hash parameter on requests with form-encoded
        # request bodies."
        self['oauth_body_hash'] = base64.b64encode(sha(self.body).digest())

    if 'oauth_consumer_key' not in self:
        self['oauth_consumer_key'] = consumer.key

    if token and 'oauth_token' not in self:
        self['oauth_token'] = token.key

    self['oauth_signature_method'] = signature_method.name
    self['oauth_signature'] = signature_method.sign(self, consumer, token) 

第493行是:

self['oauth_body_hash'] = base64.b64encode(sha(self.body).digest())  

所以身体被设置为None。由于httplib2在关注重定向时将请求正文设置为None,我的猜测是,当您在线部署时,会出现一个httplib2跟随的新重定向,然后将正文设置为None并且提出这个错误。

好消息是,这是我修复过的错误issued a pull request。在它被接受之前,您可以编辑自己的oauth2/\_\_init\_\_.py文件来处理正文为Nonedownload and use我的python-oauth版本的情况。