这是我使用c++与google cloud speech API交互的grpc的主要代码,您可以看到我正在调用context.AddMetadata("x-api-key","AIzaxxxxxxxxxxxxxxxxxxx");
在下面的日志中,我可以看到当客户端正在编写时,我们得到了一个断开的管道错误(这是远程正在关闭套接字)
请告诉我这是不是正确的做法
我也试过使用这个key (“x-goog-api-context.AddMetadata”,"AIzaxxxxxxxxxxxxxxxxx");
在文档中,根据此链接:https://cloud.google.com/endpoints/docs/grpc/restricting-api-access-with-api-keys
它提到了这个
根据是从gRPC客户端还是从=snippet客户端调用,使用API键调用API的API会有所不同。
gRPC客户端如果方法需要API键,则gRPC客户端需要将键值作为x- API -key元数据与其方法调用一起传递。
=snippet end=
哪一个不起作用?请解释一下为什么?
另外,在给出的示例中,有在python示例中提供了授权密钥的auth_token?这是x-api-key所必需的吗?
=
它工作的唯一方式是取消注释以下注释行,并且不使用grpc::InsecureChannelCredentials()
//auto creds = grpc::GoogleDefaultCredentials();
这是在创建通道时。您需要设置GOOGLE_APPLICATION_CREDENTIALS,它适用于一个服务帐户。
但我的要求是让客户端使用多个服务(可能有多个服务帐户)。那么,对于我的需求,推荐的方法是什么?
int main(int argc, char** argv) {
// Create a Speech Stub connected to the speech service.
//auto creds = grpc::GoogleDefaultCredentials();
/*auto channel = grpc::CreateChannel("speech.googleapis.com", creds); */
auto channel = grpc::CreateChannel("speech.googleapis.com", grpc::InsecureChannelCredentials());
if (channel == nullptr)
{
std::cout << "could not allocate channel" <<std::endl;
return -1;
}
std::unique_ptr<Speech::Stub> speech(Speech::NewStub(channel));
// Parse command line arguments.
StreamingRecognizeRequest request;
auto* streaming_config = request.mutable_streaming_config();
char* file_path =
ParseArguments(argc, argv, streaming_config->mutable_config());
if (nullptr == file_path) {
std::cerr << kUsage;
return -1;
}
auto start = std::chrono::system_clock::now();
std::time_t start_time = std::chrono::system_clock::to_time_t(start);
std::cout << "time start " << std::ctime(&start_time) << std::endl;
// Begin a stream.
grpc::ClientContext context;
context.AddMetadata("x-api-key", "AIzaSxxxxxxxxxxxxxxxxxxxxxxxxxx");
auto streamer = speech->StreamingRecognize(&context);
// Write the first request, containing the config only.
streaming_config->set_interim_results(true);
streamer->Write(request);
// The microphone thread writes the audio content.
std::thread microphone_thread(&MicrophoneThreadMain, streamer.get(),
file_path);
// Read responses.
StreamingRecognizeResponse response;
while (streamer->Read(&response)) { // Returns false when no more to read.
// Dump the transcript of all the results.
for (int r = 0; r < response.results_size(); ++r) {
const auto& result = response.results(r);
std::cout << "Result stability: " << result.stability() << std::endl;
for (int a = 0; a < result.alternatives_size(); ++a) {
const auto& alternative = result.alternatives(a);
std::cout << alternative.confidence() << "\t"
<< alternative.transcript() << std::endl;
}
}
}
在日志中,我可以看到hTTP头被发送: I1127 16:06:19.028497113 30954 chttp2_transport.cc:1374] HTTP:0:HDR:CLI: x-api-HTTP: AIzaxxxxxxxxxxxxxxxxxx
但是在写套接字时,我收到了“管道断开”错误:
D1127 16:06:19.060125242 30954 tcp_posix.cc:1077]数据: 69 64 65 6e 74 74 79 2c 67 7a 69 70‘标识,gzip’I1127 16:06:19.060200892 30954 tcp_posix.cc:1111]写入:{"created":"@1574831179.060166760",“description”:“管道断开”,"errno":32,"fd":9,“file”:“src/I1127/lib/iomgr/tcp_posx.cc”,"file_line":998,"grpc_status":14,“os_error”:“管道破裂”,"syscall":"sendmsg","target_address":"ipv4:216.58.199.74:443"}
发布于 2019-11-28 02:52:31
我想出了一个组合,我们需要创建SSL连接来添加元数据x-goog- API -key="your API key“
在beginning...do中创建通道时,这代替了不安全的通道创建.
auto creds = grpc::SslCredentials(grpc::SslCredentialsOptions());
auto channel = grpc::CreateChannel("speech.googleapis.com", creds);
当我们发送危及安全的API密钥时,确保连接安全是有意义的,“中间人”可以以某种方式嗅探到这一点!
我已经在google-speech讨论上打开了一个主题,以获得更多的答案,比如我们是否可以使用JWT令牌发送更大的编码授权数据。如果有什么有用的,我会在这里更新。
https://stackoverflow.com/questions/59063722
复制相似问题