DjangoのGoogle認証
12243 ワード
こんにちはみんなこの時間はどのようにGoogleの認証を我々のdjango webappに追加することができますを見ることができます
では、Google認証を追加するために必要なパッケージをWebアプリケーションにインストールしましょう
AuthenticationMountバックエンドプロパティを設定で指定する必要があります.パイ
あなたの設定に次のアプリを追加する必要があります.あなたがdjangoを加える必要があるPy注意.contrib.サイトは、それが存在しない場合ApplalledConeアプリに
下記のコードスニペットを使用してプロジェクトにdjangoのallauth URLを含める
移行を実行するには、以下の2つのコマンドを使用します
スーパーユーザーを作成するには、次のコマンドを使用します
さて、次のリンクを訪問してGoogle Developerダッシュボードをご覧ください.google api console
現在、Gmailや市場の下で検索バーの検索では、GmailのAPIを選択
Gmail APIを有効にする
今すぐ訪問credentials ページをクリックし、“+作成資格情報”ボタンをクリックし、"OAuthクライアントID "をクリックします
次のように、Webとしてアプリケーションの種類を選択します
今、私はあなたが何をする必要がありますアプリケーションの名前は、私はdjango Google Authとしてそれを命名していますが、私たちが念頭に置いておく必要が主なことは、リダイレクトのURLとJavascript
画面上に表示されるクライアントIDと秘密をコピーする
設定で.Py以下のコード行を追加する
今社会的なアプリケーションに移動し、そこにGoogleクライアントのIDと秘密を追加
あなたは、我々がcliend idと秘密を加えた方法を上で見ることができて、また、ChosenホストにLocalhostを加えるのを忘れないでください
訪問http://127.0.0.1:8000/accounts/login/
Googleでログインのリンクを見ることができます.リンクをクリックしてログインすることができます
ログインページ
グーグルAuth
以下のコードスニペットを使用してカスタムログインページを作成できます
Djangoプロジェクトの設定
django-admin startproject GoogleAuthentication
ここでは、プロジェクトの名前は文字通り任意のものになることができます.Django allauthパッケージのインストール
では、Google認証を追加するために必要なパッケージをWebアプリケーションにインストールしましょう
pip install django-allauth
3 .認証バックエンドの設定パイ
AuthenticationMountバックエンドプロパティを設定で指定する必要があります.パイ
# settings.py
# all the below property in your settings.py file for django allauth to work
AUTHENTICATION_BACKENDS = [
# django's inbuild authentication backend
'django.contrib.auth.backends.ModelBackend',
# django's allauth authentication backend
'allauth.account.auth_backends.AuthenticationBackend',
]
4 .設定で必要なアプリを追加します。パイ
あなたの設定に次のアプリを追加する必要があります.あなたがdjangoを加える必要があるPy注意.contrib.サイトは、それが存在しない場合ApplalledConeアプリに
# settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sites', # <-- add the sites app here
# apps needed for django all auth
'allauth',
'allauth.account',
'allauth.socialaccount',
# add the below app for google authentication to work
'allauth.socialaccount.providers.google',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
メインURLにallauth URLを追加します。パイ
下記のコードスニペットを使用してプロジェクトにdjangoのallauth URLを含める
# main urls.py file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')), # <- include the allauth urls here
]
6 .移行を実行する
移行を実行するには、以下の2つのコマンドを使用します
python manage.py makemigrations
python manage.py migrate
7 .スーパーユーザを作る
スーパーユーザーを作成するには、次のコマンドを使用します
python manage.py createsuperuser
出力Username (leave blank to use 'rohit'): admin
Email address: admin@admin.com
Password:
Password (again):
Superuser created successfully.
作成するGoogleクライアントのIDと秘密
さて、次のリンクを訪問してGoogle Developerダッシュボードをご覧ください.google api console
現在、Gmailや市場の下で検索バーの検索では、GmailのAPIを選択

Gmail APIを有効にする

今すぐ訪問credentials ページをクリックし、“+作成資格情報”ボタンをクリックし、"OAuthクライアントID "をクリックします

次のように、Webとしてアプリケーションの種類を選択します

今、私はあなたが何をする必要がありますアプリケーションの名前は、私はdjango Google Authとしてそれを命名していますが、私たちが念頭に置いておく必要が主なことは、リダイレクトのURLとJavascript
# add the following to redirect urls
http://127.0.0.1:8000/accounts/google/login/callback/
http://localhost:8000/accounts/google/login/callback/
# add the following to javascript origins
http://127.0.0.1:8000
http://localhost:8000
上記のURLを追加した後、Create
画面上に表示されるクライアントIDと秘密をコピーする
9 .クライアントIDと秘密を設定する
設定で.Py以下のコード行を追加する
# settings.py
SITE_ID = 1
django管理ページにログインし、サイトの例を編集します.COMへのCOM
今社会的なアプリケーションに移動し、そこにGoogleクライアントのIDと秘密を追加

あなたは、我々がcliend idと秘密を加えた方法を上で見ることができて、また、ChosenホストにLocalhostを加えるのを忘れないでください
訪問http://127.0.0.1:8000/accounts/login/
Googleでログインのリンクを見ることができます.リンクをクリックしてログインすることができます
ログインページ

グーグルAuth

以下のコードスニペットを使用してカスタムログインページを作成できます
<html>
<head>
<!--Dont forget to replace with client id below-->
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
</head>
<body>
<div id="my-signin2"></div>
<script>
function onSuccess(googleUser) {
console.log('Logged in as: ' + googleUser.getBasicProfile().getName());
}
function onFailure(error) {
console.log(error);
}
function renderButton() {
gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': onSuccess,
'onfailure': onFailure
});
}
</script>
<script src="https://apis.google.com/js/platform.js?onload=renderButton" async defer></script>
</body>
</html>
Reference
この問題について(DjangoのGoogle認証), 我々は、より多くの情報をここで見つけました https://dev.to/rohit20001221/google-authentication-in-django-the-simplest-way-3982テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol