来自python-oauth2的错误:“quot;呈现时出现TypeError:必须是字符串或缓冲区,而不是None“;
我试图在我的网站上获取我的推特时间线,我正在Django制作。为此,我找到了一段很好的代码,我在本地的网站开发副本上实现了它。在本地,一切都很完美!推文显示出来了,我没有收到任何错误 现在我已经在线部署了我的网站,我似乎遇到了一个无法解决的错误 这是我得到的错误:来自python-oauth2的错误:“quot;呈现时出现TypeError:必须是字符串或缓冲区,而不是None“;,python,django,twitter,oauth-2.0,Python,Django,Twitter,Oauth 2.0,我试图在我的网站上获取我的推特时间线,我正在Django制作。为此,我找到了一段很好的代码,我在本地的网站开发副本上实现了它。在本地,一切都很完美!推文显示出来了,我没有收到任何错误 现在我已经在线部署了我的网站,我似乎遇到了一个无法解决的错误 这是我得到的错误: 之后,它将显示以下内容: 模板错误 模板中 /home/mstrijker/webapps/django/vorm4ufolio/templates/index.html,错误 第85行 呈现时捕获到TypeError:必须是字符
之后,它将显示以下内容: 模板错误 模板中 /home/mstrijker/webapps/django/vorm4ufolio/templates/index.html,错误 第85行 呈现时捕获到TypeError:必须是字符串或缓冲区,而不是无
我希望这将为你们提供必要的信息来帮助我解决这个问题。如果你们需要更多的信息或代码,请询问我,我会提供。我浏览了文章oauth2的代码,我怀疑oauth2的签名中post_body的默认值应该是“”,而不是None
def oauth_req(url, http_method="GET", post_body=None, http_headers=None):
试试看。这个问题是python-oauth2模块中有一个bug,当请求(或后续请求)的主体为
None
时抛出一个类型错误。下面是oauth2/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\.py中的签名请求函数:
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/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\.py
文件来处理主体为None
的情况,或者我的python oauth版本。我现在已经尝试过了(希望这是您的意思):def oauth\u-req(url,http\u method=“GET”,post\u-body='',http\u-headers=''):consumer=oauth.consumer(key=CONSUMER\u key,secret=CONSUMER\u secret)token=oauth.token(key=oauth\u token,secret=oauth\u secret)client=oauth.client(CONSUMER,token)
不过这并没有什么不同。你重新启动了apache吗?有时候,当你修改代码时,你需要重新启动apache才能使其正常工作。你是最棒的!我已经像你说的那样更改了代码,重新启动了apache,完全解决了问题!非常感谢!
def oauth_req(url, http_method="GET", post_body=None, http_headers=None):
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)
self['oauth_body_hash'] = base64.b64encode(sha(self.body).digest())