def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) if not serializer.is_valid(): return Response({'error': serializer.errors}, status.HTTP_400_BAD_REQUEST) try: # submit the auth code to facebook for a facebook user token code = serializer.validated_data['code'] path = ('https://graph.facebook.com/v%s/oauth/access_token' % (settings.FACEBOOK_GRAPH_VERSION)) params = { 'client_id': settings.FACEBOOK_APP_ID, 'redirect_uri': settings.FACEBOOK_SUCCESSFUL_LOGIN_URL, 'client_secret': settings.FACEBOOK_APP_SECRET, 'code': code } response = self.with_log(request, 'post', path, {}, {}, params) result = json.loads(response.content.decode("utf-8")) if response.status_code != status.HTTP_200_OK: return Response({"result": result}, response.status_code) access_token = result['access_token'] return Response({"result": { "facebook_user_token": access_token }}, status.HTTP_200_OK) except Exception as e: return unknown_exception_handler(e)
def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) if not serializer.is_valid(): return Response({'error': serializer.errors}, status.HTTP_400_BAD_REQUEST) try: flow = _googleFlow() # submit the auth code to Google for a Google user token code = serializer.validated_data['code'] try: flow.fetch_token(code=code) except Exception as e: return unknown_exception_handler(e) google_user_token = flow.credentials.__dict__['token'] # access_token = result['access_token'] return Response( {"result": { "google_user_token": google_user_token }}, status.HTTP_200_OK) except Exception as e: return unknown_exception_handler(e)
def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) if not serializer.is_valid(): return Response({'error': serializer.errors}, status.HTTP_400_BAD_REQUEST) try: # have a facebook user token, exchange it for a Django access token facebook_user_token = serializer.validated_data[ 'facebook_user_token'] return _facebookTokenForAccessToken(facebook_user_token) except Exception as e: return unknown_exception_handler(e)
def get(self, request, *args, **kwargs): try: graph = facebook.GraphAPI( access_token=settings.FACEBOOK_APP_CLIENT_TOKEN, version=settings.FACEBOOK_GRAPH_VERSION) perms = ["email"] fb_login_url = graph.get_auth_url( settings.FACEBOOK_APP_ID, settings.FACEBOOK_SUCCESSFUL_LOGIN_URL, perms) except Exception as e: return unknown_exception_handler(e) return Response({"result": fb_login_url}, status.HTTP_200_OK)
def post(self, request, *args, **kwargs): try: # re-use the end point for obtaining the facebook user token cfgt = AuthCodeForGoogleToken.as_view()(self.request._request) # if something went wrong, pass along the bad news if cfgt.status_code != status.HTTP_200_OK: return cfgt result = cfgt.data['result'] google_user_token = result['google_user_token'] # have a Google user token, exchange it for a Django access token return _googleTokenForAccessToken(google_user_token) except Exception as e: return unknown_exception_handler(e)
def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) if not serializer.is_valid(): return Response({'error': serializer.errors}, status.HTTP_400_BAD_REQUEST) try: ''' This code is verbose on purpose for reference. API key and auth token are included for reference as well. api keys, URLs and auth tokens would not normally be stored here, but rather in an environment variable specific to a staging or production server instancese. The auth_token could be a permanent token, or there could be additional logic for re generating expired tokens. ''' path = 'https://jsonplaceholder.typicode.com/users' ''' #error test path = 'http://www.mocky.io/v2/5c9e9556300000af21ee98ab' ''' params = {'sort': 'asc', 'limit': '100'} api_key = '123456789' auth_token = 'abcdefghi' headers = { 'Content-Type': 'application/json', 'X-Auth-API-Key': api_key, 'authorization': 'Bearer ' + auth_token } body = {"company_id": 12, "department": "accounting"} response = requests.get(path, headers=headers, json=body, params=params) result = json.loads(response.content.decode("utf-8")) except Exception as e: return unknown_exception_handler(e) return Response({"result": result}, status.HTTP_200_OK)