2
3

More than 1 year has passed since last update.

oauthlibでGoogle認証を実装する方法

Posted at

はじめに

検索したけど見つからなかったので、備忘録として。

レシピ

  • Python >= 3.7
  • oauthlib >= 3.2.0

Code

必要というか、メインの部分だけ。

import os
import requests
from typing import Optional
from requests_oauthlib import OAuth2Session

class GoogleAuthService:
  GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
  GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
  OAUTH_URL = "https://accounts.google.com/o/oauth2/auth"
  TOKEN_URL = "https://oauth2.googleapis.com/token"
  USER_INFO_URL = "https://www.googleapis.com/oauth2/v1/userinfo"
  SCOPES = ["profile", "email"]
  
  def get_authorization_url(self) -> str:
      # ここでGoogle認証画面に遷移するためのURLを取得する
      # OAuthにない任意のパラメータをもたせたい場合は、 state を使うとよい
      # ex)
      #   # name を渡したい場合
      #   exp = int(datetime.utcnow().timestamp()) + 30
      #   encoded = jwt.encode({"exp": exp, "name": "hoge"}, "secrets", algorithm="HS256")
      #   state = encoded.decode("utf-8")
      #   redirect_url, _ = self._client().authorization_url(self.OAUTH_URL, state=state)

      redirect_url, _ = self._client().authorization_url(self.OAUTH_URL)
      return redirect_url

  def fetch_token(self, code: str) -> str:
      # Googleからリダイレクトして来たときにURLに付与されている code を使って token を取得する
      token = self._client().fetch_token(
          self.TOKEN_URL,
          client_secret=self.GOOGLE_CLIENT_SECRET,
          code=code,
      )
      return token["access_token"]

  def get_user_info(self, access_token: str) -> Optional[dict]:
      # access_token を使って、ユーザ情報を取得する
      endpoint_url = f"{self.USER_INFO_URL}?access_token={access_token}"
      response = requests.get(endpoint_url)
      if response.status_code == 200:
          return response.json()

      return None

  def _client(self) -> OAuth2Session:
      return OAuth2Session(
          self.GOOGLE_CLIENT_ID,
          scope=self.SCOPES,
      )  

あとは、これらのメソッドを呼び出すように実装を足すくらい。

  • Googleとのリダイレクトを行うフロントエンド
    • Reactとか?
  • フロントエンドとやり取りをするバックエンドのエンドポイント
    • Flaskとか?

気が向いたら、サンプルコードをGithubにあげる。

ちなみに、Github認証は、以下の部分を変更したら同じコードを使って行うことが可能。

  • GOOGLE_CLIENT_ID
  • GOOGLE_CLIENT_SECRET
  • OAUTH_URL
  • TOKEN_URL
  • USER_INFO_URL
  • SCOPES
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3