diff --git a/README.markdown b/README.markdown index b2ccb92..ae4b8da 100644 --- a/README.markdown +++ b/README.markdown @@ -104,10 +104,10 @@ Examples oauthapp = yahoo.application.OAuthApplication(CONSUMER_KEY, CONSUMER_SECRET, APPLICATION_ID, CALLBACK_URL) # Fetch request token - request_token = oauthapp.get_request_token() + request_token = oauthapp.get_request_token(CALLBACK_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 access_token = oauthapp.get_access_token(request_token) @@ -120,6 +120,16 @@ Examples 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 ===== diff --git a/examples/appengine/djangodemo/yosdemo/models.py b/examples/appengine/djangodemo/yosdemo/models.py index 1d2da2b..0f1e672 100755 --- a/examples/appengine/djangodemo/yosdemo/models.py +++ b/examples/appengine/djangodemo/yosdemo/models.py @@ -1,11 +1,5 @@ -# Create your models here. - # models.py from django.db import models from google.appengine.ext import db - -#class Visitor(db.Model): -# ip = db.StringProperty() -# added_on = db.DateTimeProperty(auto_now_add=True) diff --git a/examples/appengine/djangodemo/yosdemo/tests.py b/examples/appengine/djangodemo/yosdemo/tests.py deleted file mode 100755 index 9cf5d7e..0000000 --- a/examples/appengine/djangodemo/yosdemo/tests.py +++ /dev/null @@ -1,27 +0,0 @@ -# Create your tests here. - -# tests.py - -""" -This file demonstrates two different styles of tests (one doctest and one -unittest). These will both pass when you run "manage.py test". - -Replace these with more appropriate tests for your application. -""" - -from django.test import TestCase - -class SimpleTest(TestCase): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.failUnlessEqual(1 + 1, 2) - -__test__ = {"doctest": """ -Another way to test that 1 + 1 is equal to 2. - ->>> 1 + 1 == 2 -True -"""} - diff --git a/examples/appengine/djangodemo/yosdemo/views.py b/examples/appengine/djangodemo/yosdemo/views.py index 431f759..5ecee23 100755 --- a/examples/appengine/djangodemo/yosdemo/views.py +++ b/examples/appengine/djangodemo/yosdemo/views.py @@ -1,24 +1,9 @@ -# Create your views here. - # views.py from django.http import HttpResponse -# from mashname.main.models import Visitor - def main(request): - #visitor = Visitor() - #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 .........................................................." + result = "demo .........................................................." - return HttpResponse(result) + return HttpResponse(result) diff --git a/examples/appengine/yosdemo.py b/examples/appengine/yosdemo.py index 7ce4ee3..560e13b 100644 --- a/examples/appengine/yosdemo.py +++ b/examples/appengine/yosdemo.py @@ -86,7 +86,7 @@ def get(self): session.save() # 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: diff --git a/src/yahoo/application.py b/src/yahoo/application.py index e5703e3..c5f3ab8 100644 --- a/src/yahoo/application.py +++ b/src/yahoo/application.py @@ -46,7 +46,7 @@ class OAuthApplicationException(Exception): 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() @@ -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() # oauth standard apis - def get_request_token(self): - # self.options['lang'] - parameters = { 'xoauth_lang_pref': 'en' } + def get_request_token(self, callback = 'oob'): + parameters = { 'xoauth_lang_pref': self.options['lang'], 'oauth_callback': callback } 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) return self.client.fetch_request_token(request) - def get_authorization_url(self, request_token, callback): - return oauthlib.oauth.OAuthRequest.from_token_and_callback(token=request_token, callback=callback, http_method='GET', http_url=self.client.authorization_url).to_url() + def get_authorization_url(self, request_token): + 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): if verifier == None: @@ -126,7 +125,7 @@ def getConnections(self, guid=None, offset=0, limit=10000): return False 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 } 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) @@ -136,8 +135,7 @@ def getContacts(self, offset=0, limit=10000): return False def getContact(self, contact_id): - guid = self.token.yahoo_guid - url = SOCIAL_API_URL + '/user/%s/contact/%s' % (guid, contact_id) + url = SOCIAL_API_URL + '/user/%s/contact/%s' % (self.token.yahoo_guid, contact_id) 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.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token) @@ -147,35 +145,30 @@ def getContact(self, contact_id): return False def addContact(self, contact): - guid = self.token.yahoo_guid - url = SOCIAL_API_URL + '/user/%s/contacts' % guid + url = SOCIAL_API_URL + '/user/%s/contacts' % self.token.yahoo_guid parameters = { 'format': 'json' } - data = {'contact': contact} - body = simplejson.dumps(data); + data = { 'contact': contact } 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) try: - return simplejson.loads(self.client.access_resource(request, body)) + return simplejson.loads(self.client.access_resource(request, simplejson.dumps(data))) except: return False def syncContacts(self, contact_sync): - guid = self.token.yahoo_guid - url = SOCIAL_API_URL + '/user/%s/contacts' % guid + url = SOCIAL_API_URL + '/user/%s/contacts' % self.token.yahoo_guid parameters = { 'format': 'json' } - data = {'contactsync': contact_sync} - body = simplejson.dumps(data); + data = { 'contactsync': contact_sync } 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) try: - return simplejson.loads(self.client.access_resource(request, body)) + return simplejson.loads(self.client.access_resource(request, simplejson.dumps(data))) except: return False - def getContactSync(self, rev = 0): - guid = self.token.yahoo_guid - url = SOCIAL_API_URL + '/user/%s/contacts' % guid - parameters = { 'format': 'json', 'view': 'sync', 'rev': rev } + def getContactSync(self, revision = 0): + url = SOCIAL_API_URL + '/user/%s/contacts' % self.token.yahoo_guid + parameters = { 'format': 'json', 'view': 'sync', 'rev': revision } 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) try: @@ -195,9 +188,10 @@ def getUpdates(self, guid=None, offset=0, limit=10000): except: return False - def insertUpdate(self, descr, title, link, guid=None): + def insertUpdate(self, title, description, link, guid=None): if guid == None: guid = self.token.yahoo_guid + source = "APP.%s" % self.application_id suid = 'ugc%s' % random.randrange(0, 101) parameters = { 'format': 'json' } @@ -217,7 +211,7 @@ def insertUpdate(self, descr, title, link, guid=None): "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) request = oauthlib.oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_method='PUT', http_url=url, parameters=parameters) diff --git a/test/unit/yahoo/test_application.py b/test/unit/yahoo/test_application.py index cfa82b9..c0b5394 100755 --- a/test/unit/yahoo/test_application.py +++ b/test/unit/yahoo/test_application.py @@ -24,7 +24,7 @@ def setUp(self): self.oauthapp.token = self.oauthapp.refresh_access_token(self.oauthapp.token) 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('https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token=%s' % request_token.key, request_token.request_auth_url) @@ -34,8 +34,8 @@ def test_refresh_access_token(self): self.assertEquals('ECPZF7D765KTAXPDKWS7GE7CUU', self.oauthapp.token.yahoo_guid) def test_get_authorization_url(self): - request_token = self.oauthapp.get_request_token() - 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)) + request_token = self.oauthapp.get_request_token(CALLBACK_URL) + 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): profile = self.oauthapp.getProfile() @@ -56,12 +56,12 @@ def test_get_contacts(self): self.assertEquals(10, contacts['count']) def test_get_contact(self): - contact = self.oauthapp.getContact(1)['contact'] - self.assertEquals(1, contact['id']) - + contact = self.oauthapp.getContact(1)['contact'] + self.assertEquals(1, contact['id']) + def test_get_contact_sync(self): - contactsync = self.oauthapp.getContactSync(0)['contactsync'] - self.assertEquals(0, contactsync['clientrev']) + contactsync = self.oauthapp.getContactSync(0)['contactsync'] + self.assertEquals(0, contactsync['clientrev']) def test_get_updates(self): updates = self.oauthapp.getUpdates()['updates']