2

I'm trying to call the google cloud API. Specifically, the language API from c# using the RestSharp library and OAuth 2. I'm able to successfully connect to the API using the curl call below:

curl -s -k -H "Content-Type: application/json" -H "Authorization: Bearer <access_token>"
     https://language.googleapis.com/v1beta1/documents:annotateText
 -d @c:\temp\entity_request.json > c:\temp\googleanalysis.json

I've tried several different ways of authenticating, but none of them so far have worked. My latest c# code looks like the following:

var client = new RestClient("https://language.googleapis.com");
client.Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator("client-app", "<access_token>");


var request = new RestRequest("/v1beta1/documents:analyzeEntities", Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddFile("filename", @"c:\temp\entity_request.json");

var response = client.Execute(request);
var content = response.Content;

When I run this call from c# I get the following error back:

{
  "error": {
    "code": 403,
    "message": "The request cannot be identified with a client project. Please pass a valid API key with the request.",
    "status": "PERMISSION_DENIED"
  }
}

My question is how do I properly call the google cloud API in RestSharp the way I am successfully with curl?

Ben Anderson
  • 1,069
  • 2
  • 16
  • 38

1 Answers1

0

This works for me:

            //obtenemos el token para las peticiones
            string access_token = GetAccessToken(jsonFolder, new string[] { "https://www.googleapis.com/auth/cloud-platform" });

            //peticiones hacia el rest de automl
            var client = new RestClient("https://language.googleapis.com");
            var request = new RestRequest("v1/documents:analyzeEntities", Method.POST);

            request.AddHeader("Authorization", string.Format("Bearer {0}", access_token));
            request.AddHeader("Content-Type", "aplication/json");

            //seteamos el objeto
            var aml = new AutoMLload_entities();
            aml.document.content = text;

            request.AddJsonBody(aml);
            IRestResponse response = client.Execute(request);
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
angie
  • 1