Search code examples
phppythonapioauthrauth

How do I get the oauth_verifier for a session in Rauth


I'm using Rauth for my requests to the Beatport API. The follwing is my current code.

from rauth import OAuth1Service

beatport = OAuth1Service(
    name='beatport',
    consumer_key='xxxxxxxxxxxxxx',
    consumer_secret='xxxxxxxxxxxxxxxxxx',
    request_token_url= 'https://oauth-api.beatport.com/identity/1/oauth/request-token',
    access_token_url='https://oauth-api.beatport.com/identity/1/oauth/access-token',
    authorize_url='https://oauth-api.beatport.com/identity/1/oauth/authorize',
    base_url='https://oauth-api.beatport.com/json/catalog')

request_token, request_token_secret = beatport.get_request_token(method='POST')

print request_token
print request_token_secret

This part works correctly and prints the tokens

authorize_url = beatport.get_authorize_url(request_token)

print authorize_url

This generates the authorization URL along with the request token.

import urllib
import urllib2

beatport_login = 'login'
beatport_pass = 'pass'

post_string = 'oauth_token='+request_token+'&username='+beatport_login+'&password='+beatport_pass+'&submit=Login'
f = {'https://oauth-api.beatport.com/identity/1/oauth/authorize-submit' : post_string }
g = {'https://oauth-api.beatport.com/identity/1/oauth/request-token?oauth_callback' : 'http://localhost:8000/'}

print urllib.urlencode(f)
print urllib.urlencode(g)

#print('Visit this URL in your browser: {url}'.format(url=authorize_url))
#pin = raw_input('Enter PIN from browser: ')

session = beatport.get_raw_access_token(request_token, request_token_secret, method='POST', data={
    'oauth_verifier': pin })

print session

r = session.get('https://oauth-api.beatport.com/catalog/3/tracks?returnFacets=artistName%3AHardwell&perPage=5&sortBy=releaseDate+DESC', params={'format': 'json'})
print r.json()

This is the part that confuses me. For the session I need a pin as the oauth_verifier. According to this answer it'll be appended at the end of my callback URL but I'm unable to understand the steps. How do I get this pin?

I'm using this working example in PHP as reference.


Solution

  • I got it to work. I've made a lot of changes to the rest of the code, but here is the code for getting the oauth verifier.

    values = {
        'oauth_token': request_token,
        'username': beatport_login,
        'password': beatport_pass,
        'submit' : 'Login',
    }
    
    r = requests.post('https://oauth-api.beatport.com/identity/1/oauth/authorize-submit', data=values)
    
    verifier = r.url.split("oauth_verifier=",1)[1]
    
    tokens = beatport.get_raw_access_token(request_token, request_token_secret, method='POST', data={
        'oauth_verifier': verifier})