##概要
GCP VideoIntelligenceServiceClientのインスタンスを作成しようとして、__"missing fields token_uri, client_email."__が起きたら、Viewer権限の認証用jsonファイルを読み込ませたかどうか、確認してみてください。
>>> creds = service_account.Credentials.from_service_account_info(info)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.9/site-packages/google/oauth2/service_account.py", line 220, in from_service_account_info
signer = _service_account_info.from_dict(
File "/usr/local/lib/python3.9/site-packages/google/auth/_service_account_info.py", line 49, in from_dict
raise ValueError(
ValueError: Service account info was not in the expected format, missing fields token_uri, client_email.
>>>
GCPの静止画像用のAPIを叩くときには、オーナー権限の認証用jsonファイルを使うことができました。
- Google Cloud Platformの画像の撮影場所を言い当てるAPIは、なかなかの優れもの
- MacbookからGoogle Cloud Platformの画像ラベル出力APIを動かしてみた
- Google Cloud Platformでのアカウント登録から、MacbookからAPIを叩いて成功するまで
しかし、VideoIntelligenceServiceを使うときには、Viewer権限のアカウントに紐づく認証用ファイルを読み込ませないと、__"missing fields token_uri, client_email."__エラーが起きることがわかりました。
####書式エラーが吐かれたGCPアカウント認証用jsonファイル
{"installed":{"client_id":"XXXXXXXXX.apps.googleusercontent.com","project_id":"electroncgpvision","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"ZZZZZZ","client_secret":"YYYYYYYYYYYYYYYY","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
####OKだったGCPアカウント認証用jsonファイル
{
"type": "service_account",
"project_id": "electroncgpvision",
"private_key_id": "AAAAAAAAAAAAA",
"private_key": "-----BEGIN PRIVATE KEY-----AAAAAAAAAAAAABBBBBBBBBBBBBBCCCCCCCCCCCCCCCDDDDDDDDDDDDDDEEEEEEEEEEEEFFFFFFFFFFF -----END PRIVATE KEY-----\n",
"client_email": "xxx@electroncgpvision.com",
"client_id": "1111112222222223333333333444444444555556666677777",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "ZZZZZZ",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/electron_viewer%40electroncgpvision.iam.ZZZ.com"
}
####( 実行成功画面 )
>>> service_account_key_name = "electroncgpvision-abcde.json"
>>> info = json.load(open(service_account_key_name))
>>> creds = service_account.Credentials.from_service_account_info(info)
>>>
>>> video_client = videointelligence.VideoIntelligenceServiceClient(credentials=creds)
##( 試行錯誤した履歴 )
以下、試行錯誤した履歴を載せておきます。
####GCP用モジュールを取得
electron@diynoMacBook-Pro gcp % pip3 install -U google-cloud-videointelligence
( 省略 )
electron@diynoMacBook-Pro gcp %
electron@diynoMacBook-Pro gcp % python3
>>> import json
>>> from google.cloud import videointelligence
>>> from google.oauth2 import service_account
>>>
>>> service_account_key_name = "/Users/electron/Desktop/gcp/XXYYZZAABBCCDD.json"
>>> info = json.load(open(service_account_key_name))
>>>
>>> creds = service_account.Credentials.from_service_account_info(info)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.9/site-packages/google/oauth2/service_account.py", line 220, in from_service_account_info
signer = _service_account_info.from_dict(
File "/usr/local/lib/python3.9/site-packages/google/auth/_service_account_info.py", line 49, in from_dict
raise ValueError(
ValueError: Service account info was not in the expected format, missing fields token_uri, client_email.
>>>
####エラー発生:読み込んだjsonファイルの書式が違うらしい
ValueError: Service account info was not in the expected format, missing fields token_uri, client_email.
####読み込んだjsonファイルを確認
electron@diynoMacBook-Pro gcp % cat XXYYZZAABBCCDD.json
{"installed":{"client_id":"XXXXXXXXX.apps.googleusercontent.com","project_id":"electroncgpvision","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"ZZZZZZ","client_secret":"YYYYYYYYYYYYYYYY","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
electron@diynoMacBook-Pro gcp %
####以下の書式を持つ認証用jsonファイルが必要
この形式の認証用jsonファイルは、以下で取得できる。
- GCPコンソール画面で、ViewerのRole権限を持つアカウントを作成する。
- そのアカウントの認証用用jsonファイルをダウンロードする。
You need to get the service account file that contains the private key credentials, >it's basically a different file from the one you have. You can get it, or get a new >one by going to https://console.developers.google.com/iam-admin/iam/ then >select your project, then select "Service accounts" and create a new one as role >"viewer" for the project for example (or use one that already exists and click >"create new key")
The "key" is a json or p12 file that will be downloaded when you create the >account (or use "create new key" there) which contains the correct fields and >credentials that will work for your code.
Example structure of the downloaded "key" file (when selecting JSON):{
"type": "service_account",
"project_id": "zeta-handler-9999",
"private_key_id": "123456789deedbeaf",
"private_key": "-----BEGIN PRIVATE KEY-----\nREDACTED REDACTED...-----END PRIVATE KEY-----\n",
"client_email": "projectname-service-account@zeta-handler-9999.iam.gserviceaccount.com",
"client_id": "12345678909999",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/projectname-service-account%40zeta-handler-9999.iam.gserviceaccount.com"
}
####Viewer権限のアカウントを作成し、認証jsonファイルをダウンロード
以下の手順を実行する。
- GCPコンソール画面で、ViewerのRole権限を持つアカウントを作成する。
- そのアカウントの認証用用jsonファイルをダウンロードする。
ダウンロードしたjsonファイル
electron@diynoMacBook-Pro gcp % cat electroncgpvision- abcde.json
{
"type": "service_account",
"project_id": "electroncgpvision",
"private_key_id": "AAAAAAAAAAAAA",
"private_key": "-----BEGIN PRIVATE KEY-----AAAAAAAAAAAAABBBBBBBBBBBBBBCCCCCCCCCCCCCCCDDDDDDDDDDDDDDEEEEEEEEEEEEFFFFFFFFFFF -----END PRIVATE KEY-----\n",
"client_email": "xxx@electroncgpvision.com",
"client_id": "1111112222222223333333333444444444555556666677777",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "ZZZZZZ",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/electron_viewer%40electroncgpvision.iam.ZZZ.com"
}
electron@diynoMacBook-Pro gcp %
###今度は成功
>>> service_account_key_name = "electroncgpvision-abcde.json"
>>> info = json.load(open(service_account_key_name))
>>> creds = service_account.Credentials.from_service_account_info(info)
>>>
>>> video_client = videointelligence.VideoIntelligenceServiceClient(credentials=creds)