0

I'm trying to write a Chrome extension that takes the text from a website and runs it through a custom AutoML Natural Language model on Google Cloud and then displays the prediction data to the screen.

I've tried authenticating using OAuth2, which is giving me Permission denied errors, and the documentation is saying to set up a service account instead. I haven't figured out how to make the chrome extension into a service account, but I've found people suggesting that I will have to set up a separate Node.JS server and relay the queries that way.

In manifest.js I have:

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

and in background.js I have:

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));
  });

This is a translation of the curl request described in the documentation turned into a fetch request. I'm getting

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

As far as I can tell, the account is properly set up and given the permissions on the Google Cloud side. Does anyone see what's going on here?

j_webber
  • 11
  • 2
  • I am trying the same! Did you find any solution to this? – Mauritius Nov 21 '19 at 09:24
  • The issue is that Chrome extensions have pretty strict security limitations, they're not able to be service accounts. The solution I found was to send a message to a Google Cloud Function, which relays it to the AutoML API. The Cloud Function is able to be a service account. – j_webber Nov 27 '19 at 17:52

1 Answers1

1

So, after puzzling with this some more, the answer ultimately was NO, you cannot query directly, Chrome Extension are not very powerful. The solution I found was to create a Google Cloud Function on the same Cloud Platform account as the AutoML model, and use fetch to send HTTP requests from the Chrome Extension to the Cloud Function. I made the Cloud Function a service account and imported the Node.JS AutoML API client which I used to query the AutoML and return the results to the extension. I hope this can be helpful to someone.

j_webber
  • 11
  • 2