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:
python oauth-sample.py
| import json, urllib, oauth2 as oauth | |
| # Sample publish to LR Sandbox using OAuth | |
| _netloc = "sandbox.learningregistry.org" | |
| _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 https://sandbox.learningregistry.org/auth to create a publishing account, | |
| # Replace the Consumer and Token keys and secrets below with the ones generated there. | |
| _consumer = { | |
| "key": "[email protected]", | |
| "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": "http://example.com/oauth-sample/resource", | |
| "keys": [], | |
| "payload_placement": "inline", | |
| "payload_schema": ["sample"], | |
| "resource_data": "This is sample data", | |
| "TOS": { | |
| "submission_TOS": "http://www.learningregistry.org/information-assurances/open-information-assurances-1-0" | |
| } | |
| } | |
| def Oauth2Lib(): | |
| # Create your consumer with the proper key/secret. | |
| consumer = oauth.Consumer(key=_consumer["key"], | |
| secret=_consumer["secret"]) | |
| token = oauth.Token(key=_token["key"], | |
| secret=_token["secret"]) | |
| # 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. | |
| client.disable_ssl_certificate_validation=True | |
| # 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"])) | |
| print resp.read() | |
| Oauth2Lib() |