Lorena Nicole
Lorena Nicole

Reputation: 519

Python Oauth Request to Twitter Yields Response 403 - SSL is Required

Using Python's oauth2 library I've been attempting to implement a three-legged oauth request (as outlined here - http://www.linuxuser.co.uk/news/handling-twitters-three-legged-oauth-process) to Twitter only to obtain a 403 response - "SSL is required" error.

I've been using this guide (https://bitbucket.org/david/django-oauth-plus/wiki/consumer_example) to help setup my 1.5 Django app to make my requests but with no avail.

Version of Python I'm currently using is 2.7.

(I have tried playing with adding the verify parameter for requests.get call and using add_certificate on my client)

The specific area I am looking to pinpoint is how I can add SSL to my request here is a snapshop of code as it currently stands --

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)

Upvotes: 0

Views: 611

Answers (1)

karthikr
karthikr

Reputation: 99620

Here:

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

These requests need to be via https://

So

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

Should do the trick.

Upvotes: 1

Related Questions