示例#1
0
    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
示例#2
0
    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
        '''
        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
        
        try:
            user = User.objects.get(username = '******'%screen_name )
            return user
        except User.DoesNotExist:
            user = User(username = '******'%screen_name)
            temp_password = User.objects.make_random_password(length=12)
            user.set_password(temp_password)
            user.first_name = userinfo.name
            user.email = '%s@twitteruser.%s.com'%(userinfo.screen_name, settings.SITE_NAME)
            user.save()
            userprofile = TwitterUserProfile(user = user)
            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()
            auth_meta = AuthMeta(user=user, provider='Twitter').save()
            return user
示例#4
0
    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
示例#5
0
def twitter_connect_done(request):
    token = request.session.get('token', None)
    if not token:
        return render_to_response('socialauth/twitter_connect_error.html', {'token': True})
    token = oauth.OAuthToken.from_string(token)
    if token.key != request.GET.get('oauth_token', 'no-token'):
        return render_to_response('socialauth/twitter_connect_error.html', {'mismatch': True})
    token = twitter.get_authorized_token(token)

    # Actually login
    obj = twitter.is_authorized(token)
    
    if obj is None:
        return render_to_response('socialauth/twitter_connect_error.html', {'username': True})
    try: 
        twitter_profile = TwitterUserProfile.objects.get(\
                  screen_name=obj['screen_name'])
    except: 
        twitter_profile = TwitterUserProfile(screen_name=obj['screen_name'])
    
    twitter_profile.oauth_token = request.session['token']
    twitter_profile.oauth_token_key = token.key
    twitter_profile.oauth_token_secret = token.secret
    twitter_profile.user = request.user
    twitter_profile.save()
    #del request.session['token']
    return HttpResponseRedirect(reverse('newsfeed'))
    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
示例#7
0
    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
示例#8
0
    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)
        try:
            userinfo = twitter.GetUserInfo()
        except:
            # If we cannot get the user information, user cannot be authenticated
            raise

        screen_name = userinfo.screen_name
        
        try:
            user_profile = TwitterUserProfile.objects.get(screen_name = screen_name)
            user = user_profile.user
            return user
        except TwitterUserProfile.DoesNotExist:
            # Create new user
            username = "******".format(screen_name)
            if not user:
                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
                user.email = screen_name + "@socialauth"
                #user.email = '*****@*****.**'%(userinfo.screen_name)
                user.save()
            userprofile = TwitterUserProfile(user = user, screen_name = screen_name)
            # userprofile.access_token = access_token.key
            userprofile.save()
            auth_meta = AuthMeta(user=user, provider='Twitter', 
                provider_model='TwitterUserProfile', provider_id=userprofile.pk).save()
            return user
示例#9
0
    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
    def authenticate(self, twitter_access_token):
        '''authenticates the token by requesting user information from twitter
        '''
        twitter = oauthtwitter.OAuthApi(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, twitter_access_token)
        try:
            userinfo = twitter.GetUserInfo()
        except:
            # If we cannot get the user information, user cannot be authenticated
            raise

        screen_name = userinfo.screen_name
        
        try:
            user_profile = TwitterUserProfile.objects.get(screen_name = screen_name)
            user = user_profile.user
            return user
        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
            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
            #user.email = '*****@*****.**'%(userinfo.screen_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()
            auth_meta = AuthMeta(user=user, provider='Twitter').save()
            return user