2017-05-27 122 views
0

我試圖實現與GRPC定製的基於令牌的身份驗證方案,我必須在客戶端下面的代碼:如何在服務器端獲取身份驗證令牌?

with open('server.crt', 'rb') as f: 
    trusted_certs = f.read() 

credentials = grpc.ssl_channel_credentials(root_certificates=trusted_certs) 
composite_credentials = grpc.composite_channel_credentials(credentials, grpc.access_token_call_credentials("test_token")) 
channel = grpc.secure_channel('{}:{}'.format('localhost', 50051), composite_credentials) 
stub = helloworld_pb2_grpc.GreeterStub(channel) 
response = stub.SayHello(helloworld_pb2.HelloRequest(name='test')) 

此代碼工作正常,似乎測試令牌傳送到服務器。但是,我不能想出一個如何在服務器端獲取令牌並檢查它的解決方案。

回答

2

以防萬一,如果有人跑上去對同樣的問題,正確的解決方法是使用:

context.invocation_metadata() 

在服務器端。這將返回表示在客戶端傳遞的元數據的元組的集合(例如,「授權」頭和它的值)。

相關問題