我正在尝试在MacOS Sierra上运行Google Vision APIs的快速启动演示。
def run_quickstart():
# [START vision_quickstart]
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
# Instantiates a client
vision_client = vision.Client()
# 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 = vision_client.image(
content=content)
# Performs label detection on the image file
labels = image.detect_labels()
print('Labels:')
for label in labels:
print(label.description)
# [END vision_quickstart]
if __name__ == '__main__':
run_quickstart()
脚本如上所示。我正在使用服务帐户密钥文件进行身份验证。正如文档建议的那样,我已经通过pip安装了google-vision依赖项,并使用
设置了一个环境变量export GOOGLE_APPLICATION_CREDENTIALS=/my_credentials.json
正确设置了环境变量。还是脚本加注,
oauth2client.client.HttpAccessTokenRefreshError:invalid_grant:无效的JWT签名。
使用API密钥时,如果未提及使用服务帐户文件,则会出现类似的问题。