Instantly share code, notes, and snippets.

What would you like to do?
Learning Registry OAuth Sample

OAuth Sample

This is a trivial example of publishing to Learning Registry using OAuth 1.0 authored in python 2.7.

To use, you will need to install the oauth2 library:

    pip install oauth2

Once installed execute using:

import json, urllib, oauth2 as oauth
# Sample publish to LR Sandbox using OAuth
_netloc = ""
_testing_url = "https://%s/auth/oauth_verify" % _netloc
_publish_url = "https://%s/publish" % _netloc
_obtain_url = "https://%s/obtain?by_doc_ID=true&request_ID=%s"
# Go to to create a publishing account,
# Replace the Consumer and Token keys and secrets below with the ones generated there.
_consumer = {
"key": "",
"secret": "eQMlCjUC88CjCOYgGgnHm1t48H48UF43"
_token = {
"key": "node_sign_token",
"secret": "lRMhkV6rIEl1xSvppkym7KSpwgHEYkCJ"
# Just a dummy resource data document
resource_doc = {
"doc_type": "resource_data",
"doc_version": "0.23.0",
"active": True,
"resource_data_type": "metadata",
"resource_locator": "",
"keys": [],
"payload_placement": "inline",
"payload_schema": ["sample"],
"resource_data": "This is sample data",
"TOS": {
"submission_TOS": ""
def Oauth2Lib():
# Create your consumer with the proper key/secret.
consumer = oauth.Consumer(key=_consumer["key"],
token = oauth.Token(key=_token["key"],
# Create our client.
client = oauth.Client(consumer, token=token)
# we only need to do this because Sandbox uses a self-signed SSL cert that is untrusted. This should not be needed on production.
# This request just allows you to Test your OAuth Credentials, if it succeeds it will return the contents of your user profile
resp, content = client.request(_testing_url, "POST", body=json.dumps({"documents": [resource_doc]}), headers={"Content-Type": "application/json"})
print resp
print content
# Publish a dummy document using OAuth node signing
resp, content = client.request(_publish_url, "POST", body=json.dumps({"documents": [resource_doc]}), headers={"Content-Type": "application/json"})
print resp
print content
# Read the document back from the node. Note the identity and digital_signature blocks will be updated to reflect signer and submitter.
result = json.loads(content)["document_results"][0]
resp = urllib.urlopen(_obtain_url%(_netloc, result["doc_ID"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment