本文整理匯總了Python中voter.models.VoterManager.retrieve_voter_by_twitter_request_token方法的典型用法代碼示例。如果您正苦於以下問題:Python VoterManager.retrieve_voter_by_twitter_request_token方法的具體用法?Python VoterManager.retrieve_voter_by_twitter_request_token怎麽用?Python VoterManager.retrieve_voter_by_twitter_request_token使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類voter.models.VoterManager
的用法示例。
在下文中一共展示了VoterManager.retrieve_voter_by_twitter_request_token方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: process_sign_in_response_view
# 需要導入模塊: from voter.models import VoterManager [as 別名]
# 或者: from voter.models.VoterManager import retrieve_voter_by_twitter_request_token [as 別名]
def process_sign_in_response_view(request):
oauth_token = request.GET.get('oauth_token', '')
oauth_verifier = request.GET.get('oauth_verifier', '')
if not positive_value_exists(oauth_token) or not positive_value_exists(oauth_verifier):
# Redirect back to ReactJS so we can display failure message
return HttpResponseRedirect('http://localhost:3001/twitter/missing_variables') # TODO Convert to env variable
voter_manager = VoterManager()
# Look in the Voter table for a matching request_token, placed by the API endpoint twitterSignInStart
results = voter_manager.retrieve_voter_by_twitter_request_token(oauth_token)
if not results['voter_found']:
# Redirect back to ReactJS so we can display failure message if the token wasn't found
return HttpResponseRedirect('http://localhost:3001/twitter/token_missing') # TODO Convert to env variable
voter = results['voter']
# Fetch the access token
try:
# Set up a tweepy auth handler
auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
auth.request_token = {'oauth_token': voter.twitter_request_token,
'oauth_token_secret': voter.twitter_request_secret}
auth.get_access_token(oauth_verifier)
if not positive_value_exists(auth.access_token) or not positive_value_exists(auth.access_token_secret):
# Redirect back with error
return HttpResponseRedirect('http://localhost:3001/twitter/access_token_missing') # TODO Convert to env var
voter.twitter_access_token = auth.access_token
voter.twitter_access_secret = auth.access_token_secret
voter.save()
# Next use the access_token and access_secret to retrieve Twitter user info
api = tweepy.API(auth)
tweepy_user_object = api.me()
voter_manager.save_twitter_user_values(voter, tweepy_user_object)
except tweepy.RateLimitError:
success = False
status = 'TWITTER_RATE_LIMIT_ERROR'
except tweepy.error.TweepError as error_instance:
success = False
status = ''
error_tuple = error_instance.args
for error_dict in error_tuple:
for one_error in error_dict:
status += '[' + one_error['message'] + '] '
# Redirect back, success
return HttpResponseRedirect('http://localhost:3001/ballot') # TODO Convert to env var