【发布时间】:2020-05-11 10:45:31
【问题描述】:
我按照从here 生成 Python gPRC 客户端的说明进行操作,但很难为请求提供令牌
不安全的通道不起作用
auth_creds = grpc.access_token_call_credentials(TOKEN)
channel = grpc.insecure_channel('localhost:10000', auth_creds)
>> TypeError: 'CallCredentials' object is not iterable
安全通道也不起作用
auth_creds = grpc.access_token_call_credentials(TOKEN)
channel = grpc.secure_channel('localhost:10000', auth_creds)
>> TypeError: Argument 'channel_credentials' has incorrect type (expected grpc._cython.cygrpc.ChannelCredentials, got grpc._cython.cygrpc.MetadataPluginCallCredentials)
CallCredentials 必须与安全通道一起使用,否则元数据将不会传输到服务器。
但是,我如何创建一个安全通道,因为创建这些 ChannelCredentials 的方法对应于 ssl 或类似的,不是吗?
除此之外,似乎可以简单地将元组{'Authorization':'Bearer <TOKEN>'} 解析为元数据,如here。但是,我注意到 - 正如在提出的评论中一样 - 不允许使用大写字符。
with_call 带有凭据的方法也不起作用
auth_creds = grpc.access_token_call_credentials(TOKEN)
channel = grpc.secure_channel('localhost:10000', auth_creds)
stub = foo_bar_pb2_grpc.ServiceStub(channel)
response = stub.Get.with_call(message_pb2.Message(), credentials=auth_creds)
>> TypeError: Argument 'channel_credentials' has incorrect type (expected grpc._cython.cygrpc.ChannelCredentials, got grpc._cython.cygrpc.MetadataPluginCallCredentials)
with_call 带有元数据的方法也不起作用
metadata = 'Authorization', 'Bearer <TOKEN>'
channel = grpc.insecure_channel('localhost:10000')
stub = foo_bar_pb2_grpc.ServiceStub(channel)
response = stub.Get.with_call(message_pb2.Message(), metadata=metadata))
>> ValueError: too many values to unpack (expected 2)
总结:如何使用访问令牌对客户端进行身份验证?
【问题讨论】:
标签: python client grpc bearer-token