Ruby を使用して GCP Bearer トークンを生成する
概要
gcloud を使用して行うのと同じように、サービス アカウントの Ruby ライブラリを使用して認証ベアラー トークンを生成したいと考えています。 gcloud auth application-default print-access-token
簡単なコードを書きました。
require 'googleauth'
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: File.open('sa.json'))
token=authorizer.fetch_access_token!
トークンを取得できますが、これには範囲が必要です。以下の gcloud auth コマンドと同様に、スコープなしでトークンを生成できるかどうかを確認したかったのです。
Python でも同様の質問があったようです。 Ruby に似たものを探しています。
解決策
他の皆さんのために:
require 'googleauth'
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: File.open(path_to_json_file),
scope: 'https://www.googleapis.com/auth/cloud-platform')
token = authorizer.fetch_access_token!