在Docker中运行的Python / Flask中运行Vision API演示时的StatusCode.UNAUTHENTICATED
我已经按照Vision API的Google Client Libraries页面的说明开始使用Python中的Vision API(我正在运行2.7)。 由于我的代码在Docker(一个Flask应用程序)中运行,我按照以下方式按照说明操作:
- 将google-cloud-vision和google-cloud库添加到我的requirements.txt文件中。
- 创build了一个json帐户凭证文件,并将该文件的位置设置为名为GOOGLE_APPLICATION_CREDENTIALS的环境variables
- 成功运行gcloud init,同时“ssh”进入我的Web应用程序的泊坞窗容器
- 将上面链接中的客户端库示例完全复制到testing视图中以运行代码
上面的步骤导致下面的错误:
> RetryError: GaxError(Exception occurred in retry method that was not > classified as transient, caused by <_Rendezvous of RPC that terminated > with (StatusCode.UNAUTHENTICATED, Traceback (most recent call last): > File "src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi", > line 154, in grpc._cython.cygrpc.plugin_get_metadata > (src/python/grpcio/grpc/_cython/cygrpc.c:7054) File > "/usr/local/lib/python2.7/site-packages/grpc/_plugin_wrapping.py", > line 106, in __call__ > AuthMetadataPluginCallback(wrapped_cygrpc_callback)) File "/usr/local/lib/python2.7/site-packages/google/auth/transport/grpc.py", > line 73, in __call__ > callback(self._get_authorization_headers(context), None) File "/usr/local/lib/python2.7/site-packages/google/auth/transport/grpc.py", > line 61, in _get_authorization_headers > headers) File "/usr/local/lib/python2.7/site-packages/google/auth/credentials.py", > line 121, in before_request > self.refresh(request) File "/usr/local/lib/python2.7/site-packages/google/oauth2/service_account.py", > line 310, in refresh > request, self._token_uri, assertion) File "/usr/local/lib/python2.7/site-packages/google/oauth2/_client.py", > line 143, in jwt_grant > response_data = _token_endpoint_request(request, token_uri, body) File > "/usr/local/lib/python2.7/site-packages/google/oauth2/_client.py", > line 98, in _token_endpoint_request > body = urllib.parse.urlencode(body) AttributeError: 'Module_six_moves_urllib_parse' object has no attribute 'urlencode' > )>)
我认为这个问题与我的问题有关,因为它说StatusCode.UNAUTHENTICATED,但我一直没有得到这个固定的。 谁能帮忙? 谢谢!
TL; DR – 您已经观察到您的证书有问题。 使用应用程序默认凭证将会简单得多,并且可以使您的应用程序在多个环境中移植。 更具体地说,如果您已经在gcloud
拥有一组工作证书,那么您可以使用gcloud auth application-default login
来执行一次性激活gcloud
应用程序默认凭证的步骤。
更长的版本
Greg,我尝试了我的机器上的示例,遵循Cloud Vision API客户端库页面上提到的步骤,并且适用于我。
如果步骤不清楚,我会带你走过去:
-
安装
google-cloud-vision
python包。 (从你的post你似乎已经这样做了)。pip install --upgrade google-cloud-vision
-
在您的机器/泊坞箱容器上设置应用程序默认凭证。 你似乎提到你成功运行
gcloud init
。 这不足以设置应用程序默认凭据。 相反,您将不得不运行在要求您在浏览器中打开链接的步骤中提到的以下命令,并且必须将确认从浏览器中粘贴回到命令行中。gcloud auth application-default login
-
下载testing图像
wakeupcat.jpg
或使用您自己的图像。 这部分不幸的是没有在页面上明确提到。 -
用下面的内容创build一个名为
cloud-vision-example.py
的文件或其他任何你喜欢的名字(注意:我已经在这里更新了图像文件的path):
cloud-vision-example.py
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('.', '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)
- 运行python脚本。
python cloud-vision-example.py
。
输出:
$ python cloud-vision-example.py Labels: cat photo caption small to medium sized cats cat like mammal whiskers snout kitten asian
应用程序默认凭据如何工作
-
如果您想使用服务帐户的凭据,您可以创build一个新的服务帐户 ,下载私钥json文件,并在json文件中指向环境variables
GOOGLE_APPLICATION_CREDENTIALS
(看起来像您这样做了,但您可能想要双倍检查您是否下载了正确的证书并且path是有效的)。 -
如果未设置
GOOGLE_APPLICATION_CREDENTIALS
,则客户端库将尝试使用gcloud应用程序默认凭据(应在此之前设置)。 -
如果您的应用程序在App Engine或Compute Engine上运行,则将使用关联的内置服务帐户作为最后的手段。