Created
December 15, 2013 16:06
-
-
Save newsomc/7974765 to your computer and use it in GitHub Desktop.
Beatport OAuth 1.0 documentation: https://oauth-api.beatport.com/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
@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
Traceback:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in call
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functionsrule.endpoint
File "/Users/hcnewsom/Sites/beatport/bp-flask.py", line 48, in callback
access_token = beatport.fetch_access_token(access_token_url)
File "/Library/Python/2.7/site-packages/requests_oauthlib/oauth1_session.py", line 228, in fetch_access_token
token = self._fetch_token(url)
File "/Library/Python/2.7/site-packages/requests_oauthlib/oauth1_session.py", line 264, in _fetch_token
token = dict(urldecode(self.post(url).text))
File "/Library/Python/2.7/site-packages/oauthlib/common.py", line 135, in urldecode
raise ValueError('Not a valid urlencoded string.')
ValueError: Not a valid urlencoded string.