Hi everyone,
I'm trying to publish messages to Google PubSub topic from a C++ application. So far I've been able to get it to work with the emulator, but when trying to send messages to Google's API, the gRPC request always fails with error 14: Endpoint read failed.
I suspect that I'm not authenticating correctly. So far I've created a service account that is allowed to publish, and I've set GOOGLE_APPLICATION_CREDENTIALS to point to the .json file with the credentials.
My code looks like this:
#include <iostream>
#include <memory>
#include <grpc++/grpc++.h>
#include "google/pubsub/v1/pubsub.grpc.pb.h"
auto main() -> int {
using grpc::ClientContext;
using google::pubsub::v1::Publisher;
using google::pubsub::v1::PublishRequest;
using google::pubsub::v1::PublishResponse;
using google::pubsub::v1::PubsubMessage;
auto creds = grpc::GoogleDefaultCredentials();
auto stub = std::make_unique<Publisher::Stub>(grpc::CreateChannel("pubsub.googleapis.com", creds));
PublishRequest request;
auto const topic_string = "projects/pubsub-cpp-api-1504713535863/topics/testing";
request.set_topic(topic_string);
PubsubMessage msg;
msg.set_data("Hello from C++");
*request.add_messages() = msg;
PublishResponse response;
ClientContext ctx;
auto status = stub->Publish(&ctx, request, &response);
if (!status.ok()) {
std::cout << "Publishing message failed with error " + std::to_string(status.error_code()) + ": " + status.error_message() << '\n';
}
} What am I missing?
Best regards
Manuel