简体   繁体   中英

How to call Google NLP Api from a Google Chrome extension

My aim is to select some text from a web page, start a google chrome extension and give the text to a google cloud api (Natural Language API) in my case.

I want to do some sentimental analysis and then get back the result to mark/ highlight positive sentences in green and negative ones in red. I am new to this and do not know how to start. The extension consists of manifest, popup etc. How should I call an API from there that does Natural Language Processing?

Should I create a Google Cloud Application with an API_KEY to call? In that case I would have to upload my credentials right? Sorry sounds a bit confusing I know but I just don't know how I can bring this 2 things together an would be more than happy about any help

The best way to authenticate your app will depend on the specific needs and use cases of your application. You can see an overview of all the different methods here .

If you are not planning on identifying users nor on using a back end server that handles authenticating (as I assume to be your case), the best option would indeed be to use API keys. They do not identify the user, but are enough for the Natural Language APIs.

To do this you will need to create an API key for the services you want and add the necessary restrictions to make the key as secure as possible. Detailed instructions on how to do this and how to use the key in a url can be found here .

The API call could be made from within the Chrome extension with any JavaScript method capable of performing POST requests. For example usingXMLHttpRequest or the Fetch API . You can find an example of the parameters that need to be included in the request here .

You may run into CORS issues when making the request directly from the extension. I recommend reading this answer , where a couple of workarounds for these issues are suggested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM