Python Oauth请求Twitter产生响应403 - SSL是必需的

时间:2014-04-03 14:08:47

标签: python django ssl twitter oauth

使用Python的oauth2库我一直试图在Twitter上实现一个三条腿的oauth请求(如此处所述 - http://www.linuxuser.co.uk/news/handling-twitters-three-legged-oauth-process),只是为了获得403响应 - “SSL是必需的”错误。

我一直在使用本指南(https://bitbucket.org/david/django-oauth-plus/wiki/consumer_example)来帮助设置我的1.5 Django应用程序以提出我的请求但无济于事。

我目前使用的Python版本是2.7。

(我试过在request.get call中添加verify参数并在我的客户端上使用add_certificate)

我希望确定的具体领域是如何在我的请求中添加SSL,这是目前的代码快照 -

import oauth2 as oauth
import requests
import urlparse

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.conf import settings

from twitterapp.models import User

consumer = oauth.Consumer(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SEC)
client = oauth.Client(consumer)
client.add_certificate

request_token_url = 'http://api.twitter.com/oauth/request_token'
access_token_url = 'http://api.twitter.com/oauth/access_token'
authorize_url='https://api.twitter.com/oauth/authorize'

def signin(request):
    oauth_request = oauth.Request.from_consumer_and_token(consumer, http_url=request_token_url, parameters={'oauth_callback':callback_url})
    oauth_request.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, None)
    response = requests.get(request_token_url, headers=oauth_request.to_header(), verify=True)
    request_token = dict(urlparse.parse_qsl(response.content))

    url = 'https://api.twitter.com/oauth/authorize?oauth_token=%s' % request_token['oauth_token']

    return HttpResponseRedirect(url)

1 个答案:

答案 0 :(得分:1)

这里:

request_token_url = 'http://api.twitter.com/oauth/request_token'
access_token_url = 'http://api.twitter.com/oauth/access_token'

这些请求必须通过https://

所以

request_token_url = 'https://api.twitter.com/oauth/request_token'
access_token_url = 'https://api.twitter.com/oauth/access_token'

应该做的伎俩。