Last active
February 7, 2016 18:29
-
-
Save makerj/24f34189125565a5575b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@api_view(['GET', 'POST']) | |
def login_facebook(request): | |
""" | |
login via facebook | |
PREREQUISITES: getting 'code' at frontend using oauth uri like this: | |
https://www.facebook.com/dialog/oauth?client_id=563193193877116&response_type=code&scope=user_friends,public_profile,email&redirect_uri=http://api.phople.us/login_facebook | |
:param request: request | |
:return: login result | |
""" | |
""" | |
##Retrieving friends list | |
https://graph.facebook.com/{uid}/friends?access_token={access_token} | |
{ | |
"data": [ | |
], | |
"summary": { | |
"total_count": 1 | |
} | |
} | |
""" | |
appid, appsecret = read_credential('facebook', 'APP_ID'), read_credential('facebook', 'APP_SECRET') | |
app_access_token = '{}|{}'.format(appid, appsecret) | |
# error case | |
if request.query_params.get('error'): | |
return Response('login rejected by user', status=status.HTTP_403_FORBIDDEN) | |
elif request.query_params.get('code'): | |
# step1: retrieving 'access token' | |
code = request.query_params.get('code') | |
uri = 'https://graph.facebook.com/v2.3/oauth/access_token?client_id={appid}&redirect_uri={uri}&client_secret={appsecret}&code={code}' | |
uri = uri.format(appid=appid, uri='http://api.phople.us/login_facebook', appsecret=appsecret, code=code) | |
''' | |
{ | |
“access_token”: <access-token>, | |
“token_type”:<type>, | |
“expires_in”:<seconds-til-expiration> | |
}''' | |
access_token_response = nap.url.Url(uri).get().json() | |
access_token = access_token_response.get('access_token') | |
# step2: retrieving user information by using access token | |
# 2.1 inspect access token. expected response: | |
""" | |
{ | |
"data": { | |
"app_id": "563193193877116", | |
"application": "phople", | |
"expires_at": 1454695200, | |
"is_valid": true, | |
"scopes": [ | |
"user_friends", | |
"email", | |
"public_profile" | |
], | |
"user_id": "438863309656541" | |
} | |
}""" | |
inspected = nap.url.Url('https://graph.facebook.com/debug_token'). \ | |
get(params={'input_token': access_token, 'access_token': app_access_token}).json().get('data') | |
is_valid, user_id = inspected.get('is_valid', False), inspected.get('user_id', -1) | |
if not is_valid: | |
return Response(status=status.HTTP_400_BAD_REQUEST) | |
# 2.2 retrieving user information | |
uri = 'https://graph.facebook.com/{uid}?fields=id,name,first_name,last_name,age_range,link,gender,locale,picture,timezone,updated_time,verified,email&access_token={access_token}' | |
uri = uri.format(uid=user_id, access_token=access_token) | |
''' | |
https://graph.facebook.com/438863309656541?fields=id,name,first_name,last_name,age_range,link,gender,locale,picture,timezone,updated_time,verified,email&access_token=CAAIAOKJCznwBAGeynk870ds8ZCAzhCV6mmhf3j33j8e01YA783O7rxDxCJhLM3p6WZBoZAl7oBpIzpuvcTvpitPiaipo5NgU1NNl7x0UXUQ6l4EXcFDW4TzLaAhNvkmNiwHlppouk0v5lVXQ5EAUJqPdFZBVDUmkASfmyaqWg49ExwF7bGQZBjZCRIZCKzf7EH5Fh6YnFgKKQZDZD | |
{ | |
"id": "438863309656541", | |
"name": "JunHui Lee", | |
"first_name": "JunHui", | |
"last_name": "Lee", | |
"age_range": { | |
"min": 21 | |
}, | |
"link": "https://www.facebook.com/app_scoped_user_id/438863309656541/", | |
"gender": "male", | |
"locale": "ko_KR", | |
"picture": { | |
"data": { | |
"is_silhouette": true, | |
"url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfa1/v/t1.0-1/c15.0.50.50/p50x50/10354686_10150004552801856_220367501106153455_n.jpg?oh=31a919a3d795bc64ad02d0a1b462300e&oe=5736B82F&__gda__=1463225095_484ea4441f278e6b8895ab53a1be91a0" | |
} | |
}, | |
"timezone": 9, | |
"updated_time": "2015-07-11T17:39:44+0000", | |
"verified": true, | |
"email": "ohenwkgdj\u0040gmail.com" | |
}''' | |
user_information = nap.url.Url(uri).get().json() | |
return Response(user_information) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment