def register(request): if request.method == 'POST': form = FundRaiserRegistration(request.POST) if form.is_valid(): # partially create user, and store unauthed token user = _user_from_reg_form(form) try: profile = _profile_from_reg_form(form, user) except ValueError: return HttpResponse('that page is already registered') request.session['user_id'] = user.pk handler = OAuthHandler(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET, callback='http://www.justgivingthanks.com/callback/', secure=True) auth_url = handler.get_authorization_url() request.session['unauthed_token'] = handler.request_token.to_string() print 'created user, got token, redirecting to %s' % (auth_url) return HttpResponseRedirect(auth_url) else: form = FundRaiserRegistration() # An unbound form return render_to_response('registration.html', {'form': form}, context_instance=RequestContext(request))
def cmd_get_auth_url(bot, update, chat): auth = OAuthHandler(bot.tw.auth.consumer_key, bot.tw.auth.consumer_secret) auth_url = auth.get_authorization_url() chat.twitter_request_token = json.dumps(auth.request_token) chat.save() msg = "go to [this url]({}) and send me your verifier code using /verify code" bot.reply(update, msg.format(auth_url), parse_mode=telegram.ParseMode.MARKDOWN)
def get_auth_url(self, params={}, callback_url=None): if callback_url is None: callback_url = self._callback_url callback = '%s?%s' % (callback_url, urllib.urlencode(params)) auth = OAuthHandler(self._consumer_key, self._consumer_secret, callback) url = auth.get_authorization_url() return url, auth.request_token
def cmd_get_auth_url(update: telegram.Update, context: CallbackContext) -> None: bot = context.bot chat, _created = TelegramChat.get_or_create( chat_id=update.message.chat.id, tg_type=update.message.chat.type, ) auth = OAuthHandler(bot.tw.auth.consumer_key, bot.tw.auth.consumer_secret) auth_url = auth.get_authorization_url() chat.twitter_request_token = json.dumps(auth.request_token) chat.save() msg = "go to [this url]({}) and send me your verifier code using /verify code" bot.reply(update, msg.format(auth_url), parse_mode=telegram.ParseMode.MARKDOWN)
def login(): auth = OAuthHandler(CONSUMER_TOKEN, CONSUMER_SECRET, CALLBACK_URL) try: #get the request tokens redirect_url = auth.get_authorization_url(signin_with_twitter=True) print(redirect_url) session_t['request_token'] = auth.request_token print('se guardo en la sesion') if 'request_token' in session_t: user = session_t['request_token'] print(user) except tweepy.TweepError: print('Error! Failed to get request token') #this is twitter's url for authentication # session['request_token'] = auth.request_token return flask.redirect(redirect_url)
def get(self): #Set up our twitter auth object config = self.app.config['twitter'] auth = OAuthHandler(config['consumer_key'], config['consumer_secret'], self.request.host_url + '/auth') #Check the session state. If it contains a twitter token, #The user has already gone through the authorization step tkn = self.session.get('twitter') if tkn: #If we are on the second phase already auth.set_request_token(tkn[0], tkn[1]) del self.session['twitter'] verifier = self.request.get('oauth_verifier') if verifier: #Get the verification code from the URL auth.get_access_token(verifier) me = API(auth).me() #See if we already have a user that has this id. results = User.query().filter(User.twitter_id == me.id).fetch() if results: user = results[0] #Make sure all the properties are up-to-date user.name = me.name user.location = me.location user.put() else: user = User(twitter_id=me.id, name=me.name, location=me.location) user.put() #The user_id should be part of the session self.session['user'] = user.key.id() self.redirect('/') else: #Grabs request_tokens and creates a URL for a redirect redirect_url = auth.get_authorization_url(signin_with_twitter=True) #Store the request_token information for the next step self.session['twitter'] = (auth.request_token.key, auth.request_token.secret) self.redirect(redirect_url)
def get_template_state_for_user(self): _template_values = {} user = users.get_current_user() user_model = None if user: logging.info("user: %s",user.nickname) social_users = model.SocialKeysForUsers.all() social_users.filter("user_id =",user.user_id()) user_model = social_users.get() if not user_model == None: if user_model.access_token_key and user_model.access_token_secret: _template_values["needs_twitter_auth"] = False auth = OAuthHandler(social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET) auth.set_access_token(user_model.access_token_key,user_model.access_token_secret) api = API(auth) api_is_working = api.test() if not api_is_working: logging.warning("api is NOT working: %s",api_is_working) try: twitter_user = memcache.get("twitter_user:%s" % user.user_id()) if twitter_user == None: twitter_user = api.me() memcache.add("twitter_user:%s" % user.user_id(), twitter_user, 60) logging.info(twitter_user) _template_values["twitter_user"] = twitter_user except TweepError: logging.error( "TweepError error has occured, clearing access tokents") user_model.access_token_key = None user_model.access_token_secret = None user_model.put() _template_values["needs_twitter_auth"] = True else: _template_values["needs_twitter_auth"] = True else: _template_values["needs_twitter_auth"] = True user_model = model.SocialKeysForUsers(user_id=user.user_id()) user_model.put() else: _template_values["needs_twitter_auth"] = True logging.warning("user is empty") redirect_url = None if _template_values["needs_twitter_auth"] and not user_model == None: auth = OAuthHandler(social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET,"https://%s/callback" % self.request.host) redirect_url = auth.get_authorization_url() user_model.request_token_key = auth.request_token.key user_model.request_token_secret = auth.request_token.secret user_model.put() _template_values["redirect_url"] = redirect_url _template_values["logout_url"] = users.create_logout_url("/") _template_values["login_url"] = users.create_login_url("/") _template_values["user"] = user _template_values["user_model"] = user_model util = helpers.Util() _template_values["next_mix_run_time"] = util.get_next_mix_runtime() _template_values["current_server_time"] = util.get_current_time_for_show() return _template_values
def get_template_state_for_user(self): _template_values = {} user = users.get_current_user() user_model = None if user: logging.info("user: %s", user.nickname) social_users = model.SocialKeysForUsers.all() social_users.filter("user_id =", user.user_id()) user_model = social_users.get() if not user_model == None: if user_model.access_token_key and user_model.access_token_secret: _template_values["needs_twitter_auth"] = False auth = OAuthHandler(social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET) auth.set_access_token(user_model.access_token_key, user_model.access_token_secret) api = API(auth) api_is_working = api.test() if not api_is_working: logging.warning("api is NOT working: %s", api_is_working) try: twitter_user = memcache.get("twitter_user:%s" % user.user_id()) if twitter_user == None: twitter_user = api.me() memcache.add("twitter_user:%s" % user.user_id(), twitter_user, 60) logging.info(twitter_user) _template_values["twitter_user"] = twitter_user except TweepError: logging.error( "TweepError error has occured, clearing access tokents" ) user_model.access_token_key = None user_model.access_token_secret = None user_model.put() _template_values["needs_twitter_auth"] = True else: _template_values["needs_twitter_auth"] = True else: _template_values["needs_twitter_auth"] = True user_model = model.SocialKeysForUsers(user_id=user.user_id()) user_model.put() else: _template_values["needs_twitter_auth"] = True logging.warning("user is empty") redirect_url = None if _template_values["needs_twitter_auth"] and not user_model == None: auth = OAuthHandler(social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET, "https://%s/callback" % self.request.host) redirect_url = auth.get_authorization_url() user_model.request_token_key = auth.request_token.key user_model.request_token_secret = auth.request_token.secret user_model.put() _template_values["redirect_url"] = redirect_url _template_values["logout_url"] = users.create_logout_url("/") _template_values["login_url"] = users.create_login_url("/") _template_values["user"] = user _template_values["user_model"] = user_model util = helpers.Util() _template_values["next_mix_run_time"] = util.get_next_mix_runtime() _template_values[ "current_server_time"] = util.get_current_time_for_show() return _template_values