I want to use text-detection from image (OCR) of google cloud vision api. But i dont know how to get the subscription key from and how to authenticate and make calls in C#. Can some body tell me the step by step procedure to do that. Im very new this btw.
-
Which "subscription key" do you mean? Could you link to the docs you're trying to follow? You'll need to a Google Cloud Platform project, and then you could create a service account, download the JSON for that, and use that for authentication. – Jon Skeet Apr 05 '18 at 06:07
-
i created a project and service account. i just enabled cloud vision api. and i could get the api key for the same. what is downloading json. how should i authenticate and call those api's from asp.net? – Manisha P Apr 05 '18 at 06:33
-
1Don't focus on API keys - they're discouraged these days. Follow the docs at https://cloud.google.com/docs/authentication/getting-started which will show you how to download the JSON authentication file associated with the service account, and set the appropriate environment variable for that to be picked up by the library. (It's still not clear which library you're using - there are multiple options, but I'd recommend Google.Cloud.Vision.V1.) – Jon Skeet Apr 05 '18 at 06:36
-
I want to do it with subscription key. isn't there any step by step tutorials to guide. i just but i coulnd find one – Manisha P Apr 05 '18 at 06:54
-
I don't know of any way of using Google.Cloud.Vision.V1 with an API key. You could probably use Google.Apis.Vision.v1 with an API key, but it won't be as clean an experience. Is there any particular reason you want to use an API key rather than a service account? – Jon Skeet Apr 05 '18 at 07:04
-
no specific reasons. But i don't know how to proceed. I have already used microsoft and twiiter api's using subscription key. So, i thought it would be better. Any method, i need a step by step procedure to do it in C# – Manisha P Apr 05 '18 at 07:11
-
1Please read https://cloud.google.com/vision/docs/reference/libraries which has a quick-start for C#. – Jon Skeet Apr 05 '18 at 07:16
1 Answers
I think the question is a bit messed up, so let me take a step back and try to cover the most important things regarding authentication when using the Cloud Vision API.
First of all, the documentation offers a really clear explanation on how to authenticate to the Cloud Vision API, using API keys or Service Accounts. Bear in mind that, as documented in the best practices for authentication in the Google Cloud Platform:
For almost all cases, whether you are developing locally or in a production application, you should use service accounts, rather than user accounts or API keys.
Being this clarified, it is obviously up to you whether to use API keys (I understand this is what you refer to when you mention "subscription keys") or Service Accounts. Here are the main differences that may be relevant for you regarding these two authentication methods:
- Service Accounts: they are more secure, the recommended approach to use, and integrate well and easily with the APIs Client Libraries, which make your life much easier when it comes to interacting with GCP APIs. I strongly recommend you to use Service Accounts and idiomatic Client Libraries.
- API keys: you should follow a set of best practices for securely using them, and they cannot (or at least I am not aware of that) integrate with the Client Libraries; therefore you will need to make calls to the REST API directly, which is more complex than using idiomatic Client Libraries.
I hope I have been able to highlight the differences between the alternative authentication methods available. So let's move on to the next topic, authenticating requests:
If you are using API keys, it is as easy as as appending the API key to the REST API method that you want to call, just like this:
https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY
In such a case, you will have to find a way to make HTTP requests in C#, parse the JSON response, etc.
If you are using Service Accounts (I hope I convinced you to do so in the first part of my answer), you will need to follow these steps detailed in the documentation:
- Download the JSON key for your Service Account.
- Refer to it in the
GOOGLE_APPLICATION_CREDENTIALS
environment variable in your machine. - Use the C# Client Libraries as explained in this simple example, or use the complete documentation reference to have all the details about how to use this library (Client Library documentation and
ImageAnnotatorClient
documentation).

- 7,864
- 2
- 33
- 50
-
Finally, and out of the answer itself, let me also share with you this helpful page on the Stack Overflow Help Center, which can help you to [ask a good question](https://stackoverflow.com/help/how-to-ask). Note that in Stack Overflow we like questions were the research and development effort is shown, and not of the type *please teach me how to do this* or *share with me a tutorial to do this*. This will for sure help you to get better answers for your questions ;) – dsesto Apr 19 '18 at 15:05
-
Thank you for this detailed explanation. Now i have one question - What if i use this *idiomatic client library* in my mvc web application and when i published that web application on production server at that time, I need to create an environment variable using that json file which i did on my local server, Am i right ? – Krishnraj Rana Dec 14 '18 at 15:39