Skip to content

Instantly share code, notes, and snippets.

@newsomc
Created December 15, 2013 16:06
Show Gist options
  • Save newsomc/7974765 to your computer and use it in GitHub Desktop.
Save newsomc/7974765 to your computer and use it in GitHub Desktop.
Beatport OAuth 1.0 documentation: https://oauth-api.beatport.com/
from requests_oauthlib import OAuth1Session
from flask import Flask, request, redirect, session, url_for, current_app
from flask.json import jsonify
import os
import urllib
app = Flask(__name__)
# This information is obtained upon registration of a new Bp
client_id='<key>'
client_secret='<secret>'
# 1. OAuth endpoints given in the Beatport API documentation.
request_token_url = 'https://oauth-api.beatport.com/identity/1/oauth/request-token'
authorization_base_url = 'https://oauth-api.beatport.com/identity/1/oauth/authorize'
access_token_url = 'https://oauth-api.beatport.com/identity/1/oauth/access-token'
callback_uri = 'http://127.0.0.1:5000/callback'
@app.route("/")
def demo():
"""Step 1: User Authorization.
Redirect the user/resource owner to the OAuth provider (i.e. Beatport)
using an URL with a few key OAuth parameters.
"""
beatport = OAuth1Session(client_id, client_secret=client_secret, callback_uri=callback_uri)
beatport.fetch_request_token(request_token_url)
authorization_url = beatport.authorization_url(authorization_base_url)
return redirect(authorization_url)
# Step 2: User authorization, this happens on the provider.
@app.route("/callback", methods=["GET"])
def callback():
""" Step 3: Retrieving an access token.
The user has been redirected back from the provider to your registered
callback URL. With this redirection comes an authorization code included
in the redirect URL. We will use that to obtain an access token.
"""
beatport = OAuth1Session(client_id, client_secret=client_secret)
beatport.parse_authorization_response(request.url)
# current_app.logger.debug(auth_response)
# current_app.logger.debug(access_token_url)
# This is where error out.
access_token = beatport.fetch_access_token(access_token_url)
# current_app.logger.debug(access_token)
# # At this point you can fetch protected resources but lets save
# # the token and show how this is done from a persisted token
# # in /profile.
return redirect(url_for('.profile'))
@app.route("/profile", methods=["GET"])
def profile():
"""Fetching a protected resource using an OAuth 2 token.
"""
beatport = OAuth1Session(client_id, client_secret=client_secret)
return jsonify(beatport.get('https://oauth-api.beatport.com/catalog/3/beatport/home').json())
if __name__ == "__main__":
# This allows us to use a plain HTTP callback
os.environ['DEBUG'] = "1"
app.secret_key = os.urandom(24)
app.run(debug=True)
@sigmavirus24
Copy link

@newsomc can you place backticks aruond your traceback?

\```
<traceback>
\```

Be sure to exclude the backslashes \ and use newlines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment