0
0

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

GCP VideoIntelligenceServiceClientのエラー対処法:"missing fields token_uri, client_email."

Last updated at Posted at 2021-07-25

##概要

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ファイルを使うことができました。

しかし、VideoIntelligenceServiceを使うときには、Viewer権限のアカウントに紐づく認証用ファイルを読み込ませないと、__"missing fields token_uri, client_email."__エラーが起きることがわかりました。

####書式エラーが吐かれたGCPアカウント認証用jsonファイル

NGファイル(オーナー権限のアカウント認証ファイル)
{"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ファイル

OKファイル(Viewer権限のアカウント認証ファイル)
{
  "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用モジュールを取得

Terminal
electron@diynoMacBook-Pro gcp % pip3 install -U google-cloud-videointelligence

( 省略 )

electron@diynoMacBook-Pro gcp % 
Terminal
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ファイルを確認

Terminal
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ファイルは、以下で取得できる。

  1. GCPコンソール画面で、ViewerのRole権限を持つアカウントを作成する。
  2. そのアカウントの認証用用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ファイルをダウンロード

以下の手順を実行する。

  1. GCPコンソール画面で、ViewerのRole権限を持つアカウントを作成する。
  2. そのアカウントの認証用用jsonファイルをダウンロードする。

ダウンロードしたjsonファイル

Terminal
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 % 

###今度は成功

Terminal
>>> 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)
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?