環境
- Python 3.6
- google-cloud-vision 0.34.0
- Windows10
2018/10/10に実行
背景
Google Cloudクライアントライブラリを使って、Cloud Vision APIにアクセスしたいです。
以下のサイトに従って、次のサンプルコードを実行しました。
https://cloud.google.com/vision/docs/libraries#client-libraries-resources-python
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
'resources/wakeupcat.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)
問題
response = client.label_detection(image=image)
の部分で、次のエラーが発生しました。
ServiceUnavailable: 503 Getting metadata from plugin failed with error:
('invalid_grant: Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values and use a clock with skew to account for clock differences between systems.', )
JWTに関する用語が分からなかったので、調べました。
- JWT : JSON Web TOken
- iat:issued at. JWT を発行した時刻を示す
- exp:expiration time. JWT の有効期限を示す
https://openid-foundation-japan.github.io/draft-ietf-oauth-json-web-token-11.ja.html#expDef 引用
「clock with skew」の意味は分かりませんでした。
原因
PCの時刻が約12時間ずれていたことが原因でした。正しい時刻に設定し直したら、上記のコードは動きました。
PCの時刻がずれていたことは気づいていましたが、問題ないと思い放っていました。時刻は常に正しい状態にすべきですね。
参考サイト
BigQueryなどでも同様のメッセージが出るので、どうやらGCP共通のエラーメッセージのようです。