forked from mrorii/python-api-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect.py
More file actions
53 lines (40 loc) · 1.79 KB
/
connect.py
File metadata and controls
53 lines (40 loc) · 1.79 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
52
53
from requests_oauthlib import OAuth1Session
import webbrowser
import sys
hostname = "musescore.com"
request_token_url = 'http://api.' + hostname+'/oauth/request_token'
base_authorization_url = 'http://' + hostname+'/oauth/authorize'
access_token_url = 'http://api.' + hostname+'/oauth/access_token'
client_key = 'YOUR_CLIENT_KEY'
client_secret='YOUR_CLIENT_SECRET'
resource_owner_key=''
resource_owner_secret=''
if client_key == 'YOUR_CLIENT_KEY' or client_secret == 'YOUR_CLIENT_SECRET':
print "Please change your client key and secret in connect.py header"
sys.exit(0)
#obtain a request token
oauth = OAuth1Session(client_key, client_secret=client_secret)
fetch_response = oauth.fetch_request_token(request_token_url)
print fetch_response
resource_owner_key = fetch_response.get('oauth_token')
resource_owner_secret = fetch_response.get('oauth_token_secret')
# Obtain authorization
authorization_url = oauth.authorization_url(base_authorization_url)
print 'Please go here and authorize,', authorization_url
webbrowser.open(authorization_url)
redirect_response = raw_input('Press any key when authorized')
# Obtain access token
oauth = OAuth1Session(client_key,
client_secret=client_secret,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret)
oauth_tokens = oauth.fetch_access_token(access_token_url)
print oauth_tokens
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)