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.
问题:
回答1:
Any method, i need a step by step procedure to do it in C#
Here is a complete, ready-to-run open-source C# demo app for the Google Cloud OCR API and the OCR.space API:
https://github.com/A9T9/Google-OCR-Vision-API-CSharp
回答2:
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).