def twitter_connect(request): twitter_consumer_key = settings.TWITTER_CONSUMER_KEY twitter_consumer_secret = settings.TWITTER_CONSUMER_SECRET oauth_token = request.GET.get('oauth_token') oauth_verifier = request.GET.get('oauth_verifier') denied = request.GET.get('denied') if denied: logging.user(request, "~BB~FRDenied Twitter connect") return {'error': 'Denied! Try connecting again.'} elif oauth_token and oauth_verifier: try: auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret) auth.request_token = request.session['twitter_request_token'] # auth.set_request_token(oauth_token, oauth_verifier) auth.get_access_token(oauth_verifier) api = tweepy.API(auth) twitter_user = api.me() except (tweepy.TweepError, IOError) as e: logging.user(request, "~BB~FRFailed Twitter connect: %s" % e) return dict( error="Twitter has returned an error. Try connecting again.") # Be sure that two people aren't using the same Twitter account. existing_user = MSocialServices.objects.filter( twitter_uid=str(twitter_user.id)) if existing_user and existing_user[0].user_id != request.user.pk: try: user = User.objects.get(pk=existing_user[0].user_id) logging.user( request, "~BB~FRFailed Twitter connect, another user: %s" % user.username) return dict(error=( "Another user (%s, %s) has " "already connected with those Twitter credentials." % (user.username, user.email or "no email"))) except User.DoesNotExist: existing_user.delete() social_services = MSocialServices.get_user(request.user.pk) social_services.twitter_uid = str(twitter_user.id) social_services.twitter_access_key = auth.access_token social_services.twitter_access_secret = auth.access_token_secret social_services.syncing_twitter = True social_services.save() SyncTwitterFriends.delay(user_id=request.user.pk) logging.user(request, "~BB~FRFinishing Twitter connect") return {} else: # Start the OAuth process auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret) auth_url = auth.get_authorization_url() request.session['twitter_request_token'] = auth.request_token logging.user(request, "~BB~FRStarting Twitter connect: %s" % auth.request_token) return {'next': auth_url}
def twitter_connect(request): twitter_consumer_key = settings.TWITTER_CONSUMER_KEY twitter_consumer_secret = settings.TWITTER_CONSUMER_SECRET oauth_token = request.REQUEST.get('oauth_token') oauth_verifier = request.REQUEST.get('oauth_verifier') denied = request.REQUEST.get('denied') if denied: logging.user(request, "~BB~FRDenied Twitter connect") return {'error': 'Denied! Try connecting again.'} elif oauth_token and oauth_verifier: try: auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret) auth.set_request_token(oauth_token, oauth_verifier) access_token = auth.get_access_token(oauth_verifier) api = tweepy.API(auth) twitter_user = api.me() except (tweepy.TweepError, IOError): logging.user(request, "~BB~FRFailed Twitter connect") return dict(error="Twitter has returned an error. Try connecting again.") # Be sure that two people aren't using the same Twitter account. existing_user = MSocialServices.objects.filter(twitter_uid=unicode(twitter_user.id)) if existing_user and existing_user[0].user_id != request.user.pk: try: user = User.objects.get(pk=existing_user[0].user_id) logging.user(request, "~BB~FRFailed Twitter connect, another user: %s" % user.username) return dict(error=("Another user (%s, %s) has " "already connected with those Twitter credentials." % (user.username, user.email or "no email"))) except User.DoesNotExist: existing_user.delete() social_services, _ = MSocialServices.objects.get_or_create(user_id=request.user.pk) social_services.twitter_uid = unicode(twitter_user.id) social_services.twitter_access_key = access_token.key social_services.twitter_access_secret = access_token.secret social_services.syncing_twitter = True social_services.save() SyncTwitterFriends.delay(user_id=request.user.pk) logging.user(request, "~BB~FRFinishing Twitter connect") return {} else: # Start the OAuth process auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret) auth_url = auth.get_authorization_url() logging.user(request, "~BB~FRStarting Twitter connect") return {'next': auth_url}