Question

I'm trying to get a website to connect to LinkedIn, I know I have to use OAuth tokens to connect to it, and I have seen the examples on the LinkedIn developer site, however, they all use the OAuth2 library and the site that I'm working with uses the rauth library, which seems to skip a few of the steps. It already has Twitter integrated, so I'll include the code below in case anyone doesn't quite understand what I mean.

twitter.py:

import json

from django.http import HttpResponse
from django.conf import settings
from rauth.service import OAuth1Service

def twitter(request, username):
    twitter = OAuth1Service(
    name='twitter',
    consumer_key=settings.TWITTER_CONSUMER_KEY,
    consumer_secret=settings.TWITTER_CONSUMER_SECRET,
    request_token_url=settings.TWITTER_API_URL + 'oauth/request_token',
    access_token_url=settings.TWITTER_API_URL + 'oauth/access_token',
    authorize_url=settings.TWITTER_API_URL + 'oauth/authorize',
    header_auth=True)

url = '{0}1/statuses/user_timeline.json?include_rts=false' \
    '&exclude_replies=true&count=50&screen_name={1}'.format(
        settings.TWITTER_API_URL, username)

r = twitter.request('GET', url, access_token=settings.TWITTER_USER_KEY,
                    access_token_secret=settings.TWITTER_USER_SECRET)

return HttpResponse(content=json.dumps(r.response.json),
                    status=r.response.status_code,
                    content_type=r.response.headers['content-type'])

Since it isn't commented, I think it's makes a request to the url which returns the user's timeline when sent, but how come there's no request token creation OR access token creation? It has the TWITTER_USER_KEY and TWITTER_USER_SECRET, but the rauth documentation says you should call explicit methods to get the tokens. What am I missing here?

EDIT: I'd quite like to just use the rauth library, and not meddle around with other libraries too.

Was it helpful?

Solution

r = twitter.request('GET', url, access_token=settings.TWITTER_USER_KEY,
                access_token_secret=settings.TWITTER_USER_SECRET)

The function twitter creates an OAuth1Service and returns the object. The above code requests the users timeline and it seems that the access tokens and secrets are present in the settings object. It could be that the authentication routine for obtaining the access token and secrets is elsewhere.

A typical example of the usage of the library (taken directly from the rauth documentation http://packages.python.org/rauth/)

service = OAuth1Service(
           name='example',
           consumer_key='123',
           consumer_secret='456',
           request_token_url='http://example.com/request_token',
           access_token_url='http://example.com/access_token',
           authorize_url='http://example.com/authorize')

request_token, request_token_secret = service.get_request_token()
authorize_url = service.get_authorize_url(request_token)

The authorize URL is passed to the browser so that it will redirect the user to the oAuth provider's website and he can grant permission for the application and thereafter redirected to the client and the below request can be sent to obtain the access token using which requests to fetch data can be made.

response = service.get_access_token(method='GET'
                             request_token=request_token,
                             request_token_secret=request_token_secret)
request_token, request_token_secret = service.get_request_token()

Happy coding :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top