在 Google Video Intelligence 中使用 python vcr - 请求出错

问题描述

当我使用 python vcr 运行测试时,我遇到了一个奇怪的谷歌视频智能 api 请求错误。我想知道是否有其他人看到过这个错误,如果有,他们是怎么解决的?

我使用 VCR 来模拟对视频智能 api 的请求。

我使用 pytest 来运行我的测试。

如果我删除 VCR 并重新运行它,它运行良好,如果我在创建 VCR 后再次尝试运行它,它也运行良好。但是过了一会儿,(目前不确定这是几小时还是几天,我会在运行 vcr 时收到以下错误)。

错误如下:-

    app/tests/test_video_intelligence.py:18 (TestsVideoIntelligenceUnitTests.test_vi_process_video)
    args = (features: logo_RECOGNITION
    video_context {
      person_detection_config {
        include_bounding_Boxes: true
        include_p...5\210\300\037\317RP\005o\000\000\000\rA\232\340/\300\002{\335\n=\000V\360\000\000\000\010A\233\000+\300\002\322\260",)
    kwargs = {'Metadata': [('x-goog-api-client','gl-python/3.6.9 grpc/1.35.0 gax/1.26.0 gapic/1.15.0')],'timeout': 120.0}
        @six.wraps(callable_)
        def error_remapped_callable(*args,**kwargs):
            try:
    >           return callable_(*args,**kwargs)
    ../../.virtualenvs/python3.6/site-packages/google/api_core/grpc_helpers.py:73: 
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
    self = <grpc._channel._UnaryUnaryMultiCallable object at 0x7f774bcc8c50>
    request = features: logo_RECOGNITION
    video_context {
      person_detection_config {
        include_bounding_Boxes: true
    
    credentials = None,wait_for_ready = None,compression = None
        def __call__(self,request,timeout=None,Metadata=None,credentials=None,wait_for_ready=None,compression=None):
            state,call,= self._blocking(request,timeout,Metadata,credentials,wait_for_ready,compression)
    >       return _end_unary_response_blocking(state,False,None)
    ../../.virtualenvs/../python3.6/site-packages/grpc/_channel.py:923: 
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
    state = <grpc._channel._RPCState object at 0x7f775cd26940>
    call = <grpc._cython.cygrpc.SegregatedCall object at 0x7f775a6a5a08>
    with_call = False,deadline = None
        def _end_unary_response_blocking(state,with_call,deadline):
            if state.code is grpc.StatusCode.OK:
                if with_call:
                    rendezvous = _MultiThreadedRendezvous(state,None,deadline)
                    return state.response,rendezvous
                else:
                    return state.response
            else:
    >           raise _InactiveRpcError(state)
    E           grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
    E               status = StatusCode.UNAUTHENTICATED
    E               details = "Request had invalid authentication credentials. Expected OAuth 2 access token,login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project."
    E               debug_error_string = "{"created":"@1617013080.261482871","description":"Error received from peer ipv4:216.58.211.170:443","file":"src/core/lib/surface/call.cc","file_line":1067,"grpc_message":"Request had invalid authentication credentials. Expected OAuth 2 access token,login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","grpc_status":16}"
    E           >
    ../../.virtualenvs/../lib/python3.6/site-packages/grpc/_channel.py:826: _InactiveRpcError
    The above exception was the direct cause of the following exception:
    
    
    self = <tests.test_video_intelligence.TestsVideoIntelligenceUnitTests object at 0x7f775a5efe80>

这是录像机代码错误

    @vcr.use_cassette(os.path.join(VCR_DIR,f'test_video_intelligence_process_video_{VIDEO_NAME}.yaml'),filter_headers=['authorization'],serializer='yaml',record_mode='once')
        def func(self)
        .....
    
    
    value = None
    from_value = <_InactiveRpcError of RPC that terminated with:
        status = StatusCode.UNAUTHENTICATED
        details = "Request had invalid a...entication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","grpc_status":16}"
    >
    >   ???
    E   google.api_core.exceptions.Unauthenticated: 401 Request had invalid authentication credentials. Expected OAuth 2 access token,login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
    <string>:3: Unauthenticated
    Assertion Failed

解决方法

Google Vision Intelligence 就像其他 Google 服务一样,使用 gRPC 与服务后端通信。不幸的是,VCR 尚不支持 gRPC。

在测试期间 VCR 记录的唯一内容是 OAuth 令牌(检查 VCR 生成的盒式磁带文件)。当它过期时,它会导致Unauthenticated错误

参考:

,

error 似乎指向无效的身份验证凭据。

此错误背后可能有两种可能的情况 -

您可以按照下面提到的步骤来验证您所做的初始设置配置:

解决方案 1-对于自定义服务帐户,即用户管理的服务帐户:

  • 如果您已经创建了自己的服务帐户,则需要为该服务帐户生成密钥。

  • 您还需要使用以下方法设置环境变量:

    “导出 GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH”

解决方案 2 - 应用默认凭据不可用:

对于默认服务帐户(已启用 Compute Engine API 的项目将有一个将使用应用程序默认凭据的 Compute Engine 默认服务帐户),您可以按照以下提到的步骤操作: >

  • 您需要使用 command 登录:gcloud auth application-default login

  • 如果您想确保 authentication process 运行良好,请运行:gcloud auth application-default print-access-token。您应该能够看到访问令牌。

  • 访问令牌的生命周期有限。如果您的应用程序需要在单个访问令牌的生命周期之外访问 Google API,它可以获得刷新令牌。刷新令牌允许您的应用程序获取新的访问令牌。

  • 基本上,gcloud auth application-default login 会获取凭据,以便本地计算机中的应用程序在配置为使用应用程序默认凭据时可以使用这些凭据。