【问题标题】:Is it possible to send queries directly from a Chrome extension to the Google Cloud AutoML API?是否可以直接从 Chrome 扩展程序向 Google Cloud AutoML API 发送查询?
【发布时间】:2019-08-30 02:02:22
【问题描述】:

我正在尝试编写一个 Chrome 扩展程序,它从网站获取文本并通过 Google Cloud 上的自定义 AutoML 自然语言模型运行它,然后将预测数据显示到屏幕上。

我尝试使用 OAuth2 进行身份验证,这给了我 Permission denied 错误,并且文档说要设置一个服务帐户。我还没有弄清楚如何将 chrome 扩展程序变成服务帐户,但我发现有人建议我必须设置一个单独的 Node.JS 服务器并以这种方式中继查询。

在 manifest.js 我有:

"oauth2": {
    "client_id": "{account_id}.apps.googleusercontent.com",
    "scopes":["https://www.googleapis.com/auth/cloud-platform"]
 }

在 background.js 中我有:

let authToken = '';
chrome.identity.getAuthToken({interactive: true}, function(token) {

    authToken = token;
    fetch("https://automl.googleapis.com/v1beta1/{name}:predict", {

    method: "POST",
    body: `{ 
        "payload": {
            "textSnippet": { 
                "content": "TEXT TO PREDICT GOES HERE", 
                "mime_type": "text/plain"
            }, 
        }  
    }`,
    headers: {
        Authorization: "Bearer " + authToken,
        "Content-Type": "application/json"
    }
    }).then(response => response.text())
    .then(data => console.log(data));
  });

这是将文档中描述的 curl 请求翻译成 fetch 请求。我来了

{
    "error": {
    "code": 403,
    "message": "The caller does not have permission",
    "status": "PERMISSION_DENIED"
    }
}

据我所知,该帐户已正确设置并获得了 Google Cloud 端的权限。有人看到这里发生了什么吗?

【问题讨论】:

  • 我也在尝试!你找到解决办法了吗?
  • 问题是 Chrome 扩展有相当严格的安全限制,它们不能成为服务帐户。我找到的解决方案是向 Google Cloud Function 发送消息,该函数将消息中继到 AutoML API。 Cloud Function 可以成为服务帐户。

标签: javascript google-chrome automl google-cloud-automl


【解决方案1】:

所以,再纠结这个问题后,最终的答案是否定的,不能直接查询,Chrome 扩展功能不是很强大。我找到的解决方案是在与 AutoML 模型相同的 Cloud Platform 帐户上创建一个 Google Cloud Function,并使用 fetch 将 HTTP 请求从 Chrome 扩展发送到 Cloud Function。 我将 Cloud Function 设为服务帐户并导入了 Node.JS AutoML API 客户端,用于查询 AutoML 并将结果返回给扩展。我希望这可以对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2013-07-29
    • 2012-09-16
    • 2020-04-23
    • 2016-08-11
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多