Service Account で API を利用する時のアクセストークンの再取得について

988 views
Skip to first unread message

wezardnet

unread,
Jul 20, 2016, 12:35:31 AM7/20/16
to Google Apps API Japan
お世話になります wezardnet です。

Service Account を使って Google API を利用する時に、次のように GoogleCredential を生成してアクセストークンを取得しています。


GoogleCredential credential = new GoogleCredential.Builder()
 
.setTransport(HTTP_TRANSPORT)
 
.setJsonFactory(JSON_FACTORY)
 
.setServiceAccountId("sam...@xxxxxx.iam.gserviceaccount.com")
 
.setServiceAccountScopes(SCOPES)
 
.setServiceAccountUser("{Google Apps のユーザー名(メールアドレス)}")
 
.setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
 
.build() ;

credential
.refreshToken() ;

String accessToken = credential.getAccessToken() ;
String refreshToken = credential.getRefreshToken() ; これが null になる


アクセストークンの有効期限切れの場合、リフレッシュトークンを使って再発行するのが通常の OAuth の流れになると思うのですが、
初回にリフレッシュトークンが取得できません。

Service Account の場合は、毎回アクセストークンを取得するモノなのでしょうか?

毎回取得していると Google Apps のトークン監査ログに取得のたびに、「XXXX さんが ****** のスコープについて Application Name へのアクセスを承認しました」とログが残るのであまり良い気はしないです。

Keisuke Oohashi

unread,
Jul 20, 2016, 7:06:16 AM7/20/16
to Google Apps API Japan

たしかそのはずです。

公式ドキュメントにも以下のように書いてます。
https://developers.google.com/identity/protocols/OAuth2#serviceaccount

"When the token expires, the application repeats the process"


2016/07/20 (水) 1:35PM wezardnet <wezard...@gmail.com>:
--
このメールは Google グループのグループ「Google Apps API Japan」に登録しているユーザーに送られています。
このグループから退会し、グループからのメールの配信を停止するには google-apps-api-...@googlegroups.com にメールを送信してください。
このグループに投稿するには google-app...@googlegroups.com にメールを送信してください。
その他のオプションについては https://groups.google.com/d/optout にアクセスしてください。

Tatsuya Nakano

unread,
Jul 20, 2016, 7:51:47 AM7/20/16
to Google Apps API Japan
中野です。

私の知る限りもリフレッシュトークンは取れないですね。

以下の3つしか取れないようです。
{
  "access_token" : "1/8xbJqaOZXSUZbHLl5EOtu1pxz3fmmetKx9W8CV4t79M",
  "token_type" : "Bearer",
  "expires_in" : 3600
}
 

2016年7月20日水曜日 20時06分16秒 UTC+9 soundTricker:

たしかそのはずです。

公式ドキュメントにも以下のように書いてます。
https://developers.google.com/identity/protocols/OAuth2#serviceaccount

"When the token expires, the application repeats the process"


2016/07/20 (水) 1:35PM wezardnet <wezard...@gmail.com>:
お世話になります wezardnet です。

Service Account を使って Google API を利用する時に、次のように GoogleCredential を生成してアクセストークンを取得しています。


GoogleCredential credential = new GoogleCredential.Builder()
 
.setTransport(HTTP_TRANSPORT)
 
.setJsonFactory(JSON_FACTORY)
 
.setServiceAccountId("sam...@xxxxxx.iam.gserviceaccount.com")
 
.setServiceAccountScopes(SCOPES)
 
.setServiceAccountUser("{Google Apps のユーザー名(メールアドレス)}")
 
.setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
 
.build() ;

credential
.refreshToken() ;

String accessToken = credential.getAccessToken() ;
String refreshToken = credential.getRefreshToken() ; これが null になる


アクセストークンの有効期限切れの場合、リフレッシュトークンを使って再発行するのが通常の OAuth の流れになると思うのですが、
初回にリフレッシュトークンが取得できません。

Service Account の場合は、毎回アクセストークンを取得するモノなのでしょうか?

毎回取得していると Google Apps のトークン監査ログに取得のたびに、「XXXX さんが ****** のスコープについて Application Name へのアクセスを承認しました」とログが残るのであまり良い気はしないです。

--
このメールは Google グループのグループ「Google Apps API Japan」に登録しているユーザーに送られています。
このグループから退会し、グループからのメールの配信を停止するには google-apps-api-japan+unsub...@googlegroups.com にメールを送信してください。

wezardnet

unread,
Jul 20, 2016, 7:00:23 PM7/20/16
to Google Apps API Japan
soundTricker さん、中野さん、返信ありがとうございます!

やはりそうなんですね。正しい挙動を知りたくて、みなさんのご意見をお聞きしたかったので助かりました。

たしかに公式ドキュメントのご指摘の部分に記載がありますね。expires の値を見て期限切れなら Credential 生成して取得という流れで良いみたいです。



2016年7月20日水曜日 13時35分31秒 UTC+9 wezardnet:
お世話になります wezardnet です。

Service Account を使って Google API を利用する時に、次のように GoogleCredential を生成してアクセストークンを取得しています。


GoogleCredential credential = new GoogleCredential.Builder()
 
.setTransport(HTTP_TRANSPORT)
 
.setJsonFactory(JSON_FACTORY)

 
.setServiceAccountId("sample@xxxxxx.iam.gserviceaccount.com")

 
.setServiceAccountScopes(SCOPES)
 
.setServiceAccountUser("{Google Apps のユーザー名(メールアドレス)}")
 
.setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
 
.build() ;

credential
.refreshToken() ;

String accessToken = credential.getAccessToken() ;
String refreshToken = credential.getRefreshToken() ; これが null になる

Reply all
Reply to author
Forward
0 new messages