def register(request): userid = request.POST['user'] access_token = request.POST['access_token'] res = {} res['valid'] = False if userid and access_token: #Now use the access token to get the user information from graph api try: content = fetch_user_info_from_fb(userid, access_token) except: return render(request, 'polls/error.html', {"message": "Something went wrong. Please logout and login again!"}) logger.debug("Checking userid") #confirm userid is equal to id returned from graph api if 'id' not in content or userid != content['id']: return render(request, 'polls/error.html', {"message": "Something went wrong. Please logout and login again!"}) content['access_token'] = access_token logger.debug("Trying to save user") try: res = save_fb_user(content) logger.debug("Saved user info succesfully"+str(res)) except: logger.exception("Unexpected error while saving user: "******"application/json") return HttpResponse(json.dumps(res), content_type="application/json")
def home(request): logger.info("Loading home page") access_token = request.GET.get('access_token') userid = request.GET.get('userid') res = {} res['valid'] = False #Update the access token for the user in the database try: user = fetch_fb_user(userid) except IndexError: #New user, add user to database try: content = fetch_user_info_from_fb(userid, access_token) except: logger.debug("Error fetching user info from facebook for userid "+str(userid)+"\n"); return render(request, 'polls/error.html', {"message": "Something went wrong. Please logout and login again!"}) #confirm userid is equal to id returned from graph api if 'id' not in content or userid != content['id']: return render(request, 'polls/error.html', {"message": "Something went wrong. Please logout and login again!"}) content['access_token'] = access_token logger.debug("Trying to save user") try: res = save_fb_user(content) logger.debug("Saved user info succesfully"+str(res)) except: logger.exception("Unexpected error while saving user: "******"application/json") if res['valid']: user = fetch_fb_user(userid) logger.debug('Got user '+str(user)+' for userid '+str(userid)+str(access_token)) if user: user.access_token = access_token user.save() return render(request, 'polls/home.html')