def authenticate(self, twitter_access_token, user=None): '''authenticates the token by requesting user information from twitter''' # twitter = oauthtwitter.OAuthApi(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, twitter_access_token) twitter = oauthtwitter.TwitterOAuthClient( settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET) try: userinfo = twitter.get_user_info(twitter_access_token) except: # If we cannot get the user information, user cannot be authenticated raise screen_name = userinfo.screen_name twitter_id = userinfo.id try: user_profile = TwitterUserProfile.objects.get( screen_name=screen_name) # Update Twitter Profile user_profile.url = userinfo.url user_profile.location = userinfo.location user_profile.description = userinfo.description user_profile.profile_image_url = userinfo.profile_image_url user_profile.save() user = user_profile.user return user except TwitterUserProfile.DoesNotExist: # Create new user if not user: same_name_count = User.objects.filter( username__startswith=screen_name).count() if same_name_count: username = '******' % (screen_name, same_name_count + 1) else: username = screen_name user = User(username=username) name_data = userinfo.name.split() try: first_name, last_name = name_data[0], ' '.join( name_data[1:]) except: first_name, last_name = screen_name, '' user.first_name, user.last_name = first_name, last_name #user.email = screen_name + "@socialauth" #user.email = '*****@*****.**'%(userinfo.screen_name) user.save() user_profile = TwitterUserProfile(user=user, screen_name=screen_name) user_profile.access_token = twitter_access_token user_profile.url = userinfo.url user_profile.location = userinfo.location user_profile.description = userinfo.description user_profile.profile_image_url = userinfo.profile_image_url user_profile.save() auth_meta = AuthMeta(user=user, provider='Twitter').save() return user
def authenticate(self, access_token): '''authenticates the token by requesting user information from twitter ''' twitter = oauthtwitter.OAuthApi(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, access_token) try: userinfo = twitter.GetUserInfo() except: # If we cannot get the user information, user cannot be authenticated raise screen_name = userinfo.screen_name img_url = userinfo.profile_image_url try: user_profile = TwitterUserProfile.objects.get(screen_name = screen_name) if user_profile.user.is_active: return user_profile.user else: return except TwitterUserProfile.DoesNotExist: #Create new user same_name_count = User.objects.filter(username__startswith = screen_name).count() if same_name_count: username = '******' % (screen_name, same_name_count + 1) else: username = screen_name username = '******'+username name_count = AuthUser.objects.filter(username__startswith = username).count() if name_count: username = '******'%(username, name_count + 1) user = User(username = username) temp_password = User.objects.make_random_password(length=12) user.set_password(temp_password) name_data = userinfo.name.split() try: first_name, last_name = name_data[0], ' '.join(name_data[1:]) except: first_name, last_name = screen_name, '' user.first_name, user.last_name = first_name, last_name if img_url: img = ContentFile(urlopen(img_url).read()) name = img_url.split('/')[-1] user.picture.save(name, img, False) #user.email = '%s@twitteruser.%s.com'%(userinfo.screen_name, settings.SITE_NAME) user.save() userprofile = TwitterUserProfile(user = user, screen_name = screen_name) userprofile.access_token = access_token.key userprofile.url = userinfo.url userprofile.location = userinfo.location userprofile.description = userinfo.description userprofile.profile_image_url = userinfo.profile_image_url userprofile.save() AuthMeta(user=user, provider='Twitter').save() return user
def authenticate(self, access_token): """ authenticates the token by requesting user information from twitter """ try: api = oauthtwitter.OAuthApi(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, access_token) userinfo = api.GetUserInfo() except: # If we cannot get the user information, user cannot be authenticated raise try: user_profile = TwitterUserProfile.objects.get( screen_name=userinfo.screen_name) if user_profile.user.is_active: return user_profile.user else: return except TwitterUserProfile.DoesNotExist: #Create new user username = userinfo.screen_name user = new_user(username=username, provider='Twitter') name_data = userinfo.name.split() try: first_name, last_name = name_data[0], ' '.join(name_data[1:]) except: first_name, last_name = userinfo.screen_name, '' user.first_name, user.last_name = first_name, last_name user.save() img_url = userinfo.profile_image_url userprofile = TwitterUserProfile(user=user, screen_name=userinfo.screen_name) userprofile.access_token = access_token.key userprofile.url = userinfo.url userprofile.location = userinfo.location userprofile.description = userinfo.description userprofile.profile_image_url = userinfo.profile_image_url #user.email = '%s@Twitteruser.%s.com'%(userinfo.screen_name, settings.SITE_NAME) if img_url: img = ContentFile(urlopen(img_url).read()) name = img_url.split('/')[-1] user.picture.save(name, img, False) userprofile.save() AuthMeta(user=user, provider='Twitter').save() return user