def user_login(service, cust_callback_url=None): """Create RequestToken to auth on user return and redirect user to third party url for auth.""" try: oauth = _oauth(service, cust_callback_url) redirect_url = oauth.get_authorization_url() # Make sure we have an auth container if not service.auth: auth_handler = OAuth() else: auth_handler = service.auth # Create a new requesttoken new_rt = RequestToken() new_rt.oauth_token = oauth.request_token.key new_rt.oauth_token_secret = oauth.request_token.secret new_rt.save() auth_handler.request_token = new_rt auth_handler.save() if not service.auth: service.auth = auth_handler service.save() except tweepy.TweepError: return False # Redirect user to Twitter to authorize return redirect(redirect_url)
def user_login(service, cust_callback_url=None): """Create RequestToken to auth on user return and redirect user to third party url for auth.""" oauth = service.app.oauth try: auth = _auth(oauth, cust_callback_url) redirect_url = auth.get_authorization_url() # check if we have an existing RequestToken # if so delete it. rt = RequestToken.objects.filter(service=service) if rt: rt.delete() # create a new requesttoken new_rt = RequestToken() new_rt.service = service new_rt.oauth_token = auth.request_token.key new_rt.oauth_token_secret = auth.request_token.secret new_rt.created = datetime.now() new_rt.save() except tweepy.TweepError: return False # Redirect user to Twitter to authorize return redirect(redirect_url)
def auth(request): """Prepare a oauth request by saving a record locally ready for the redirect from twitter.""" service = get_model_instance(request.user, PACKAGE) if not service.auth: auth_handler = OAuth() else: auth_handler = service.auth current_site = Site.objects.get(id=settings.SITE_ID) callback = reverse('causal-flickr-callback') callback = "http://%s%s" % (current_site.domain, callback,) consumer = Consumer(service.app.auth_settings['api_key'], service.app.auth_settings['api_secret']) client = Client(consumer) resp, content = client.request(service.app.auth_settings['request_url'], "POST", body='oauth_callback=%s' % (callback), headers={'Content-Type' :'application/x-www-form-urlencoded'}) oauth_callback_confirmed, oauth_token, oauth_token_secret = content.split('&') new_rt = RequestToken() new_rt.oauth_token = oauth_token.split('=')[1] new_rt.oauth_token_secret = oauth_token_secret.split('=')[1] new_rt.save() auth_handler.request_token = new_rt auth_handler.save() if not service.auth: service.auth = auth_handler service.save() #http://www.flickr.com/services/oauth/authorize return redirect('http://www.flickr.com/services/oauth/authorize?oauth_token=%s&perms=read' %(oauth_token.split('=')[1]))