| 1 |
Google |
| 2 |
------ |
| 3 |
|
| 4 |
The Google provider is OAuth2 based. |
| 5 |
|
| 6 |
More info: |
| 7 |
https://developers.google.com/identity/protocols/OAuth2 |
| 8 |
|
| 9 |
|
| 10 |
App registration |
| 11 |
**************** |
| 12 |
Create a google app to obtain a key and secret through the developer console. |
| 13 |
|
| 14 |
Google Developer Console |
| 15 |
https://console.developers.google.com/ |
| 16 |
|
| 17 |
After you create a project, you will have to create an OAuth client ID and fill |
| 18 |
in some project details for the consent form that will be presented to the |
| 19 |
client: |
| 20 |
|
| 21 |
#. Under "APIs & Services", go to "Credentials", click "Create credentials" and |
| 22 |
create a new "OAuth client ID". Probably you will want to choose "Web |
| 23 |
application" as the application type. Provide your domain name or test |
| 24 |
domain name in "Authorized JavaScript origins". Finally, fill in the |
| 25 |
"Authorized redirect URIs" field with URLs like |
| 26 |
``http://example.com/accounts/google/login/callback/`` , replacing the |
| 27 |
domain name with your domain name, or with ``127.0.0.1:8000`` for testing. |
| 28 |
After creating the OAuth client ID, make a note of the client ID and the |
| 29 |
client secret, as you will need it later. |
| 30 |
|
| 31 |
#. Users that log in using the app will be presented a consent form. For this |
| 32 |
to work, additional information is required. Under "APIs & Services", go to |
| 33 |
"OAuth consent screen" and at least provide an email address and a product |
| 34 |
name. |
| 35 |
|
| 36 |
|
| 37 |
Django configuration |
| 38 |
******************** |
| 39 |
|
| 40 |
Don't forget to add the Google provider to your ``INSTALLED_APPS`` setting: |
| 41 |
|
| 42 |
.. code-block:: python |
| 43 |
|
| 44 |
INSTALLED_APPS = [ |
| 45 |
... |
| 46 |
'allauth.socialaccount.providers.google', |
| 47 |
] |
| 48 |
|
| 49 |
The app credentials are configured for your Django installation via the admin |
| 50 |
interface. Create a new socialapp through ``/admin/socialaccount/socialapp/``. |
| 51 |
|
| 52 |
Fill in the form as follows: |
| 53 |
|
| 54 |
* Provider, "Google" |
| 55 |
* Name, your pick, suggest "Google" |
| 56 |
* Client id, is called "Client ID" by Google |
| 57 |
* Secret key, is called "Client secret" by Google |
| 58 |
* Key, is not needed, leave blank. |
| 59 |
|
| 60 |
Optionally, you can specify the scope to use as follows: |
| 61 |
|
| 62 |
.. code-block:: python |
| 63 |
|
| 64 |
SOCIALACCOUNT_PROVIDERS = { |
| 65 |
'google': { |
| 66 |
'SCOPE': [ |
| 67 |
'profile', |
| 68 |
'email', |
| 69 |
], |
| 70 |
'AUTH_PARAMS': { |
| 71 |
'access_type': 'online', |
| 72 |
}, |
| 73 |
'OAUTH_PKCE_ENABLED': True, |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
By default (if you do not specify ``SCOPE``), ``profile`` scope is |
| 78 |
requested, and optionally ``email`` scope depending on whether or not |
| 79 |
``SOCIALACCOUNT_QUERY_EMAIL`` is enabled. |
| 80 |
|
| 81 |
You must set ``AUTH_PARAMS['access_type']`` to ``offline`` in order to |
| 82 |
receive a refresh token on first login and on reauthentication requests |
| 83 |
(which is needed to refresh authentication tokens in the background, |
| 84 |
without involving the user's browser). When unspecified, Google defaults |
| 85 |
to ``online``. |
| 86 |
|
| 87 |
By default, the userinfo endpoint will not be fetched. In most cases, |
| 88 |
this will be fine, as most in scope user data is gained via decoding |
| 89 |
the JWT. However if users have a private style of avatar_url |
| 90 |
then this will not ordinarily be returned in the JWT and |
| 91 |
as such, subsequent calls to get_avatar_url will return None. |
| 92 |
|
| 93 |
You can optionally specify the following setting so that the userinfo |
| 94 |
endpoint will be used to populate the avatar_url for those users |
| 95 |
who have a private style of avatar_url. |
| 96 |
|
| 97 |
.. code-block:: python |
| 98 |
|
| 99 |
SOCIALACCOUNT_PROVIDERS = { |
| 100 |
'google': { |
| 101 |
'FETCH_USERINFO' : True |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
One Tap Sign-In |
| 106 |
*************** |
| 107 |
|
| 108 |
One Tap Sign-In can be enabled by adding a snippet like this snippet to your |
| 109 |
template:: |
| 110 |
|
| 111 |
<script src="//accounts.google.com/gsi/client" async></script> |
| 112 |
<div id="g_id_onload" |
| 113 |
data-client_id="123-secret.apps.googleusercontent.com" |
| 114 |
data-login_uri="{% url 'google_login_by_token' %}"> |
| 115 |
</div> |
| 116 |
|
| 117 |
Follow the `Sign In with Google for Web`_ guide for more information. |
| 118 |
|
| 119 |
.. _Sign In with Google for Web: https://developers.google.com/identity/gsi/web/guides/overview |