Skip to content

Instantly share code, notes, and snippets.

@spchamp
Last active September 2, 2024 23:29
Show Gist options
  • Save spchamp/6a9bd83041cd3d3783b3ced877e86363 to your computer and use it in GitHub Desktop.
Save spchamp/6a9bd83041cd3d3783b3ced877e86363 to your computer and use it in GitHub Desktop.
Evernote sample code (Python 3)
# Python OAuth example
#
# an adaptation of earlier sample code https://stackoverflow.com/q/71564457/1061095
#
# see also: https://github.com/spchamp/evernote-py3
#
# Documentation, Oauth 1 in Evernote
# https://dev.evernote.com/doc/articles/authentication.php
#
# Usage:
#
# - request an API Key, whether for official use or for personal application
# https://dev.evernote.com/doc/
#
# - install https://github.com/spchamp/evernote-py3
#
# $ git clone https://github.com/spchamp/evernote-py3 sdk
# $ cd sdk
# $ python -m venv env
# $ env/bin/pip install -e.
#
# - run the sample script with the API key and API secret
# set in the Python environment
#
# $ EVERNOTE_API_KEY=myapp-12334 EVERNOTE_API_SECRET=abcdef321 env/bin/python evernote_sample.py
#
# known issues:
#
# - if the oauth process results in an error, the server may respond with a redirect
# to a data URL. Without further patching, the data URL might be parsed as the final
# access token reply, resulting in a later error in the example.
#
# - HTTP/2 is not known to be supported in the final request for the access token.
#
# If using an HTTP client library that would automatically upgrade to HTTP/2 (e.g Qt networking)
# the library, application, or individual request should be configured to avoid the upgrade
# for requests during the oauth process with this endpoint
#
# - With the dev branch in the (unofficial) Python 3 fork for the Evernote SDK, the code
# is presently using a combination of bytes and string values for dictionary keys,
# depending on the context.
#
# This sample has been patched in a tentative adaptation to that quirk.
#
# - This sample code is presntly resulting in an error for every oauth workflow.
#
# The error occurs during the final request for an API access token. The error
# message is presented via a data URL provided as a redirect destination, during
# the last stage of the oauth process.
#
import os
from evernote.api.client import EvernoteClient
##
# 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)
##
client = EvernoteClient(
consumer_key=os.environ["EVERNOTE_API_KEY"],
consumer_secret=os.environ['EVERNOTE_API_SECRET'],
sandbox=False)
request_token = client.get_request_token('http://localhost')
print("Paste this URL in your browser and login")
print('\t'+client.get_authorize_url(request_token))
print('-------')
after_accept_url = 'http://localhost/?oauth_token=1111&oauth_verifier=2222&sandbox_lnb=false'
vals = parse_query_string(after_accept_url)
print(f"Auth Token: {request_token[b'oauth_token']}")
print(f"Auth Secret: {request_token[b'oauth_token_secret']}")
print(f"OAuth verifier: {vals['oauth_verifier']}")
auth_token = client.get_access_token(
request_token[b'oauth_token'],
request_token[b'oauth_token_secret'],
vals['oauth_verifier']
)
# Create a new client using the auth token
client = EvernoteClient(token=auth_token)
userStore = client.get_user_store()
user = userStore.getUser()
print(user.username)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment