Skip to content

Instantly share code, notes, and snippets.

@muzizongheng
Created June 28, 2013 15:46
Show Gist options
  • Save muzizongheng/5885681 to your computer and use it in GitHub Desktop.
Save muzizongheng/5885681 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import cgi, cgitb
# Uncomment this to view errors
# cgitb.enable()
import urlparse
import urllib
import sys
import oauth2 as oauth
class Evernote:
CONSUMER_KEY = "YOUR-CONSUMER-KEY"
SHARED_SECRET = "YOUR-CONSUMER-SECRET"
API_URL = "https://sandbox.evernote.com" # https://www.evernote.com FOR PRODUCTION
def __init__(self, temp_token, verifier):
if (verifier is None):
self.consumer = oauth.Consumer(key = Evernote.CONSUMER_KEY,
secret = Evernote.SHARED_SECRET)
self.client = oauth.Client(self.consumer)
# YOU CAN APPEND SESSION INFO WITH YOUR URL
self.request_token = self.get_request_token("CALLBACK-URL-TO-YOUR-SERVICE")
request_token = self.request_token["oauth_token"]
# E.G. HTTPS://EXAMPLE.COM?SESSION=1234567
request_secret = self.request_token["oauth_token_secret"]
request_callback = self.request_token["oauth_callback_confirmed"]
redirect = self.redirect(request_token)
print 'Location:'+redirect
print
else:
self.consumer = oauth.Consumer(key = Evernote.CONSUMER_KEY,
secret = Evernote.SHARED_SECRET)
self.token = oauth.Token(temp_token, '')
self.token.set_verifier(verifier)
self.client = oauth.Client(self.consumer, self.token)
self.client.set_signature_method(oauth.SignatureMethod_PLAINTEXT())
self.access_token = self.get_request_access_token()
edam_shard = self.access_token["edam_shard"]
edam_userId = self.access_token["edam_userId"]
print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
print "<TITLE>YOUR-TITLE</TITLE>"
print "<H1>blood on the oauth dance floor!</H1>"
print "<p>edam_shard: ", edam_shard ,"</p><br>"
print "<p>edam_userId: ", edam_userId ,"</p>"
# THAT'S IT. YOUR EVERNOTE INTEGRATION STARTS HERE
def request_token_url(self) :
return self.API_URL + "/oauth"
def get_request_access_token(self) :
resp, access_token_content = self.client.request(self.request_token_url(), "POST")
# Make a dictionary out of the token content so we can retrieve pieces.
return dict(urlparse.parse_qsl(access_token_content))
def get_request_token(self, callback) :
# Specify the callback url
params = {}
params["oauth_callback"] = callback
# With the oauth2 library, body data only works using POST
resp, request_token_content = self.client.request(self.request_token_url(),
"POST", body=urllib.urlencode(params))
# Make a dictionary out of the token content so we can retrieve pieces.
return dict(urlparse.parse_qsl(request_token_content))
def redirect(self, request_token):
# FOR PRODUCTION, CHANGE THIS TO https://www.evernote.com/OAuth.action?oauth_token=" + request_token
tokenurl = "https://sandbox.evernote.com/OAuth.action?oauth_token=" + request_token
return tokenurl
def main():
form = cgi.FieldStorage()
if form.has_key("oauth_verifier"):
oauth_token = form['oauth_token'].value
verifier = form['oauth_verifier'].value
# LAST STEP OF THE OAUTH DANCE
evertrack = Evernote(oauth_token, verifier)
else:
# FIRST STEP OF THE OAUTH DANCE
evertrack = Evernote(temp_token=None, verifier=None)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment