def callback_view(request): if request.GET.has_key('bounceback'): request.session['bounceback'] = request.GET['bounceback'] if request.GET.has_key("code"): code = request.GET.get('code') api = TroveAPI(TROVE['ID'], TROVE['SECRET'], TROVE['CALLBACK_URL']) oauth_token = api.get_access_token(code) else: # this might be a /add callback response (instead of a loginwith) if request.user.is_authenticated(): return __bounceback__(request) else: return redirect('trove-login') try: at = TroveAccessToken.objects.get(oauth_token_as_string=oauth_token) except TroveAccessToken.DoesNotExist: # we have to create one! -- although this does run into issues with users having different access tokens user_info = api.get_user_info() try: u = User.objects.get(username=user_info['trove_internal_id']) except User.DoesNotExist: u = User() u.username = user_info['trove_internal_id'] try: if user_info['first_name'] is not None: u.first_name = to_unicode_or_bust(user_info['first_name']) else: u.first_name = " " except: u.first_name = " " try: if user_info['last_name'] is not None: u.last_name = to_unicode_or_bust(user_info['last_name']) else: u.last_name = "" except: u.last_name = "" u.set_unusable_password() u.email = "" u.is_active = True u.save() at = TroveAccessToken() at.user = u at.oauth_token_as_string = oauth_token at.save() user = authenticate(access_token=oauth_token) if user is not None: if user.is_active: login(request, user) return __bounceback__(request) else: return redirect('trove-login') else: return redirect('trove-login')
def callback_view(request): if request.GET.has_key('bounceback'): request.session['bounceback'] = request.GET['bounceback'] if request.GET.has_key("oauth_token_secret"): oauth_token = OAuthToken.from_string("oauth_token_secret=%s&oauth_token=%s" % (request.GET['oauth_token_secret'], request.GET['oauth_token'])) api = TroveAPI(TROVE['KEY'], TROVE['SECRET'], TROVE['CONTENT_REQUESTED'], oauth_token) elif request.session.has_key('request_token'): api = TroveAPI(TROVE['KEY'], TROVE['SECRET'], TROVE['CONTENT_REQUESTED']) oauth_token = api.get_access_token(request.session['request_token']) del request.session['request_token'] else: return redirect('trove-login') try: at = TroveAccessToken.objects.get(oauth_token_as_string=oauth_token.__str__()) except TroveAccessToken.DoesNotExist: # we have to create one! -- although this does run into issues with users having different access tokens user_info = api.get_user_info() try: u = User.objects.get(username=user_info['trove_internal_id']) except User.DoesNotExist: u = User() u.username = user_info['trove_internal_id'] try: u.first_name = user_info['first_name'] except: pass try: u.last_name = user_info['last_name'] except: pass u.set_unusable_password() u.email = "" u.is_active = True u.save() at = TroveAccessToken() at.user = u at.oauth_token_as_string = oauth_token.__str__() at.save() user = authenticate(access_token=oauth_token.__str__()) if user is not None: if user.is_active: login(request, user) return __bounceback__(request) else: return redirect('trove-login') else: return redirect('trove-login')
def home(request): template = 'welcome.html' context_vars = {} context = None if request.user.is_authenticated(): return get_started_or_go_home(request) else: api = TroveAPI(settings.TROVE_APP_KEY, settings.TROVE_APP_SECRET, ['photos']) if request.GET: try: oauth_token_key = request.GET['oauth_token'] request_token = request.session['trove_request_token'] oauth_token = api.get_access_token(request_token) user = authenticate(oauth_token = oauth_token) if user is not None: if user.is_active: login(request, user) return get_started_or_go_home(request) else: # Return a 'disabled account' error message pass else: # TODO FIXME failed login pass except KeyError: pass else: try: request_token = request.session['trove_request_token'] except: request_token = api.get_request_token() request.session['trove_request_token']=request_token url = api.get_authorization_url(request_token) context_vars['trove_connect_url']=url context = RequestContext(request,context_vars) return render_to_response(template,context,mimetype="text/html")
# You need a consumer. Make one in the db. Consumers have call back URLs, which are not overridable (a-la Twitter and Fb) c = { client_id: 'some_key_here', secret: 'sssh, it\'s a secret!', redirect_uri: 'http://redirecturl.dev/callback' } # Initialize the TroveAPI with the consumer's key and secret api = TroveAPI(client_id, secret, redirect_uri) url = api.get_authenticate_url() print url raw_input() # Returns back the URL to redirect the user (Looks like 'http://brooklyn.vlku.com:8000/multi/login?next=/oauth/2authorize/%3Foauth_token%3DdtKaXn5JXRWNndTa') # callback URL includes a "code" that is passed in via the code parameter. We use that to get an access_token. at = api.get_access_token(code) # This returns back the access_token (at) as an OAuthToken. You want to save this somewhere for reuse later # access_token also conveniently makes this the access_token for the instantiated API # so you can call this easily right away with no setup results = api.get_photos() # but if you have to initialize a fresh TroveAPI (say for another hit with the token from the DB:) api = TroveAPI(c.key, c.secret, ['photo'], access_token=at) results = api.get_photos()
key: 'some_key_here', secret: 'sssh, it\'s a secret!' } # Initialize the TroveAPI with the consumer's key and secret api = TroveAPI(c.key, c.secret, ['photo']) rt = api.get_request_token() # We get the request token -- returns back an 'OAuthToken' url = api.get_authorization_url(rt) print url raw_input() # Returns back the URL to redirect the user (Looks like 'http://brooklyn.vlku.com:8000/multi/login?next=/oauth/authorize/%3Foauth_token%3DdtKaXn5JXRWNndTa') # callback URL AUTHORIZES (SAME) token and redirects to a page for you to act on token to get access token # to promote a request token to an access token call this method at = api.get_access_token(rt) # This returns back the access_token (at) as an OAuthToken. You want to save this somewhere for reuse later # access_token also conveniently makes this the access_token for the instantiated API # so you can call this easily right away with no setup results = api.get_photos() # but if you have to initialize a fresh TroveAPI (say for another hit with the token from the DB:) api = TroveAPI(c.key, c.secret, ['photo'], access_token=at) results = api.get_photos()