-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
99 lines (72 loc) · 3.22 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
from requests_oauthlib import OAuth2Session
from flask import Flask, request, redirect, session, url_for
from app_utils import *
app = Flask(__name__)
# Taken from app settings in "My Apps"
# https://developers.deezer.com/myapps
config = {}
# Common for all users
authorization_base_url = 'https://connect.deezer.com/oauth/auth.php?perms=listening_history'
token_url = 'https://connect.deezer.com/oauth/access_token.php'
# Path to access token
# If no file is provided, you will need to authorize with browser
access_token_path = 'access_token.json'
@app.route("/")
def demo():
"""Step 1: User Authorization.
Redirect the user/resource owner to the OAuth provider (i.e. Deezer)
using an URL with a few key OAuth parameters.
"""
deezer = OAuth2Session(config['app_id'], redirect_uri=config['redirect_uri'])
authorization_url, state = deezer.authorization_url(authorization_base_url)
# State is used to prevent CSRF, keep this for later.
session['oauth_state'] = state
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.
"""
deezer = OAuth2Session(config['app_id'], state=session['oauth_state'])
token = deezer.fetch_token(token_url,
client_secret=config['client_secret'],
authorization_response=request.url)
# 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.
session['oauth_token'] = token
return redirect(url_for('.profile'))
@app.route("/profile", methods=["GET"])
def profile():
"""
Fetching a protected resource using an OAuth 2 token.
"""
deezer = OAuth2Session(config['app_id'], token=session['oauth_token'])
access_token = session['oauth_token']['access_token']
url_to_get = 'https://api.deezer.com/user/{}/history&access_token={}'.format(config['user_id'],
access_token)
# Load listening history from Deezer
all_history = load_listening_history(deezer, url_to_get)
# Write loaded history to a file
write_history_to_file(all_history)
msg = 'History has been loaded!\nCheck out the file in the project directory :)'
return msg
if __name__ == "__main__":
# This allows us to use a plain HTTP callback
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = "1"
# Reading config file
config = read_json('config.json')
# Checking config file and starting an application
if ('app_id' in config and
'client_secret' in config and
'redirect_uri' in config and
'user_id' in config):
app.secret_key = os.urandom(24)
app.run(debug=True)
else:
raise ValueError('Wrong config! '
'Config file must contain fields "app_id", "client_secret", "redirect_uri", "user_id"')