pyfacebook #facebook.require_login() decorator causing constant auth_token refresh - django

First time using pyFacebook. I've literally copied the example application and put it up onto my server. I've created an FB app and now when I try to load the app basically what happens is that the browser keeps refreshing. I noticed in the url (for example)
https://apps.facebook.com/myapp/?auth_token=8f826cae31717068c18fb16fd7f0a758
Keeps refreshing with the auth_token changing. If I remove the #facebook.require_login() decorator then the page displays without a problem.
Help please.
I've just noticed that it only does this when I select IFrame and not FBML within my app settings. I have fbml templates which don't work. I know have normal html templates which work on the website but when I select IFrame I get that constant loop (changing url with blank white screen)

Ok so after weeks of pain the problem I was having was that Facebook updated it's entire API. This broke python based apps that were based on that. Like PyFacebook.
I now use fandjango and this it's new, has a great developer and nice documentation.

The problem is most probably somewhere in facebook/init.py, around line 1742
if not params:
if request.method == 'POST':
params = self.validate_signature(request.POST)
if not params: #was else
iframe makes POST call, but auth_token is GET variable.. though if validate_signature fails (params still None) go to GET validation. Also I commented out the return in auth_token checks as suggested in pyfacebook issue tracker.

Related

Django/Vue braces added to request method after logout

I have a basic user management project that I'm using off of which to scaffold other projects. It is a Vue CLI 3 front end and Django/Django REST Framework/Django REST Auth back end. The project I'm posting here uses sqllite but it can relatively easily be converted to another db.
Here is the link to the full repo for anyone who is willing to download to try to replicate my issue: https://github.com/JVP3122/user-project
I'm having a very strange problem in that when I log out of the site and then try to log back in directly from the same page it seems that axios is adding the payload to the beginning of the request method.
For example, in the images found in the post I put up in Imgur (https://imgur.com/a/bEsx662) the user name is simply "test" with the password "password", and when I try to log back in after logging out the subsequent login attempt is no longer a POST route, but instead a {}POST route. If I try again, the route becomes a {"USERNAME":"TEST","PASSWORD":"PASSWORD"}POST method.
I've tried looking at the config in the axios request interceptor, looking at the dispatch method in the rest_framework source code, and I can't figure out what is going on or how to solve this. It's a small bug that doesn't take away from the rest of the functionality, but it's a bug nonetheless.
Any help would be appreciated.
Did you try:
setting up new project (npm reinstall, clear npm cache etc..)
using axios.post instead of custom made HTTP object
I don't see anything in the backend that could interrupt the request and customise method as described in the original post.
Hopefully one of these two options above will resolve it.
Responded in your issue axios/axios#1994.

Django rest framework Reactjs sessions not working

So I have set up Django rest framework as a backend API for an e-commerce website. The website is displayed through a React frontend, which is not served by the django backend.
I am currently running both the Django backend and the React frontend from their local development servers (http://127.0.0.1:8000 and http://127.0.0.1:3000 respectively). In the future they will be on separate domains, probably.
When I set a session in a view, and read the content in another, this works if I just type in the urls for creating and reading directly into my browser (just for testing purposes). But when I access the backend through my frontend, sessions can not be accessed anymore, or don't seem stored. What will happen is that I get a KeyError when trying to access the data that I set in a previous view.
I guess this has to do with something I have read about some time ago, but I find it hard to find the correct information on how to work with this. Does this have to do with the cookie with the session id not being available to the frontend, but only to the backend itself?
Main question:
I would like to know how I can work with sessions, using the above settup, for keeping a shopping cart.
My backend code, just in case someone wonders:
from django.http import HttpResponse
def cart_add(request, product_id, update, quantity):
request.session['one'] = 'created through "cart_add" view'
return HttpResponse("Created a session - cart_add")
def create(request):
request.session['one'] = 'created through "read" view'
return HttpResponse("Created a session - create")
def read(request):
print(request.session['one'])
I have removed some unnecessary code.
The cart_add view is called from the React frontend, using an ajax call (axios).
The create and the read view I called by typing their urls directly into the browser.
(This is all done for testing purposes, just making sure sessions are working before I start to write the real code.)
I've found a solution in another stackoverflow question. This is the link to it.
By adding the following to my axios request, the code works successfully:
axios.get('some api url', {withCredentials: true});
So it seems my assumption about the cookie with the session id not being available to the frontend is incorrect.
I also found out that I could see the cookie by opening the web page in Chrome, then opening the developer tools > going to 'application' tab > click on cookies.
Here all the available cookies are listed, and also a sessionid cookie is shown.
I had the same issue, by adding withCredentials in axios call didn't solve my problem in django 2.2.3 and axios 0.19.0.
If the answer here doesn't work for you, then look into the below answer :)
React Django REST framework session is not persisting/working

How to use react-router and Django templates

Folks,
I am pretty sure I am not the first one to stumble on this problem. But somehow I am unable to find any relevant resources out there.
Here is my issue, I have a backend in Django and my front completely written in Reactjs- React Router - Redux (nice combo right).
when entering the url webhost.com/, django provides me with a page with links to a bundle that is my whole react application and different stylesheets
The problem arise when I want to refresh a page, the browser still tries to query the server even though a route exists in my react-router configuration.
I had a look at the answer here (catch-all option) React-router urls don't work when refreshing or writting manually , but I don't quite understand it and I am afraid to have a new redux state everytime Django will provide the user with a new page.
You can setup up a wildcard url pattern that will render the same view that gets rendered when a request is sent to webhost.com. I don't know if that's going to retain your store though.

Single-page login in Django app

I'm currently using out-of-the-box django.contrib.auth to handle authentication in my Django app. This means that the user starts at a log in page and is redirected to the app on successful login. I would like to make my app single-page, including this login process, where a redirect doesn't happen, but maybe a "hot" template switch-out or some fancy client-side div magic (that still remains secure). My Google searching turned up pretty short, the closest solution dealing with putting a log in form on every page.
Any direction or ideas here would be much appreciated. I would obviously prefer to work within the existing confines of django.contrib.auth if possible, but I'm open to all solutions.
I'm not sure I understand your question completely. I think you want to have a single page. If so, put logic in your template that checks to see if the user is authenticated. If not, display a login form that POSTS to the appropriate django.contrib.auth view. You can supply an argument to this view to have it redirect back to your page. When you come back, the user will be authenticated, so you won't display the login form.
Have a look at Django-Easy-Pjax https://pypi.python.org/pypi/django-easy-pjax - it works like a charm and is well documented. Everything you like is being made with AJAX requests: links, forms using GET and forms using POST.
Essentially you only need to add a data-pjax="#id_of_the_container_where_the_result_goes" attribute in your a and form tags.
And the great thing about it: It updates the title and location bar of your browser.
One caveat: If you want to upload files in some form, this is not supported by Easy-Pjax, so you might want to use some workaround jQuery library for that.

local copy of this webpage is out of date

My Web pages causes following error on the browser's Back Button,
It works fine for firefox but not on IE,
IE Says,
Most likely cause:
•The local copy of this webpage is out of date, and the website requires that you download it again.
What you can try:
Click on the Refresh button on the toolbar to reload the page. After refreshing, you might need to navigate to the specific webpage again, or re-enter information.
what should be the cause ?
I'm using Django + mod_python + apache as production environment.
how can I eliminate this error or how to trace any help would be appreciated.
Using GET instead of POST isn't always possible, e.g. when the amount of data exceed the maximum URL length. So, if you want to use POST i would suggest, that you response with a redirect after each POST. Something like that (pseudocode):
def view(request):
form = Form(request.form)
if request.method == 'POST' and form.validates():
# process form data, e.g.
m = Model(form.data)
m.save()
# response with a redirect (e.g. to the newly inserted data, or
# back to the initial page)
return redirect(m.get_absolute_url())
return render_template('template.html', form=form)
The advantage of this is that the user won't get those annoying warnings about resending any POST data you described and that multiple submissions of the same data are less likely to occur (which means for example fewer double postings).
You're not wrong in your suggestion. It's because you've used a POST on that page. If you want people to go back to the page, put a link on the page to take them back to where they want to go, but make sure that they can still view the page properly without having to POST data to it.