Integrate Google OAuth in your Django Rest-framework project
Project description
Django Google Integrations
Django Google Integrations is a package that allows integrating Google OAuth into your Django application. It is build as a thin wrapper around the google-auth-oauthlib. You can view the full documentation at https://PrimedigitalGlobal.github.io/django-google-integrations/
Features
- Provides following APIs:
- Authorization URL API:
- It generates google
authorization-url
which redirects user to Google's Authorization Server to request consent from resource owner.
- It generates google
- Authorize Web API:
- Exchange authorization code for access token.
- Talk to resource server with access token and fetch user's profile information.
- Authorize IOS Token API:
- Verifies an ID Token issued by Google's OAuth 2.0 authorization server.
- Fetch user details from decoded token.
- Authorization URL API:
Dependencies
- Python >= 3.6
- Django >= 2.2.17
- djangorestframework >= 3.10.2
- google-api-python-client >= 2.9.0
- google-auth-httplib2 >= 0.1.0
- google-auth-oauthlib >= 0.4.1
Setup
You can install the library directly from pypi using pip:
$ pip install django-google-integrations
Edit your settings.py file:
INSTALLED_APPS = (
...
"django_google_integrations"
)
# Django Google Integrations Config
GOOGLE_CONFIG = {
"CLIENT_CONFIG_JSON": "[Google Client Config Json]",
"CLIENT_ID": "[Google Client ID]",
"CLIENT_SECRET": "[Google Client Secret]",
"SERVICE_ACCOUNT_SCOPES": [
"openid",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
],
"REDIRECT_URI": "http://localhost:3000/google/auth/callback",
"RESPONSE_HANDLER_CLASS": "example.testapp.google_response_handler.GoogleSigninResponseHandler",
}
Create Response Handler Class and update path in GOOGLE_CONFIG
from django_google_integrations.services import GoogleResponseHandler
class GoogleSigninResponseHandler(GoogleResponseHandler):
def handle_fetch_or_create_user(self, flow, google_user_data):
email = google_user_data.get("email", None)
user = get_user_by_email(email)
is_created = False
if not user:
user_dict = {
"first_name": google_user_data.get("given_name", ""),
"last_name": google_user_data.get("family_name", ""),
"password": None,
}
user = create_user_account(email, **user_dict)
is_created = True
extra_context = {"is_created": is_created}
return user, extra_context
def generate_response_json(self, user, extra_context):
response = AuthUserSerializer(user)
return response.data
NOTE:
AuthUserSerializer
used in above ref. could be created as per app's functionality and contain fields which needs to be sent in response of authorization.- Following service methods are used in above code ref. which could be created as per app's functionality:
get_user_by_email
create_user_account
Update URLs
from django_google_integrations.apis import GoogleAuthViewSet
default_router = routers.DefaultRouter(trailing_slash=False)
default_router.register("auth/google", GoogleAuthViewSet, basename="google-auth")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django-google-integrations-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 967fe8b82634c84ebddfe0b575fffa39a7890457e8b0040c304ac9a4097877e1 |
|
MD5 | 139564c8a16ef373b55819e3da7b1b5e |
|
BLAKE2b-256 | 9322c491d41b42bc31f61a0f11b8b1f182d97f153e634d98b968197bf2a74140 |
Close
Hashes for django_google_integrations-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6f84807fdf5c99ff0d0a8648e36ead64ddc4a50efb9fcfcb5071dbea94ff9c1 |
|
MD5 | a4661f6215c2313c7b8a07e364bc0b02 |
|
BLAKE2b-256 | 63862183bcde1bb79a711ec7a37019b07275ed64fe9aa45ed8ad5a039b0742b6 |