Exemplo n.º 1
0
def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
    """A user gets redirected here after hitting Twitter and authorizing your app to use their data.

    This is the view that stores the tokens you want
    for querying data. Pay attention to this.

    """
    # Now that we've got the magic tokens back from Twitter, we need to exchange
    # for permanent ones and store them...
    oauth_token = request.session['request_token']['oauth_token']
    oauth_token_secret = request.session['request_token']['oauth_token_secret']
    twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET,
                      oauth_token, oauth_token_secret)

    # Retrieve the tokens we want...
    authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])

    # If they already exist, grab them, login and redirect to a page displaying stuff.
    try:
        user = User.objects.get(username=authorized_tokens['screen_name'])
    except User.DoesNotExist:
        # We mock a creation here; no email, password is just the token, etc.
        user = User.objects.create_user(authorized_tokens['screen_name'], "*****@*****.**", authorized_tokens['oauth_token_secret'])
        profile = TwitterProfile()
        profile.user = user
        profile.oauth_token = authorized_tokens['oauth_token']
        profile.oauth_secret = authorized_tokens['oauth_token_secret']
        profile.save()

    user = authenticate(
        username=authorized_tokens['screen_name'],
        password=authorized_tokens['oauth_token_secret']
    )
    login(request, user)
    return HttpResponseRedirect(redirect_url)
Exemplo n.º 2
0
def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
    # tokens obtained from Twitter
    oauth_token = request.session['request_token']['oauth_token']
    oauth_token_secret = request.session['request_token']['oauth_token_secret']
    twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET,
                      oauth_token, oauth_token_secret)

    # retrieving tokens
    authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])

    # if tokens already exist, use them to login and redirect to the battle
    try:
        user = User.objects.get(username=authorized_tokens['screen_name'])
        user.set_password(authorized_tokens['oauth_token_secret'])
        user.save()
    except User.DoesNotExist:
        # mock a creation here; no email, password is just the token, etc. since no need for users in this app
        user = User.objects.create_user(authorized_tokens['screen_name'], "*****@*****.**", authorized_tokens['oauth_token_secret'])
        profile = TwitterProfile()
        profile.user = user
        profile.oauth_token = authorized_tokens['oauth_token']
        profile.oauth_secret = authorized_tokens['oauth_token_secret']
        profile.save()

    user = authenticate(
        username=authorized_tokens['screen_name'],
        password=authorized_tokens['oauth_token_secret']
    )

    login(request, user)
    return HttpResponseRedirect(redirect_url)
Exemplo n.º 3
0
def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
    """A user gets redirected here after hitting Twitter and authorizing your app to use their data.

    This is the view that stores the tokens you want
    for querying data. Pay attention to this.

    """
    # Now that we've got the magic tokens back from Twitter, we need to exchange
    # for permanent ones and store them...
    oauth_token = request.session['request_token']['oauth_token']
    oauth_token_secret = request.session['request_token']['oauth_token_secret']
    twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET,
                      oauth_token, oauth_token_secret)

    # Retrieve the tokens we want...
    authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])

    username = authorized_tokens['screen_name']
    # If they already exist, grab them, login and redirect to a page displaying stuff.
    try:
        user = TwitterProfile.objects.get(screen_name=authorized_tokens['screen_name']).user
    except TwitterProfile.DoesNotExist:
        # We mock a creation here; no email, password is just the token, etc.
        res = User.objects.filter(username=username)
        import random
        while res.count() > 0:
            num = random.randint(1, 90000)
            res = User.objects.filter(username="******" % (username, num))
            if res.count() == 0:
                username = "******" % (username, num)
        default_email = settings.TWYTHON_DJANGO_DEFAULT_EMAIL if hasattr(settings, 'TWYTHON_DJANGO_DEFAULT_EMAIL') \
            else "*****@*****.**"
        user = User.objects.create_user(username, default_email, authorized_tokens['oauth_token_secret'])
        profile = TwitterProfile()
        profile.user = user
        profile.screen_name = authorized_tokens['screen_name']
        profile.oauth_token = authorized_tokens['oauth_token']
        profile.oauth_secret = authorized_tokens['oauth_token_secret']
        profile.save()

    #user = authenticate(
    #    username=authorized_tokens['screen_name'],
    #    password=authorized_tokens['oauth_token_secret']
    #)
    user.backend = 'django.contrib.auth.backends.ModelBackend'
    login(request, user)
    return HttpResponseRedirect(redirect_url)
Exemplo n.º 4
0
def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
    """
        A user gets redirected here after hitting Twitter and authorizing your
        app to use their data.

        ***
            This is the view that stores the tokens you want
            for querying data. Pay attention to this.
        ***
    """
    # Now that we've got the magic tokens back from Twitter, we need to exchange
    # for permanent ones and store them...
    twitter = Twython(
        twitter_token=settings.TWITTER_KEY,
        twitter_secret=settings.TWITTER_SECRET,
        oauth_token=request.session['request_token']['oauth_token'],
        oauth_token_secret=request.session['request_token']['oauth_token_secret'],
    )

    # Retrieve the tokens we want...
    authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
    user_id = int(authorized_tokens['user_id'])

    try:
        profile = TwitterProfile.objects.get(twitter_user_id=user_id)
        profile.oauth_token = authorized_tokens['oauth_token']
        profile.oauth_secret = authorized_tokens['oauth_token_secret']
        profile.screen_name = authorized_tokens['screen_name']
        profile.save()
    except TwitterProfile.DoesNotExist:
        profile = TwitterProfile()
        profile.user = request.user
        profile.oauth_token = authorized_tokens['oauth_token']
        profile.oauth_secret = authorized_tokens['oauth_token_secret']
        profile.screen_name = authorized_tokens['screen_name']
        profile.twitter_user_id = authorized_tokens['user_id']
        profile.save()

    return HttpResponseRedirect(reverse(redirect_url))