Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
added better support for oauth 1.0a + update readme to use 1.0a as
Browse files Browse the repository at this point in the history
example
  • Loading branch information
dustinwhittle committed Oct 12, 2009
1 parent 260d108 commit 537439c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 86 deletions.
14 changes: 12 additions & 2 deletions README.markdown
Expand Up @@ -104,10 +104,10 @@ Examples
oauthapp = yahoo.application.OAuthApplication(CONSUMER_KEY, CONSUMER_SECRET, APPLICATION_ID, CALLBACK_URL) oauthapp = yahoo.application.OAuthApplication(CONSUMER_KEY, CONSUMER_SECRET, APPLICATION_ID, CALLBACK_URL)


# Fetch request token # Fetch request token
request_token = oauthapp.get_request_token() request_token = oauthapp.get_request_token(CALLBACK_URL)


# Redirect user to authorization url # Redirect user to authorization url
redirect_url = oauthapp.get_authorization_url(request_token, CALLBACK_URL) redirect_url = oauthapp.get_authorization_url(request_token, verifier)


# Exchange request token for authorized access token # Exchange request token for authorized access token
access_token = oauthapp.get_access_token(request_token) access_token = oauthapp.get_access_token(request_token)
Expand All @@ -120,6 +120,16 @@ Examples
print profile print profile




## Signing with SimpleAuth (OpenID + OAuth):

See the bundled sample code in examples/simpleauth/simpleauth.py.


## Fetching people and activities with OpenSocial:

See the bundled sample code in examples/opensocial/profile.py.


Tests Tests
===== =====


Expand Down
6 changes: 0 additions & 6 deletions examples/appengine/djangodemo/yosdemo/models.py
@@ -1,11 +1,5 @@
# Create your models here.

# models.py # models.py


from django.db import models from django.db import models


from google.appengine.ext import db from google.appengine.ext import db

#class Visitor(db.Model):
# ip = db.StringProperty()
# added_on = db.DateTimeProperty(auto_now_add=True)
27 changes: 0 additions & 27 deletions examples/appengine/djangodemo/yosdemo/tests.py

This file was deleted.

19 changes: 2 additions & 17 deletions examples/appengine/djangodemo/yosdemo/views.py
@@ -1,24 +1,9 @@
# Create your views here.

# views.py # views.py


from django.http import HttpResponse from django.http import HttpResponse


# from mashname.main.models import Visitor

def main(request): def main(request):


#visitor = Visitor() result = "demo .........................................................."
#visitor.ip = request.META["REMOTE_ADDR"]
#visitor.put()

#result = ""
#visitors = Visitor.all()
#visitors.order("-added_on")

#for visitor in visitors.fetch(limit=40):
# result += visitor.ip + u" visited on " + unicode(visitor.added_on) + u""

result = "demo .........................................................."


return HttpResponse(result) return HttpResponse(result)
2 changes: 1 addition & 1 deletion examples/appengine/yosdemo.py
Expand Up @@ -86,7 +86,7 @@ def get(self):
session.save() session.save()


# redirect the user to authorize the request token # redirect the user to authorize the request token
self.redirect(oauthapp.get_authorization_url(request_token, CALLBACK_URL)) self.redirect(oauthapp.get_authorization_url(request_token))


else: else:


Expand Down
44 changes: 19 additions & 25 deletions src/yahoo/application.py
Expand Up @@ -46,7 +46,7 @@ class OAuthApplicationException(Exception):


class OAuthApplication(object): class OAuthApplication(object):


def __init__(self, consumer_key, consumer_secret, application_id, callback_url = None, token = None, options = { }): def __init__(self, consumer_key, consumer_secret, application_id, callback_url = None, token = None, options = { 'lang': 'en' }):


self.client = oauth.Client() self.client = oauth.Client()


Expand All @@ -62,15 +62,14 @@ def __init__(self, consumer_key, consumer_secret, application_id, callback_url =
self.signature_method_hmac_sha1 = oauthlib.oauth.OAuthSignatureMethod_HMAC_SHA1() self.signature_method_hmac_sha1 = oauthlib.oauth.OAuthSignatureMethod_HMAC_SHA1()


# oauth standard apis # oauth standard apis
def get_request_token(self): def get_request_token(self, callback = 'oob'):
# self.options['lang'] parameters = { 'xoauth_lang_pref': self.options['lang'], 'oauth_callback': callback }
parameters = { 'xoauth_lang_pref': 'en' }
request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_method='GET', http_url=self.client.request_token_url, parameters=parameters) request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_method='GET', http_url=self.client.request_token_url, parameters=parameters)
request.sign_request(self.signature_method_plaintext, self.consumer, None) request.sign_request(self.signature_method_plaintext, self.consumer, None)
return self.client.fetch_request_token(request) return self.client.fetch_request_token(request)


def get_authorization_url(self, request_token, callback): def get_authorization_url(self, request_token):
return oauthlib.oauth.OAuthRequest.from_token_and_callback(token=request_token, callback=callback, http_method='GET', http_url=self.client.authorization_url).to_url() return oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=request_token, http_method='GET', http_url=self.client.authorization_url).to_url()


def get_access_token(self, request_token, verifier=None): def get_access_token(self, request_token, verifier=None):
if verifier == None: if verifier == None:
Expand Down Expand Up @@ -126,7 +125,7 @@ def getConnections(self, guid=None, offset=0, limit=10000):
return False return False


def getContacts(self, offset=0, limit=10000): def getContacts(self, offset=0, limit=10000):
url = SOCIAL_API_URL + '/user/%s/contacts' % guid url = SOCIAL_API_URL + '/user/%s/contacts' % self.token.yahoo_guid
parameters = { 'format': 'json', 'view': 'tinyusercard', 'start': offset, 'count': limit } parameters = { 'format': 'json', 'view': 'tinyusercard', 'start': offset, 'count': limit }
request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='GET', http_url=url, parameters=parameters) request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='GET', http_url=url, parameters=parameters)
request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token) request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token)
Expand All @@ -136,8 +135,7 @@ def getContacts(self, offset=0, limit=10000):
return False return False


def getContact(self, contact_id): def getContact(self, contact_id):
guid = self.token.yahoo_guid url = SOCIAL_API_URL + '/user/%s/contact/%s' % (self.token.yahoo_guid, contact_id)
url = SOCIAL_API_URL + '/user/%s/contact/%s' % (guid, contact_id)
parameters = { 'format': 'json' } parameters = { 'format': 'json' }
request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='GET', http_url=url, parameters=parameters) request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='GET', http_url=url, parameters=parameters)
request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token) request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token)
Expand All @@ -147,35 +145,30 @@ def getContact(self, contact_id):
return False return False


def addContact(self, contact): def addContact(self, contact):
guid = self.token.yahoo_guid url = SOCIAL_API_URL + '/user/%s/contacts' % self.token.yahoo_guid
url = SOCIAL_API_URL + '/user/%s/contacts' % guid
parameters = { 'format': 'json' } parameters = { 'format': 'json' }
data = {'contact': contact} data = { 'contact': contact }
body = simplejson.dumps(data);
request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='POST', http_url=url, parameters=parameters) request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='POST', http_url=url, parameters=parameters)
request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token) request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token)
try: try:
return simplejson.loads(self.client.access_resource(request, body)) return simplejson.loads(self.client.access_resource(request, simplejson.dumps(data)))
except: except:
return False return False


def syncContacts(self, contact_sync): def syncContacts(self, contact_sync):
guid = self.token.yahoo_guid url = SOCIAL_API_URL + '/user/%s/contacts' % self.token.yahoo_guid
url = SOCIAL_API_URL + '/user/%s/contacts' % guid
parameters = { 'format': 'json' } parameters = { 'format': 'json' }
data = {'contactsync': contact_sync} data = { 'contactsync': contact_sync }
body = simplejson.dumps(data);
request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='PUT', http_url=url, parameters=parameters) request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='PUT', http_url=url, parameters=parameters)
request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token) request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token)
try: try:
return simplejson.loads(self.client.access_resource(request, body)) return simplejson.loads(self.client.access_resource(request, simplejson.dumps(data)))
except: except:
return False return False


def getContactSync(self, rev = 0): def getContactSync(self, revision = 0):
guid = self.token.yahoo_guid url = SOCIAL_API_URL + '/user/%s/contacts' % self.token.yahoo_guid
url = SOCIAL_API_URL + '/user/%s/contacts' % guid parameters = { 'format': 'json', 'view': 'sync', 'rev': revision }
parameters = { 'format': 'json', 'view': 'sync', 'rev': rev }
request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='GET', http_url=url, parameters=parameters) request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='GET', http_url=url, parameters=parameters)
request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token) request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token)
try: try:
Expand All @@ -195,9 +188,10 @@ def getUpdates(self, guid=None, offset=0, limit=10000):
except: except:
return False return False


def insertUpdate(self, descr, title, link, guid=None): def insertUpdate(self, title, description, link, guid=None):
if guid == None: if guid == None:
guid = self.token.yahoo_guid guid = self.token.yahoo_guid

source = "APP.%s" % self.application_id source = "APP.%s" % self.application_id
suid = 'ugc%s' % random.randrange(0, 101) suid = 'ugc%s' % random.randrange(0, 101)
parameters = { 'format': 'json' } parameters = { 'format': 'json' }
Expand All @@ -217,7 +211,7 @@ def insertUpdate(self, descr, title, link, guid=None):
"collectionID": "%s" "collectionID": "%s"
} }
] ]
}''' % (descr, suid, link, source, int(time.time()), title, guid) }''' % (description, suid, link, source, int(time.time()), title, guid)


url = "%s/user/%s/updates/%s/%s" % (SOCIAL_API_URL, guid, source, suid) url = "%s/user/%s/updates/%s/%s" % (SOCIAL_API_URL, guid, source, suid)
request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='PUT', http_url=url, parameters=parameters) request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='PUT', http_url=url, parameters=parameters)
Expand Down
16 changes: 8 additions & 8 deletions test/unit/yahoo/test_application.py
Expand Up @@ -24,7 +24,7 @@ def setUp(self):
self.oauthapp.token = self.oauthapp.refresh_access_token(self.oauthapp.token) self.oauthapp.token = self.oauthapp.refresh_access_token(self.oauthapp.token)


def test_get_request_token(self): def test_get_request_token(self):
request_token = self.oauthapp.get_request_token() request_token = self.oauthapp.get_request_token(CALLBACK_URL)
self.assertEquals('3600', request_token.expires_in) self.assertEquals('3600', request_token.expires_in)
self.assertEquals('https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token=%s' % request_token.key, request_token.request_auth_url) self.assertEquals('https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token=%s' % request_token.key, request_token.request_auth_url)


Expand All @@ -34,8 +34,8 @@ def test_refresh_access_token(self):
self.assertEquals('ECPZF7D765KTAXPDKWS7GE7CUU', self.oauthapp.token.yahoo_guid) self.assertEquals('ECPZF7D765KTAXPDKWS7GE7CUU', self.oauthapp.token.yahoo_guid)


def test_get_authorization_url(self): def test_get_authorization_url(self):
request_token = self.oauthapp.get_request_token() request_token = self.oauthapp.get_request_token(CALLBACK_URL)
self.assertEquals('https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token=%s' % request_token.key, self.oauthapp.get_authorization_url(request_token, None)) self.assertEquals(0, self.oauthapp.get_authorization_url(request_token).find('https://api.login.yahoo.com/oauth/v2/request_auth'))


def test_get_profile(self): def test_get_profile(self):
profile = self.oauthapp.getProfile() profile = self.oauthapp.getProfile()
Expand All @@ -56,12 +56,12 @@ def test_get_contacts(self):
self.assertEquals(10, contacts['count']) self.assertEquals(10, contacts['count'])


def test_get_contact(self): def test_get_contact(self):
contact = self.oauthapp.getContact(1)['contact'] contact = self.oauthapp.getContact(1)['contact']
self.assertEquals(1, contact['id']) self.assertEquals(1, contact['id'])

def test_get_contact_sync(self): def test_get_contact_sync(self):
contactsync = self.oauthapp.getContactSync(0)['contactsync'] contactsync = self.oauthapp.getContactSync(0)['contactsync']
self.assertEquals(0, contactsync['clientrev']) self.assertEquals(0, contactsync['clientrev'])


def test_get_updates(self): def test_get_updates(self):
updates = self.oauthapp.getUpdates()['updates'] updates = self.oauthapp.getUpdates()['updates']
Expand Down

0 comments on commit 537439c

Please sign in to comment.