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')
def friends_ranking(request): if request.GET.get('error'): return HttpResponse('사용자 로그인 거부') if request.GET.get('code'): code = request.GET.get('code') REDIRECT_URL = 'http://{host}{url}'.format( host=request.META['HTTP_HOST'], url=reverse('sns:friends_ranking'), ) access_token = facebook.get_access_token(code, REDIRECT_URL) # return HttpResponse('%s<br>%s' % (REDIRECT_URL, access_token)) user_id = facebook.get_user_id_from_token(access_token) # facebook에서 comment 하위 속성은 { } 형식으로 불러온다 # {{}} 형식을 사용해야 format() 형식에서 사용 가능하다 url_request_feed = 'https://graph.facebook.com/v2.8/{user_id}/feed?' \ 'fields=picture,link,comments{{from{{picture,name}},message,comments}}&limit=1000&since=2014-01-01&' \ 'access_token={access_token}'.format( user_id=user_id, access_token=access_token, ) r = requests.get(url_request_feed) dic_feed_info = r.json() json_data = json.dumps(dic_feed_info, indent=2) print(json_data) return HttpResponse(json_data)
def friends_ranking(request): if request.GET.get('error'): return HttpResponse('사용자 로그인 거부') if request.GET.get('code'): redirect_uri = 'http://{host}{url}'.format( host=request.META['HTTP_HOST'], url=reverse('sns:friends_ranking') ) print('redirect_uri : %s' % redirect_uri) code = request.GET.get('code') access_token = facebook.get_access_token(code, redirect_uri) user_id = facebook.get_user_id_from_token(access_token) url_request_feed = 'https://graph.facebook.com/v2.8/{user_id}/feed?' \ 'fields=comments{{from,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() json_data = json.dumps(dict_feed_info, indent=2) print(json_data) return HttpResponse(json_data)
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')
def friends_ranking(request): if request.GET.get('error'): return HttpResponse('사용자 로그인 거부') if request.GET.get('code'): redirect_uri = 'http://{host}{url}'.format( host=request.META['HTTP_HOST'], url=reverse('sns:friends_ranking')) print('redirect_uri : %s' % redirect_uri) code = request.GET.get('code') access_token = facebook.get_access_token(code, redirect_uri) user_id = facebook.get_user_id_from_token(access_token) url_request_feed = 'https://graph.facebook.com/v2.8/{user_id}/feed?' \ 'fields=comments{{from,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() json_data = json.dumps(dict_feed_info, indent=2) print(json_data) return HttpResponse(json_data)
def friends_ranking(request): if request.GET.get('error'): return HttpResponse('사용자 로그인 거부') if request.GET.get('code'): code = request.GET.get('code') REDIRECT_URL = 'http://{host}{url}'.format( host=request.META['HTTP_HOST'], url=reverse('sns:friends_ranking'), ) access_token = facebook.get_access_token(code, REDIRECT_URL) # return HttpResponse('%s<br>%s' % (REDIRECT_URL, access_token)) user_id = facebook.get_user_id_from_token(access_token) # facebook에서 comment 하위 속성은 { } 형식으로 불러온다 # {{}} 형식을 사용해야 format() 형식에서 사용 가능하다 url_request_feed = 'https://graph.facebook.com/v2.8/{user_id}/feed?' \ 'fields=comments{{from{{name}},comments}}&limit=1000&since=2014-01-01&' \ 'access_token={access_token}'.format( user_id=user_id, access_token=access_token, ) r = requests.get(url_request_feed) dic_feed_info = r.json() json_data = json.dumps(dic_feed_info, indent=2) # comment를 작성한 name을 리스트로 뽑아보자 facebook_friends_list = [] data = json.loads(json_data) for i in data['data']: try: first_passing = i['comments'] first_comment_name = first_passing['data'][0]['from']['name'] except: pass else: facebook_friends_list.append(first_comment_name) try: second_passing = first_passing['data'][0]['comments'][ 'data'] except: pass else: for second_i in second_passing: second_name = second_i['from']['name'] facebook_friends_list.append(second_name) # list를 dict로 변환하자 dict_friends_ranking = {} for key in facebook_friends_list: dict_friends_ranking[key] = 0 for key in facebook_friends_list: dict_friends_ranking[key] += 1 list_friends_ranking = [{ 'name': key, 'comment': value } for key, value in dict_friends_ranking.items()] sorted_list_friends_ranking = sorted(list_friends_ranking, key=lambda k: k['comment'], reverse=True) return render( request, 'sns/friends_ranking.html', {'sorted_list_friends_ranking': sorted_list_friends_ranking})