Ejemplo n.º 1
0
def login_facebook(request):
    if request.GET.get('error'):
        messages.error(request, '사용자가 페이스북 로그인을 취소했습니다')
        return redirect('member:login')

    if request.GET.get('code'):
        # 사용자가 페이스북로그인 버튼을 눌러 페이스북에서 로그인에 성공했을 경우,
        # 페이스북에서 mysite로 access_token을 요청할 수 있는 'code'값을 보내준다

        REDIRECT_URL = 'http://127.0.0.1:8000/member/login/facebook/'
        code = request.GET.get('code')

        access_token = facebook.get_access_token(code, REDIRECT_URL)
        user_id = facebook.get_user_id_from_token(access_token)
        user_info = facebook.get_user_info(user_id, access_token)

        # authenticate backends에 FacebookBackend추가해서 dict_user_info객체로 로그인 가능
        user = auth_authenticate(user_info=user_info)
        if user is not None:
            auth_login(request, user)
            messages.success(request, '페이스북 유저로 로그인 되었습니다')
            return redirect('blog:post_list')
        else:
            messages.error(request, '페이스북 로그인에 실패하였습니다')
            return redirect('member:login')
Ejemplo n.º 2
0
def login_facebook(request):
    if request.GET.get('error'):
        messages.error(request, '사용자가 페이스북 로그인을 취소했습니다')
        return redirect('member:login')

    if request.GET.get('code'):
        # 사용자가 페이스북로그인 버튼을 눌러 페이스북에서 로그인에 성공했을 경우,
        # 페이스북에서 mysite로 access_token을 요청할 수 있는 'code'값을 보내준다

        REDIRECT_URL = 'http://127.0.0.1:8000/member/login/facebook/'
        code = request.GET.get('code')

        access_token = facebook.get_access_token(code, REDIRECT_URL)
        user_id = facebook.get_user_id_from_token(access_token)
        user_info = facebook.get_user_info(user_id, access_token)

        # authenticate backends에 FacebookBackend추가해서 dict_user_info객체로 로그인 가능
        user = auth_authenticate(user_info=user_info)
        if user is not None:
            auth_login(request, user)
            messages.success(request, '페이스북 유저로 로그인 되었습니다')
            return redirect('blog:post_list')
        else:
            messages.error(request, '페이스북 로그인에 실패하였습니다')
            return redirect('member:login')
Ejemplo n.º 3
0
def login_facebook(request):

    if request.GET.get('error'):
        messages.error(request, '페이스북 로그인을 취소했습니다')
        return redirect('member:login')

    if request.GET.get('code'):

        code = request.GET.get('code')
        REDIRECT_URL = 'http://127.0.0.1:8000/member/login/facebook/'

        # 모듈화 작업함함
        access_token = facebook.get_access_token(code, REDIRECT_URL)
        user_id = facebook.get_user_id_from_token(access_token)
        dict_user_info = facebook.get_user_info(user_id, access_token)

        # 유저를 인증하고 로그인시키자
        user = auth_authenticate(user_info=dict_user_info)
        if user is not None:
            auth_login(request, user)
            messages.success(request, '페이스북 로그인 성공')
            return redirect('blog:post_list')
        else:
            messages.error(request, '페이스북 로그인 실패')
            return redirect('member:login')
Ejemplo n.º 4
0
def login_facebook(request):
    login_next = request.GET.get('login_next')
    # quote_login_next = quote(login_next, safe='')
    # quote_login_next = login_next
    # print('login_next : %s' % login_next)
    # print('quote_login_next : %s' % quote_login_next)
    REDIRECT_URI = '{}{}'.format('%s://%s' % (request.scheme, request.META['HTTP_HOST']), reverse('member:login_facebook'))

    if request.GET.get('error'):
        messages.error(request, '사용자에 의해 페이스북 로그인 거부')
        return redirect('member:login')

    if request.GET.get('code'):
        code = request.GET.get('code')
        print('code: %s' % code)

        access_token = facebook.get_access_token(code, REDIRECT_URI)
        user_id = facebook.get_user_id_from_access_token(access_token)
        user_info = facebook.get_user_info(user_id, access_token)

        user = authenticate(user_info=user_info)
        if user is not None:
            login(request, user)
            messages.success(request, '페이스북 아이디로 로그인 되었습니다')
            if login_next:
                return redirect(login_next)
            else:
                return redirect('blog:post_list')
        else:
            messages.error(request, '페이스북 로그인에 실패하였습니다')
            return redirect('member:login')
Ejemplo n.º 5
0
def login_facebook(request):
    if request.GET.get('error'):
        messages.error(request, 'failed login')
        return redirect('member:login')
    if request.GET.get('code'):
        REDIRECT_URI = 'http://127.0.0.1:8000/member/login/facebook/'
        code = request.GET.get('code')
        access_token = facebook.get_access_token(code, REDIRECT_URI)
        user_id = facebook.debug_token(access_token)
        user_info = facebook.get_user_info(user_id, access_token)

        #authenticate backends
        dict_user_info = facebook.get_user_info(user_id, access_token)
        user = auth_authenticate(user_info=dict_user_info)
        if user is not None:
            auth_login(request, user)
            messages.success(request, 'Facebook login success')
            return redirect('blog:post_list')
        else:
            messages.error(request, 'Failed Facebook login')
            return redirect('member:login')
Ejemplo n.º 6
0
def login_facebook(request):
    if request.GET.get('error'):
        messages.error(request, '사용자가 페이스북 로그인을 거절하였습니다.')
        return redirect('member:login')

    if request.GET.get('code'):
        REDIRECT_URL = 'http://127.0.0.1:8000/member/login/facebook/'
        code = request.GET.get('code')

        access_token = facebook.get_access_token(code, REDIRECT_URL)
        user_id = facebook.get_user_id_from_token(access_token)
        user_info = facebook.get_user_info(user_id, access_token)

        user = auth_authenticate(user_info=user_info)
        if user is not None:
            auth_login(request, user)
            messages.success(request, '페이스북 유저로 로그인 되었습니다')
            return redirect('blog:post_list')
        else:
            messages.error(request, '페이스북 로그인에 실패하였습니다')
            return redirect('member:login')
Ejemplo n.º 7
0
def login_facebook(request):
    if request.GET.get('error'):
        messages.error(request, '사용자가 페이스북 로그인을 취소하셨습니다.' )
        return redirect( 'login' )

    code = request.GET.get('code')
    print(code)
    redirect_uri = 'http://127.0.0.1:8000/member/login/facebook/'

    access_token = facebook.get_access_token(code, redirect_uri)
    user_id = facebook.get_user_id_from_token(access_token)
    user_info = facebook.get_user_info(user_id)

    user = auth_auth(user_info=user_info)
    if user is None:
        messages.error(request, '페이스북 로그인에 실패하였습니다.')
        return render(request, "member/login.html", {})
    else:
        auth_login(request, user)
        messages.success(request, '페이스북 로그인에 성공하였습니다.')
        return redirect( 'post_list')
Ejemplo n.º 8
0
def friends_ranking(request):
    if request.method == 'GET':
        redirect_uri = '{}{}'.format(
            '%s://%s' % (request.scheme, request.META['HTTP_HOST']),
            reverse('sns:friends_ranking'))

        if request.GET.get('error'):
            messages.error(request, '사용자에 의해 페이스북 로그인 거부')
            return redirect('member:login')

        if request.GET.get('code'):
            code = request.GET.get('code')

            access_token = facebook.get_access_token(code, redirect_uri)
            user_id = facebook.get_user_id_from_access_token(access_token)
            url_request_feed = 'https://graph.facebook.com/v2.8/{user_id}/feed?' \
                               'limit=10000&' \
                               'since=2013-01-01&' \
                               'until=2016-12-31&' \
                               'fields=comments&' \
                               'access_token={access_token}'.format(
                                    user_id=user_id,
                                    access_token=access_token
                                )
            r = requests.get(url_request_feed)
            dict_feed_info = r.json()

            comment_list = []
            for feed in dict_feed_info.get('data'):
                if feed.get('comments'):
                    for comment in feed.get('comments').get('data'):
                        comment_list.append(comment)

            counter = Counter()
            id_list = [
                comment.get('from', {}).get('id') for comment in comment_list
            ]
            for id in id_list:
                counter[id] += 1

            most_list = counter.most_common()
            most_id_list = [item[0] for item in most_list]
            str_most_id_list = ','.join(most_id_list)
            print(most_id_list)

            url_request_friends = 'https://graph.facebook.com/v2.8/?ids={ids}&' \
                                  'fields=cover,email,picture.width(500).height(500),name&' \
                                  'access_token={access_token}'.format(
                                    ids=str_most_id_list,
                                    access_token=access_token
                                )
            r = requests.get(url_request_friends)
            dict_friends_info = r.json()

            most_dict_list = []
            for most in most_list:
                id = most[0]
                for k in dict_friends_info:
                    if k == id and k != user_id:
                        most_dict_list.append({
                            'info': dict_friends_info[k],
                            'number': most[1],
                        })

            context = {
                'facebook_user_id': user_id,
                'access_token': access_token,
                'most_dict_list': most_dict_list
            }
            return render(request,
                          'projects/sns/facebook/friends_ranking.html',
                          context)

    elif request.method == 'POST':
        import ast
        user = request.user
        if not user.is_authenticated():
            access_token = request.POST['access_token']
            facebook_user_id = request.POST['facebook_user_id']
            user_info = facebook.get_user_info(facebook_user_id, access_token)
            user = authenticate(user_info=user_info)

        fr = FriendsRanking.objects.create(user=user)
        str_item_list = request.POST.getlist('item')
        for str_item in str_item_list:
            item = ast.literal_eval(str_item)
            facebook_user_id = item['info']['id']
            name = item['info']['name']
            url_profile = item['info']['picture']['data']['url']
            comment_count = item['number']
            FriendsRankingItem.objects.create(
                parent=fr,
                facebook_user_id=facebook_user_id,
                name=name,
                url_profile=url_profile,
                comment_count=comment_count)
        return redirect('sns:friends_ranking_saved', pk=fr.pk)