Skip to content

Commit

Permalink
Merge pull request wevote#1815 from SailingSteve/stevePythonApr27-440pm
Browse files Browse the repository at this point in the history
Updated ServerOAuth.md
  • Loading branch information
SailingSteve authored Apr 27, 2022
2 parents 0d2b60f + cff9f2f commit 0b212ac
Showing 1 changed file with 62 additions and 13 deletions.
75 changes: 62 additions & 13 deletions docs/ServerOAuth.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
# OAuth for Facebook and Twitter on the API Server

Changed the Python server SDK to V12.0 on April 25, 2022 (from V6.0). V13 is the latest, but what was the latest version of social_django had problems with it.
Changed the Python server SDK to V12.0 on April 25, 2022 (from V6.0). V13 was the latest version of that time, but social-auth-app-django==5.0.0 (social_django) had problems with it.

###Nomenclature:
**social-auth-app-django** is the same as **social_django**

Installation of social-auth-app-django has already been done for you, but...
```shell
$ pip install social-auth-app-django
```
installs both
```
social-auth-app-django==5.0.0
social-auth-core==4.2.0
```

## Run your local python api server in SSL (https)

In your environment_variables.json
Replace all (6) urls from `http://localhost:8000/` to `https://wevotedeveloper.com:8000/`
replace all (6) urls that contain `http://localhost:8000/` (or 8001), with `https://wevotedeveloper.com:8000/`

(Explanation at https://github.com/teddziuba/django-sslserver)

Then start a SSL-enabled debug server:
Then start an SSL-enabled debug server:

![ScreenShot](images/RunSslServer.png)
![ScreenShot](images/RunningSslServer.png)
Expand All @@ -24,14 +38,13 @@ and access the API Server Python Management app on https://wevotedeveloper.com:8

The first time you start up the [runsslserver](https://github.com/teddziuba/django-sslserver) the app may take a full minute to respond to the first request.

### Make a small necessary change to your /etc/hosts

## Facebook

### /etc/hosts
Facebook will no longer redirect to localhost, so make a second alias for 127.0.0.1 with this made up domain: `wevotedeveloper.com`

[If you define a redirect URL in Facebook setup page, be sure to not define http://127.0.0.1:8000 or http://localhost:8000 because it won’t work when testing. Instead I define http://wevotedeveloper.com and setup a mapping on /etc/hosts.](https://python-social-auth.readthedocs.io/en/latest/backends/facebook.html)
From the python-social-auth docs: "[If you define a redirect URL in Facebook setup page, be sure to not define http://127.0.0.1:8000 or http://localhost:8000 because it won’t work when testing. Instead I define http://wevotedeveloper.com and setup a mapping on /etc/hosts.](https://python-social-auth.readthedocs.io/en/latest/backends/facebook.html)"

So we have to make a small change to /etc/hosts, before:
So we have to make a small change to /etc/hosts. This is the before:
```
(venv2) stevepodell@StevesM1Dec2021 WeVoteServer % cat /etc/hosts
##
Expand Down Expand Up @@ -60,9 +73,32 @@ you need to add that domain to your 127.0.0.1 line in /etc/hosts. After the cha
::1 localhost
(venv2) stevepodell@StevesM1Dec2021 WeVoteServer %
```
On the [Facebook Login Settings](https://developers.facebook.com/apps/1097389196952441/fb-login/settings/) page under Valid OAuth Redirect URIs
we have an entry `https://wevotedeveloper.com:8000/complete/facebook/` that will allow the
3 leg OAuth redirect, to find its way back to the Python app (the Api Server).

You will need to elevate your privileges with sudo to make this edit to this linux system file ... ` % sudo vi /etc/hosts` or with some other editor.



## Facebook
In environment_variables.json, these two variable need to be set:
```
"SOCIAL_AUTH_FACEBOOK_KEY": "1...1",
"SOCIAL_AUTH_FACEBOOK_SECRET": "7...b",
```

These keys are generated (or regnerated), and there is only one chance to copy them
down, since they will never again be visible on the Facebook console.

(Ask Dale to get the latest values.)


### Facebook OAuth 2, redirect URLS
These two urls point to the API server destination, for returning from OAuth with a successful login. They have already been
registered with Facebook on the [Facebook Login Settings](https://developers.facebook.com/apps/1097389196952441/fb-login/settings/) page under Valid OAuth Redirect URIs.

In case something changes, these are the configurations that we have made.

We have an entry `https://wevotedeveloper.com:8000/complete/facebook/` for testing with a local Python API Server running on your local,
and we have a second entry for production: `https://api.wevoteusa.org/complete/facebook/`, both of these values are already setup for you.


### Debugging Facebook Oauth
Expand All @@ -71,7 +107,7 @@ Just a list of starting points for next time.

This is the key file for Oauth2: `venv2/lib/python3.9/site-packages/social_core/backends/facebook.py`

curl -i -X GET "https://graph.facebook.com/v12.0/oauth/access_token?client_id=1097389196952441&redirect_uri=https%3A%2F%2Fwevotedeveloper.com%3A8000%2Fcomplete%2Ffacebook%2F&client_secret=<secret>&code=<code generated by previous leg in OAuth>"
```curl -i -X GET "https://graph.facebook.com/v12.0/oauth/access_token?client_id=1097389196952441&redirect_uri=https%3A%2F%2Fwevotedeveloper.com%3A8000%2Fcomplete%2Ffacebook%2F&client_secret=<secret>&code=<code generated by previous leg in OAuth>"```

https://github.com/python-social-auth/social-app-django

Expand All @@ -84,9 +120,22 @@ https://python-social-auth.readthedocs.io/en/latest/backends/facebook.html
https://medium.com/@kennethjiang/python-social-auth-for-django-tutorial-16bbe792659f

## Twitter

In environment_variables.json, these two variable need to be set:
```
"SOCIAL_AUTH_TWITTER_KEY": "w...w", Twitter calls this the "API Key" from the "Consumer Keys" section
"SOCIAL_AUTH_TWITTER_SECRET": "4...H", Twitter calls this the "Secret" from the "Consumer Keys" section
```
(Ask Dale to get the latest values.)

### Twitter OAuth 2, redirect URLS
On the [Twitter Console](https://developer.twitter.com/en/portal/projects/1498394651836891139/apps/23523312/auth-settings), the Callback URI / Redirect URL list needs to include for the development app "WeVote Ballot":
```
https://wevotedeveloper.com:8000/complete/twitter/
```
And for the production App "We Vote Ballot Guide":
```
https://api.wevoteusa.org/complete/twitter/
```

### Debugging Twitter Oauth

Expand Down

0 comments on commit 0b212ac

Please sign in to comment.