Googleログイン認証をWebサイトに組み込む
Googleログイン認証をAngularを使ったWebサイトに組み込む手順を解説します。
動作イメージ
Google API クライアント ID を取得する
Google API クライアント ID を取得するの手順に従い、Google API クライアント IDを取得します。
👇プロジェクトを作成


👇認証情報(OAuthクライアントID)を作成

👇同意画面を設定

👇UserType:外部を指定し作成

👇アプリ情報を設定


👇スコープを追加


👇アプリを公開

👇認証情報(OAuthクライアントID)を作成


👇クライアントID/クライアントシークレットをコピー

Angular 構成の準備
以降の手順は以下記事の内容を前提とします。
Angularのインストール~最小限構成の設定手順
Google認証の実装
Google に認証リクエストを送信する
👇認証リクエストを送信
https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=[クライアントID]&scope=openid%20email%20profile&redirect_uri=http%3A//localhost%3A4200&nonce=20240308&access_type=offline
👇リダイレクトされたURLパラメータから認証トークンを取得します
ngOnInit(): void {
let tmp_code = this.getParamValue("code");
}
getParamValue(key: string): string{
let ret_value = "";
if(location.search.substring(1) != ""){
var pair = location.search.substring(1).split('&');
for(var i=0;pair[i];i++) {
var kv = pair[i].split('=');
if(kv[0] == key){
ret_value = kv[1];
break;
}
}
}
return ret_value;
}