ドライブ サービス アカウントをインスタンス化するためのサンプル コードを取得するのに問題があります。指示どおりに API コンソールでサービス アカウントを設定し、「https://www.googleapis.com/auth/drive」のスコープを含めましたが、これを実行すると次のエラーが生成されます:「認証に失敗しました。サーバー メッセージ: (Signet::AuthorizationError)".
奇妙なことに、user_email アドレスを省略してもエラーは発生しません。
私の目的は、組織のドライブに保存されているすべてのファイルを監査できるようにすることです。保存されているすべてのファイルのリストを取得するには、サービス アカウントを使用する方法になると理解しています。
このためのサーバー側の特別な設定を見逃していませんか?
require 'google/api_client'
## Email of the Service Account #
SERVICE_ACCOUNT_EMAIL = '<service account email>@developer.gserviceaccount.com'
## Path to the Service Account's Private Key file #
SERVICE_ACCOUNT_PKCS12_FILE_PATH = '<private key file>-privatekey.p12'
def build_client(user_email)
key = Google::APIClient::PKCS12.load_key(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'notasecret')
asserter = Google::APIClient::JWTAsserter.new(SERVICE_ACCOUNT_EMAIL, 'https://www.googleapis.com/auth/drive', key)
client = Google::APIClient.new
client.authorization = asserter.authorize(user_email)
return client
end
client = build_client("<users email address>")