예제 #1
0
def facebook_new(request):
    # get session
    facebook = get_facebook_service()

    # redirect to dialog
    params = {
        'redirect_uri': FACEBOOK_CALLBACK_URL
    }
    authorization_url = facebook.get_authorize_url(**params)
    return redirect(authorization_url)
예제 #2
0
def facebook_callback(request):
    # get/check params
    redirect_uri = FACEBOOK_CALLBACK_URL
    code = request.GET.get('code', None)
    if not code:
        messages.error(request, u'Ocorreu um erro ao adicionar a conta do facebook.')
        return redirect('accounts_account_list')

    # get session
    facebook = get_facebook_service()

    # fetch tokens
    data = {'code':code, 'redirect_uri':redirect_uri}
    r = facebook.get_raw_access_token(data=data)
    credentials = parse_qs(r.content)
    access_token = credentials.get('access_token')[0]
    expires = credentials.get('expires')[0]

    # get information about user
    client = facebook.get_session(token=access_token)
    me = client.get('me').json()

    # create new social account
    account, created = Account.objects.get_or_create(
        user=request.user,
        provider='facebook', 
        provider_id=me['id'],
        provider_username=me['username']
    )
    account.oauth_token = access_token
    account.expires_in = datetime.now() + timedelta(seconds=int(expires))
    account.save()

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