12
6

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 5 years have passed since last update.

何が何でもGoogle APIのRefresh Tokenを取得したい

Posted at

Google APIにRefresh Tokenを返してもらうようにするには、acces-type=offlineapproval-prompt=forceでトークンを要求すると良いよって日本語・英語問わずそこかしこに書いてあります。
たとえば、Passportを使っていたらこんな感じにってね。

example1.js
  .get('/', passport.authorize('google-authz', {
    failureRedirect: '/',
    scope: [
      'https://www.googleapis.com/auth/calendar',
      'profile'
    ],
    accessType: 'offline',
    approvalPrompt: 'force',
    session: false
  }))

これでテストすると実際にRefresh Tokenを取得できて、「やったー!」って思うんだけど、2回目以降はRefresh Tokenが帰ってこないんです。しょぼーん。。。

あーでも無いこーでも無いと悩んだ挙句、見つけました。

Get refresh token google apiの中の小さなコメント

Edited answer to something that is working as this answer is old and no longer seems to work. I spent significant time trying this as there are multiple places saying to use this method. I finally read the documentation (which I should have done first) and it says you must use prompt=consent. Reference: developers.google.com/identity/protocols/… – Goblinlord Jan 5 at 0:49

Goblinlordさん、ありがとう!

分かってからよく見ると、Shahadat Hossain Khanさんも答えてました。

ということで、上の例は次のようにするのが正解みたい。

example2.js
  .get('/', passport.authorize('google-authz', {
    failureRedirect: '/',
    scope: [
      'https://www.googleapis.com/auth/calendar',
      'profile'
    ],
    accessType: 'offline',
    prompt: 'consent',
    session: false
  }))

こうすると、ちゃんと毎回Refresh Tokenを取得できました。


今日の教訓

I finally read the documentation (which I should have done first) ...

which I should have done first, which I should have ...
(゚◇゚)ゞ

ではまたね。

12
6
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
12
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?