To let users log in with their Google account from our Djangos site, this is done with the django-allauth package.
If not installed, you can use the following command to install:
pip install django-allauth
configuration
The official documentation for configuring django-allauth is well written, see here:
https://django-allauth.readth…
Create the certificate in Google APIs
Now let’s go to the Google Developer Console,
In the left sidebar, find APIs & Services:
Click on theCredentials(Certificate) Options Then we go to the certificate page:
At the top of the page, clickCREATE CREDENTIALS
And selectOAuth client ID
Create the certificate:
You need to fill out a form with three options
- Name:
stdworkflow
(This name is used to identify the certificate in the control panel and is not displayed to the end user.) - Authorized JavaScript origins:
https://stdworkflow.com
(Replace your domain name) - Authorized redirect URIs:
https://stdworkflow.com/accounts/google/login/callback/
(Replace it with your domain name, the format is the same)
Save, done. Google will automatically generate a Client ID and a Client secret for you.
willClient ID 和 Client secretWrite to database
Method 1: Add GUI manually
In our database, django-allauth creates several tables for us with the prefix socialaccount_, and we need to modify two of them:
socialaccount_socialapp
socialaccount_socialapp_sites
socialaccount_socialapp
id | provider | name | client_id | secret | key |
---|---|---|---|---|---|
YOUR_CLIENT_ID | YOUR_CLENT_SECRET |
id
: No need to fill in, automatically generated by the database (in the backsocialaccount_socialapp_sites
From the table)provider
: Googlename
: Googleclient_id
Front in:APIs & ServicesAutomatically generated inclient_idsecret
Front in:APIs & ServicesAutomatically generated inclient_secretkey
: don’t fill in
socialaccount_socialapp_sites
id | socialapp_id | site_id |
---|---|---|
1 (if it’s the first site) | 1 |
id
: Do not fill, or automatically generated by the databasesocialapp_id
Front in:socialaccount_socialappAutomatically generated in the tablepk/id
(in this case1
)site_id
: 1 (Usually)
Now that everything is configured, go to your login page and you’ll see that django-allauth automatically adds a Google option to sign up and log in via your Google account.
Method 2: Write to the database command-line
Here is a complete example of writing the above data to the SOCIALACCOUNT_SOCIALAPP and SOCIALACCOUNT_SOCIALAPP_SITES tables, respectively:
[user@localhost]$ mysql -u root -p Enter password: mysql> use DATABASENAME; # repalce "DATABASENAME" with your database name Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> insert into socialaccount_socialapp values (1, 'Google', 'Google', 'YOUR_CLIENT_ID', 'YOUR_CLENT_SECRET', ''); Query OK, 1 row affected (0.01sec) mysql mysql> insert into socialaccount_socialapp_sites values (1, 1, 1); Query OK, 1 row affected (0.01sec) mysql> insert into socialaccount_socialapp_sites values (1, 1, 1); Query OK, 1 row affected (0.01 SEC) mysql> exit Bye