Skip to content

Instantly share code, notes, and snippets.

@badri
Last active November 10, 2015 05:47
Show Gist options
  • Save badri/2666d0610ade502c06bd to your computer and use it in GitHub Desktop.
Save badri/2666d0610ade502c06bd to your computer and use it in GitHub Desktop.
testing of drupal oauth services
from requests_oauthlib import OAuth1Session
import base64
client_key ='r8xyENN6KWMJPvXihv3oPVdHNCxVY6Ab';
client_secret = 'YpmEw73aVVtoCmskXH6AvB3xh4PmDrSR';
resource_owner_key = 'k4nwr3hQoz9Wo5y3SVkqJ6qoSzvYs89Q'
resource_owner_secret = 'i7L2UfqR8AjLfmLovNduPAuay8S6Dt4w'
protected_url = 'http://localhost/scout/profile/profile.json'
oauth = OAuth1Session(client_key,
client_secret=client_secret,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret)
# no username
payload = {'password': 'password'}
r = oauth.post(protected_url, data=payload)
print r.status_code
print r.text
# no e-mail
payload = {'name': 'foo'}
r = oauth.post(protected_url, data=payload)
print r.status_code
print r.text
# no password
payload = {'name': 'foo', 'mail':'b@c.cc'}
r = oauth.post(protected_url, data=payload)
print r.status_code
print r.text
# invalid country
payload = {'name': 'lakshminp1123', 'mail': 'laks@c.cc', 'password': 'password', 'country': 'Indi'}
r = oauth.post(protected_url, data=payload)
print r.status_code
print r.text
#invalid email
payload = {'name': 'lakshminp12334', 'mail': 'laks@', 'password': 'password', 'country': 'India'}
r = oauth.post(protected_url, data=payload)
print r.status_code
print r.text
# user exists already
payload = {'name': 'admin', 'mail': 'laks@c.cc', 'password': 'password', 'country': 'India'}
r = oauth.post(protected_url, data=payload)
print r.status_code
print r.text
# successful usecase
with open('/home/lakshmi/Dropbox/pics/profile.jpg', 'rb') as fd:
b64data = base64.b64encode(fd.read())
payload = {'name': 'lakshminp8086', 'mail': 'laks@c.cc', 'password': 'password', 'country': 'India', 'file': b64data, 'filename': 'profile.jpg'}
r = oauth.post(protected_url, data=payload)
print r.status_code
print r.text
# payload = {'title': 'hello world api', 'type': 'event'}
# protected_url = 'http://localhost/scout/profile/node.json'
# r = oauth.post(protected_url, data=payload)
# print r.status_code
# print r.text
# with open('/home/lakshmi/Dropbox/pics/drupal.jpg', 'rb') as fd:
# b64data = base64.b64encode(fd.read())
# files = {'file': b64data, 'filename': 'drupal.jpg'}
# protected_url = 'http://localhost/scout/profile/file.json'
# r = oauth.post(protected_url, data=files)
# print r.status_code
# print r.text
from requests_oauthlib import OAuth1Session
client_key ='r8xyENN6KWMJPvXihv3oPVdHNCxVY6Ab';
client_secret = 'YpmEw73aVVtoCmskXH6AvB3xh4PmDrSR';
request_token_url = 'http://localhost/scout/oauth/request_token'
base_authorization_url = 'http://localhost/scout/oauth/authorize'
access_token_url = 'http://localhost/scout/oauth/access_token'
protected_url = 'http://localhost/scout/profile/node.json'
#1 get request token
oauth = OAuth1Session(client_key, client_secret=client_secret)
fetch_response = oauth.fetch_request_token(request_token_url)
resource_owner_key = fetch_response.get('oauth_token')
resource_owner_secret = fetch_response.get('oauth_token_secret')
#2 authorize
authorization_url = oauth.authorization_url(base_authorization_url)
print 'Please go here and authorize,', authorization_url
redirect_response = raw_input('Paste the full redirect URL here: ')
oauth_response = oauth.parse_authorization_response(redirect_response)
verifier = "sdflk3450FASDLJasd2349dfs"
#3 get access token
oauth = OAuth1Session(client_key,
client_secret=client_secret,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=verifier)
oauth_tokens = oauth.fetch_access_token(access_token_url)
resource_owner_key = oauth_tokens.get('oauth_token')
resource_owner_secret = oauth_tokens.get('oauth_token_secret')
print client_key
print client_secret
print resource_owner_key
print resource_owner_secret
#4 access protected resources
oauth = OAuth1Session(client_key,
client_secret=client_secret,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret)
payload = {'nid': '123'}
r = oauth.get(protected_url, params=payload)
print r.text
r = oauth.post(protected_url)
print r.text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment