forked from mrorii/python-api-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectXAuth.py
More file actions
51 lines (38 loc) · 1.76 KB
/
connectXAuth.py
File metadata and controls
51 lines (38 loc) · 1.76 KB
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
from oauthlib.oauth1 import Client, SIGNATURE_TYPE_BODY
from oauthlib.common import urldecode
import requests
import sys
CONTENT_TYPE_FORM_URLENCODED = 'application/x-www-form-urlencoded'
hostname = "musescore.com"
request_token_url = 'https://api.' + hostname+'/oauth/request_token'
base_authorization_url = 'https://' + hostname+'/oauth/authorize'
access_token_url = 'https://api.' + hostname+'/oauth/access_token'
#----- PLEASE CHANGE THESE VARIABLES -----
client_key = 'YOUR_CLIENT_KEY'
client_secret = 'YOUR_CLIENT_SECRET'
username = 'USER_NAME'
password = 'USER_PASSWORD'
#-----------------------------------------
resource_owner_key = ''
resource_owner_secret = ''
if client_key == '' or client_secret == '':
print "Please change your client key and secret in connectXAuth.py header"
sys.exit(0)
if username == 'USER_NAME' or password == 'USER_PASSWORD':
print "Please change username and password in connectXAuth.py header"
sys.exit(0)
client = Client(client_key, client_secret=client_secret, signature_type=SIGNATURE_TYPE_BODY)
headers = {"Content-Type": CONTENT_TYPE_FORM_URLENCODED}
body = 'x_auth_mode=client_auth&x_auth_username='+username+'&x_auth_password='+password
result = client.sign(access_token_url, http_method="POST", headers=headers, body=body)
data = result[2]
r = requests.post(access_token_url, headers=headers, data=data)
oauth_tokens = dict(urldecode(r.text.strip()))
resource_owner_key = oauth_tokens.get('oauth_token')
resource_owner_secret = oauth_tokens.get('oauth_token_secret')
cred = {"client_key": client_key, "client_secret": client_secret,
"resource_owner_key": resource_owner_key,
"resource_owner_secret": resource_owner_secret}
import json
with open('credentials.json', 'w') as outfile:
json.dump(cred, outfile)