grpc谷歌登录失败

时间:2017-05-10 15:15:50

标签: c++ speech grpc

我正在尝试使用google的云语音api创建应用程序。 我克隆了所有的存储库,并用C ++创建了一个非常简单的客户端应用程序。

#include <grpc++/grpc++.h>
#include "google/cloud/speech/v1/cloud_speech.grpc.pb.h"
namespace gs=google::cloud::speech::v1;
using gs::Speech;
using gs::RecognizeResponse;
using gs::RecognizeRequest;
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;

int main(int argc, char** argv) {
    try{
        auto channel_creds = grpc::GoogleDefaultCredentials();
        auto channel = grpc::CreateChannel("speech.googleapis.com:443", channel_creds);
        auto stub = Speech::NewStub(channel);

        RecognizeResponse res;
        RecognizeRequest req;
        ClientContext ctx;

        auto status = stub->Recognize(&ctx, req, &res);
        if(status.ok())
        {
            std::cout << "ok" << std::endl;
        } else {
            std::cout << status.error_code() << ": " << status.error_message() << std::endl;
        }
    }catch(const std::exception& e)
    {
        std::cout<<e.what()<<std::endl;
    }
    std::cin.get();
}

这编译很好,但执行时会产生以下错误消息:

  

16:请求具有无效的身份验证凭据。预期的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据。请参阅https://developers.google.com/identity/sign-in/web/devconsole-project

我创建了一个服务帐户并将其放在env变量&#34; GOOGLE_APPLICATION_CREDENTIALS&#34;旁边的json密钥文件中。到正确的文件,我很确定它被读取(因为如果我删除它,它只是崩溃)。 我也在线启用了语音API。 我没有想法,因为无论我做什么,我都无法让它发挥作用。 我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

经过一些尝试和阅读更多示例代码后,我终于找到了原因。 我不得不使用不同的URL。将“speech.googleapis.com:443”更改为“speech.googleapis.com”会导致出现其他错误,但会导致连接和登录工作。

正确设置此功能的所有参数并提供实际音频数据会返回可用的结果。我没想到端口有所作为,考虑到两个URL都在许多示例程序中,而grpc使用http2(后者又使用端口443)。

重要提示:请确保您没有包含用于Google服务的端口。