0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Google API】OAuth認証時に毎回refresh_tokenを発行する

Last updated at Posted at 2021-03-18

はじめに

Google APIのOAuth認証時に毎回refresh_tokenを発行したい場合があると思います。
refresh_tokenは、下記の条件以外で2回目のOAuth認証時に受け取ることができません。受け取れない理由は、すでに発行したrefresh_tokenを再利用することを想定しいてるからです。

アクセストークンの有効期限の条件

  • ユーザーが アプリのアクセスを取り消しました。
  • 更新トークンは6ヶ月間使用されていません。
  • ユーザーがパスワードを変更し、更新トークンにGmailスコープが含まれています。
  • ユーザーアカウントが、付与されたアクセストークンの最大数を超えました。

結論

リクエストの認証パラメータにprompt=consentを設定します。
このパラメータを設定することでOAuth認証時に毎回同意画面が表示され、ユーザーが許可ボタンを押下すると、refresh_tokenを取得することができます。

こんな感じのリクエストです↓

https://accounts.google.com/o/oauth2/auth
?client_id=hogehoge
&redirect_uri=http://localhost
&scope=https://www.googleapis.com/auth/drive
&response_type=code
&access_type=offline
&prompt=consent

promptは、2回目の同意画面の関して制限できるオプションです。
promptの設定可能な値

説明
none 認証画面や同意画面を表示しないでください。他の値と一緒に指定してはなりません。
consent ユーザーに同意を求めます。
select_account アカウントを選択するようにユーザーに促します。
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?