0

I need to connect a grpc server with some metadata ,such as username and password.From the grpc Docs,the PHP code is clear,I think this code will work:

$client = new Routeguide\RouteGuideClient('test.yintongzhibo.com:9002', [
'credentials' => Grpc\ChannelCredentials::createSsl(file_get_contents("xxx.pem")),
'update_metadata' => $metaProcessor,
'grpc.ssl_target_name_override' => 'test.yintongzhibo.com',]);

$metaProcessor = function($metadata,$client = []){
$values = $metadata;
$values[Username] = ['xxx'];
$values[Password] = ['yyy'];
return $values;}

list($resp,$status) = $client->getMarketData(request)->wait();

But I need to achieve the same thing in python.Python Docs is not very clear,so how to write the python code to do the same thing that the PHP code does?

1 Answers1

0

Stubs have a metadata field.

Ex: stub.SayHello(helloworld_pb2.HelloRequest(name='you'), metadata=[('key','value')])

kpayson64
  • 353
  • 1
  • 4
  • I trid this:stub.getMarketData(market_request,metadata=[('UserName','xxxx'),('Password','yyyy')]),but the error message is:"metadata was invalid:[('UserName','xxxx'),('Password','yyyy')]"...please help me..thanks very very much!there is my more detailed question url:https://stackoverflow.com/questions/45676398/how-to-use-python-to-authenticate-with-google-in-grpci-have-a-runable-php-code – LeoHirasawa Aug 16 '17 at 03:41
  • And I tried this method:https://stackoverflow.com/questions/45071567/how-to-send-custom-header-metadata-with-python-grpc,like this:`myMetadata = (('username','xxxx'),('password','yyyy'))`,`stub.getMarketData(market_request,metadata=myMetadata)`,but the error meaasge is:not all arguments converted during string formatting. – LeoHirasawa Aug 16 '17 at 06:48
  • It sounds like you have a different issue unrelated to gRPC. The example you have provided should work. – kpayson64 Aug 16 '17 at 20:51
  • but only when I add this code,there with be a error,I think the reason is this code may be not right.... could you give me some links to the api about this?I didn't find this in grpc Docs..thanks again – LeoHirasawa Aug 17 '17 at 06:20
  • https://grpc.io/grpc/python/grpc.html#grpc.UnaryUnaryMultiCallable ```__call__``` is the relevant function. – kpayson64 Aug 17 '17 at 22:36