Ejemplo n.º 1
0
def twitter_new(request):
    # get session
    twitter = get_twitter_service()
    
    # get request token
    request_token, request_token_secret = twitter.get_request_token() 

    # store token secret in session
    request.session['oauth_token_secret'] = request_token_secret

    # redirect to twitter
    redirect_url = twitter.get_authorize_url(request_token)
    return redirect(redirect_url)
Ejemplo n.º 2
0
def twitter_callback(request):
    # get/check params
    oauth_token = request.GET.get('oauth_token', None)
    oauth_verifier = request.GET.get('oauth_verifier', None)
    if not oauth_token or not oauth_verifier:
        messages.error(request, u'Ocorreu um erro ao adicionar a conta do twitter.')
        return redirect('accounts_account_list')

    # get/check token secret from session
    oauth_token_secret = request.session.get('oauth_token_secret', None)
    if not oauth_token_secret:
        messages.error(request, u'Ocorreu um erro ao adicionar a conta do twitter.')
        return redirect('accounts_account_list')

    # get session
    twitter = get_twitter_service()

    # fetch access token
    request_token, request_token_secret = twitter.get_access_token(
        oauth_token, 
        oauth_token_secret,
        data={'oauth_verifier': oauth_verifier}
    )

    # get information about user
    client = twitter.get_session(
        token=(request_token, request_token_secret)
    )
    data = client.get('account/verify_credentials.json').json()

    # create social account
    account, created = Account.objects.get_or_create(
        user=request.user, 
        provider='twitter', 
        provider_id=unicode(data['id']),
        provider_username=unicode(data['screen_name'])
    )
    account.oauth_token = request_token
    account.oauth_token_secret = request_token_secret
    account.save()

    # redirect
    messages.success(request, u'Conta do twitter adicionada com sucesso.')
    return redirect('accounts_account_list')