python-oauth2错误:“渲染时类型错误:必须是字符串或缓冲区,而不是None”

0 投票
2 回答
4603 浏览
提问于 2025-04-16 17:31

我正在尝试把我的Twitter时间线放到我用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,第85行

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

<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

这个问题是关于python-oauth2模块的一个bug,当请求的内容(或者后续请求的内容)是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,从而引发了这个错误。

好消息是,这个bug我已经修复了,并且提交了一个请求。在它被接受之前,你可以选择修改自己的oauth2/\_\_init\_\_.py文件,以处理内容为None的情况,或者下载并使用我修复过的python-oauth版本。

1

我快速浏览了一下关于oauth2的帖子,觉得oauth_req这个函数里,post_body的默认值应该是空字符串'',而不是None。

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

试试看。

撰写回答