简体   繁体   English

如何从 Google Chrome 扩展程序调用 Google NLP Api

[英]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.我的目标是 select 来自 web 页面的一些文本,启动谷歌浏览器扩展并将文本提供给谷歌云 api (自然语言 API)

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?扩展由清单、弹出窗口等组成。我应该如何从那里调用一个 API 进行自然语言处理?

Should I create a Google Cloud Application with an API_KEY to call?我应该使用 API_KEY 创建一个 Google Cloud 应用程序来调用吗? 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.如果您不打算识别用户,也不打算使用处理身份验证的后端服务器(我认为是您的情况),那么最好的选择确实是使用 API 密钥。 They do not identify the user, but are enough for the Natural Language APIs.它们不识别用户,但对于自然语言 API 来说已经足够了。

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.为此,您需要为所需的服务创建 API 密钥,并添加必要的限制以使密钥尽可能安全。 Detailed instructions on how to do this and how to use the key in a url can be found here .可以在此处找到有关如何执行此操作以及如何使用 url 中的密钥的详细说明。

The API call could be made from within the Chrome extension with any JavaScript method capable of performing POST requests.可以使用任何能够执行 POST 请求的 JavaScript 方法从 Chrome 扩展中进行 API 调用。 For example usingXMLHttpRequest or the Fetch API .例如使用XMLHttpRequestFetch 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.直接从扩展程序发出请求时,您可能会遇到 CORS 问题。 I recommend reading this answer , where a couple of workarounds for these issues are suggested.我建议阅读此答案,其中建议了针对这些问题的几种解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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