Search code examples
c#.netocrgoogle-cloud-vision

Google Cloud API not returning any response


Background information:
I'm trying to create a PoC for Google Cloud Vision API using their .NET library.

What I have done:
Create a simple console apps with the following code for Vision API.

GoogleCredential credential = GoogleCredential.FromFile(ConfigurationManager.AppSettings["GoogleCredentialFile"]);
Grpc.Core.Channel channel = new Grpc.Core.Channel(Google.Cloud.Vision.V1.ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
var client = Google.Cloud.Vision.V1.ImageAnnotatorClient.Create(channel);

var image = Google.Cloud.Vision.V1.Image.FromFile(@"C:\Users\u065340\Documents\sample.jpg");
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
    if (annotation.Description != null)
    result = annotation.Description;
}

Problem:
The line client.DetectLabels(image) gets stuck for a long time before ultimately throwing the error Deadline Exceeded.
My code sits behind a corporate proxy, but I have validated that it is not blocking internet access because I can call https://vision.googleapis.com/$discovery/rest?version=v1 from the same apps and get its JSON response just fine.
Any suggestions?


Solution

  • After digging around through github issues related to proxies as suggested by Jon Skeet, I found that Google Cloud Client APIs can be generally divided into 2 categories (Ref: here): REST-based HTTP 1.1 with JSON and gRPC.
    For APIs associated as REST-based, there should be no issue with proxies. The problem starts to appear when we are using gRPC-based APIs such as Google Cloud Vision and Google Speech. In gRPC, we need to explicitly provide our proxy server information.

    For those using Java Client, it seems we still can't set proxy properly because it will eventually be ignored, and causing the Deadline Exceeded error. This issue is already well known and can be found at here and further traced into here.
    The Google team has determined that it is indeed a bug, and the status remains Open.

    As for C# Client, we can set proxy information using gRPC Environment Variables which is documented in here. The code is Environment.SetEnvironmentVariable("http_proxy", <your_proxy_server>);
    After I set the http_proxy environment variable pointing to my proxy server, all is well again. I get the expected output "This API needs Billing Account".

    Many thanks to Jon Skeet for pointing me in the right direction :D