Skip to content

Commit 4e7f653

Browse files
committed
Update documentation with Google example. Add extra formatting for readability.
1 parent 229e552 commit 4e7f653

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

README.rst

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ The settings of this app are:
9696
- DRFSO2_PROPRIETARY_BACKEND_NAME sets the name of your Oauth2 social backend (e.g Facebook), defaults to "Django"
9797
- DRFSO2_URL_NAMESPACE sets the namespace for reversing urls.
9898

99+
Setting up Application
100+
**********************
99101

100102
Now go to django admin and add a new Application.
101103
- client_id and client_secret shouldn't be changed
@@ -228,6 +230,10 @@ To use Facebook as the authorization backend of your django-rest-framework api,
228230
'fields': 'id, name, email'
229231
}
230232
233+
Remember to add new Application in your Django admin (see section "Setting up Application").
234+
235+
Testing
236+
*******
231237

232238
- You can test these settings by running the following command :
233239

@@ -240,3 +246,88 @@ You can find the id and secret of your app at https://developers.facebook.com/ap
240246
For testing purposes you can use the access token `<user_access_token>` from https://developers.facebook.com/tools/accesstoken/.
241247

242248
For more information on how to configure python-social-auth with Facebook visit http://python-social-auth.readthedocs.io/en/latest/backends/facebook.html.
249+
250+
251+
Google Example
252+
--------------------
253+
254+
To use Google OAuth2 as the authorization backend of your django-rest-framework api, your settings.py file should look like this:
255+
256+
.. code-block:: python
257+
258+
INSTALLED_APPS = (
259+
...
260+
# OAuth
261+
'oauth2_provider',
262+
'social_django',
263+
'rest_framework_social_oauth2',
264+
)
265+
266+
TEMPLATES = [
267+
{
268+
...
269+
'OPTIONS': {
270+
'context_processors': [
271+
...
272+
# OAuth
273+
'social_django.context_processors.backends',
274+
'social_django.context_processors.login_redirect',
275+
],
276+
},
277+
}
278+
]
279+
280+
REST_FRAMEWORK = {
281+
...
282+
'DEFAULT_AUTHENTICATION_CLASSES': (
283+
...
284+
# OAuth
285+
# 'oauth2_provider.ext.rest_framework.OAuth2Authentication', # django-oauth-toolkit < 1.0.0
286+
'oauth2_provider.contrib.rest_framework.OAuth2Authentication', # django-oauth-toolkit >= 1.0.0
287+
'rest_framework_social_oauth2.authentication.SocialAuthentication',
288+
)
289+
}
290+
291+
AUTHENTICATION_BACKENDS = (
292+
293+
# Others auth providers (e.g. Facebook, OpenId, etc)
294+
...
295+
296+
# Google OAuth2
297+
'social_core.backends.google.GoogleOAuth2',
298+
299+
# django-rest-framework-social-oauth2
300+
'rest_framework_social_oauth2.backends.DjangoOAuth2',
301+
302+
# Django
303+
'django.contrib.auth.backends.ModelBackend',
304+
305+
)
306+
307+
# Google configuration
308+
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = <your app key goes here>
309+
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = <your app secret goes here>
310+
311+
# Define SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE to get extra permissions from Google.
312+
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
313+
'https://www.googleapis.com/auth/userinfo.email',
314+
'https://www.googleapis.com/auth/userinfo.profile',
315+
]
316+
317+
Remember to add new Application in your Django admin (see section "Setting up Application").
318+
319+
Testing
320+
*******
321+
322+
You can test these settings by running the following command :
323+
324+
curl -X POST -d "grant_type=convert_token&client_id=<django-oauth-generated-client_id>&client_secret=<django-oauth-generated-client_secret>&backend=google-oauth2&token=<google_token>" http://localhost:8000/auth/convert-token
325+
326+
This request returns the "access_token" that you should use on all HTTP requests with DRF. What is happening here is that we are converting a third-party access token (<user_access_token>) in an access token to use with your api and its clients ("access_token"). You should use this token on each and further communications between your system/application and your api to authenticate each request and avoid authenticating with Google every time.
327+
328+
You can find the id and secret of your app at https://console.developers.google.com/apis/credentials
329+
and more information on how to create one on https://developers.google.com/identity/protocols/OAuth2.
330+
331+
For testing purposes you can use the access token `<user_access_token>` from https://developers.google.com/oauthplayground/.
332+
333+
For more information on how to configure python-social-auth with Google visit https://python-social-auth.readthedocs.io/en/latest/backends/google.html#google-oauth2.

0 commit comments

Comments
 (0)