8

I am sure many Django developers must come across this issue when using the social-auth. Initially when you develop it, you would like to test it on your local server, hence you would redirect the domainname in your etc/hosts.

I came along this in the documentation:

https://github.com/omab/django-social-auth#facebook

If you define a redirect URL in Facebook setup page, be sure to not define http://localhost:8000 because it won't work when testing. Instead I define http://myapp.com and setup a mapping on /etc/hosts or use dnsmasq.

From my understanding you can not define any ports in /etc/hosts.

Hence defining this entry there:

127.0.0.1       example.com

still doesn't hit my Django server that runs at 127.0.0.1:8000.

How do you guys do this?

Thanks,

Stephan Muller
  • 27,018
  • 16
  • 85
  • 126
Houman
  • 64,245
  • 87
  • 278
  • 460
  • 2
    If you only type mysite.com in address bar it will use port 80 by default. Try `mysite.com:8000` – Davor Lucic Jun 10 '12 at 16:09
  • Ahh thank didn't know this. But the author also says: "Instead I define http://myapp.com and setup a mapping on /etc/hosts or use dnsmasq." Hence he doesn't setup myapp.com:8000 under facebook settings. – Houman Jun 10 '12 at 16:13
  • Well I don't know if port number is significant for authenticating with Facebook, but your other option is to run Django on port 80 which will require root permissions and that port is not already taken (by Apache for example). – Davor Lucic Jun 10 '12 at 16:17
  • Ah I am trying to run PyCharm as root, but I can't do Sudo /home/username/pycharm-2.5.1/bin/pycharm.sh it popsup the installation dialogbox instead. Do you know how to run PyCHarm as root? – Houman Jun 10 '12 at 16:29
  • I'd recommend running Django from command line (`manage.py runserver`) as root rather then trying to get your IDE running as root. But even running Django as root is not really a good idea, if you really need the port 80 run it from command line until you finish setting up facebook auth then go back to your normal development environment. – Davor Lucic Jun 10 '12 at 16:33
  • In the docs I'm trying to say that I define ``http://myapp.com`` in the Facebook Developers Dashboard when the app is created on Facebook side. – omab Aug 21 '14 at 14:16

5 Answers5

5

Everyone says, "add example.com to your hosts file..." but neglects to mention that you should use example.com:8000 after starting the runserver. Here are more detailed steps that worked for me:

On linux:

  • open a terminal
  • sudo gedit (or replace gedit with the text editor of choice)
  • Open the etc/hosts file
  • add a line: 127.0.0.1 example.com (if you already have a line starting with 127.0.0.1 you can leave it - https://serverfault.com/questions/231844/is-it-safe-to-add-additional-127-0-0-1-entries-to-etc-hosts)
  • save the file
  • start your django dev server - python manage.py runserver
  • open a browser and go to example.com:8000 (this gives the correct callback to google and other sites that will not work with 127.0.0.1 or localhost)
  • You should see the home page of your django site.
  • Go to the social site you want to connect with and set up your client ID, using example.com as the website name. For example, the callback url for google is http://example.com:8000/complete/google-oauth2/
  • Copy the appropriate key/id settings and paste into your SETTINGS.py
  • Now your dev server should work!

I know this is an older post, but I could not find the complete steps anywhere. Please add clarifications if necessary as I am not an expert with this.

Community
  • 1
  • 1
Steve
  • 715
  • 10
  • 18
0

I have solved the redirection problem by following the steps here: https://serverfault.com/questions/397364/how-to-setup-named-virtual-hosts-in-nginx

In short, you need an entry in /etc/hosts as I have above plus nginx proxy between.

Community
  • 1
  • 1
Houman
  • 64,245
  • 87
  • 278
  • 460
  • @Kave, did the answer you mentioned in your serverfault answer works? I see in that comment that you were still struggling with the issue, let me know – daydreamer Jun 20 '12 at 13:13
  • Daydreamer. Yes it has worked. We sorted it out in the chat. Effectively the mistake I did was deleting the line 127.0.0.1 MySite.com from /etc/hosts before I attempted the nginx solution. I didn't realize I needed both. – Houman Jun 20 '12 at 16:14
  • when I use your setting as mentioned, add to /etc/hosts,and try http://mysite.com, I get a page saying "Welcome to Ngnix" – daydreamer Jun 20 '12 at 17:52
0

Install fiddler. Once installed, go to tools > Hosts.

Then you can map from 127.0.0.1:8000 to mysite.com

user929404
  • 2,153
  • 1
  • 22
  • 27
0

The following finally fixed it for me...

Under Authorized JavaScript origins: http://localhost:8000

Under Authorized redirect URIs: http://localhost:8000/page-name-here

0

If any of you guys is still struggling after adding 127.0.0.1 mysite.com to /etc/hosts, check whether you're using a VPN. I was still getting the error, "Unable to connect to mysite.com:8000". I resolved this error by turning off my VPN.