-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth_two.py
executable file
·119 lines (96 loc) · 3.4 KB
/
oauth_two.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python
# We use the code from python OAuth example as the basis to authenticate
# https://gist.github.com/inkedmn/5041037
import os
import sys
sys.path.append("/static/Data/1/plugins/evernote/lib")
# Python OAuth example
import evernote.edam.userstore.constants as UserStoreConstants
import evernote.edam.type.ttypes as Types
from evernote.api.client import EvernoteClient
plugin_name = "evernote"
plugin_dir = "/static/Data/1/plugins"
plugin_path = plugin_dir + "/" + plugin_name
plugin_file_path = plugin_path + "/files"
plugin_cache_path = plugin_path + "/cache"
plugin_contentHash_path = plugin_path + "/contentHash"
##
# Helper function to turn query string parameters into a
# Python dictionary
##
def parse_query_string(authorize_url):
uargs = authorize_url.split('?')
vals = {}
if len(uargs) == 1:
raise Exception('Invalid Authorization URL')
for pair in uargs[1].split('&'):
key, value = pair.split('=', 1)
vals[key] = value
return vals
##
# Create an instance of EvernoteClient using your API
# key (consumer key and consumer secret)
# include sandbox=True if operating in sandbox environment
##
client = EvernoteClient(
consumer_key = '',
consumer_secret = ''
)
##
# Parse the URL to get the OAuth verifier
##
authurl = os.environ.get("QUERY_STRING", "NOQS")
if authurl == "NOQS":
print "Content-type: text/html\n\n"
print "<html><body>Error: No Query String</body></html>"
exit()
print "Content-type: text/html\n\n"
print "<html><body><pre>"
# print "Query String: " + authurl
vals = parse_query_string("http://localhost/?" + authurl)
if not 'oauth_verifier' in vals:
print "Error: No OAuth Verifier</body></html>"
exit()
# print "OAuth: " + vals['oauth_verifier']
# Read the token secrets
token_secret = plugin_path + "/token_secret"
f = open(token_secret, "r")
oauth_token_secret = f.readline()
f.close()
oauth_token_secret.rstrip()
auth_token = client.get_access_token(
vals['oauth_token'],
oauth_token_secret,
vals['oauth_verifier']
)
# Save the auth token to a file
auth_token_file = plugin_path + "/authToken"
f = open(auth_token_file, "w")
f.write(auth_token)
f.close()
# delete token_secret file
os.remove(token_secret)
##
# Create a new EvernoteClient instance with our auth
# token.
##
client = EvernoteClient(token=auth_token, sandbox=True)
##
# Test the auth token...
##
userStore = client.get_user_store()
user = userStore.getUser()
# Print successful output in form
print "Notes Backup"
print "-------------"
print "Authentication successful"
print "Your username is: ", user.username
note_store = client.get_note_store()
# List all of the notebooks in the user's account
notebooks = note_store.listNotebooks()
print "Found ", len(notebooks), " notebooks:"
for notebook in notebooks:
print " * ", notebook.name
print "To backup your evernote into the drive, telnet into your drive and run ./evernote-sync.py in the evernote plugin directory"
print "Alternatively you can run the 'Custom Script' action in the Settings of the drive"
print "</pre></body></html>"