Ejemplo n.º 1
0
    def authenticate(self, linkedin_access_token, user=None):
        linkedin = LinkedIn(LINKEDIN_CONSUMER_KEY, LINKEDIN_CONSUMER_SECRET)
        # get their profile

        profile = (ProfileApi(linkedin).getMyProfile(
            access_token=linkedin_access_token))

        try:
            user_profile = (LinkedInUserProfile.objects.get(
                linkedin_uid=profile.id))
            user = user_profile.user
            return user
        except LinkedInUserProfile.DoesNotExist:
            # Create a new user
            username = '******' % profile.id

            if not user:
                user = User(username=username)
                user.first_name, user.last_name = (profile.firstname,
                                                   profile.lastname)
                user.email = '%s@socialauth' % (username)
                user.save()

            userprofile = LinkedInUserProfile(user=user,
                                              linkedin_uid=profile.id)
            userprofile.save()

            auth_meta = AuthMeta(user=user, provider='LinkedIn').save()
            return user