Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension gmail API cookiePolicy?

I'm building a chrome extension that will read the user's emails and check them for typos. However, when trying to authenticate the user in my background.js I'm running into this error:

uO {message: "Invalid cookiePolicy", stack: "gapi.auth2.ExternallyVisibleError: Invalid cookieP… at handleResponse (extensions::sendRequest:67:7)"}

Here is how I'm trying to authenticate them:

background.js

var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://apis.google.com/js/client.js?onload=callbackFunction";
head.appendChild(script);

chrome.identity.getAuthToken({interactive: true}, authorize);

function authorize(token) {
    gapi.auth.authorize({
        client_id: '800382879116-k3luktdc1lmb1e1fml8i8u.apps.googleusercontent.com',
        immediate: true,
        scope: 'https://www.googleapis.com/auth/gmail.readonly'
    },
    function(result){
        console.log(result);
        gapi.client.load('gmail', 'v1', callback);
    });
}

background.html

<!DOCTYPE html>
<html>
    <body>
        <script src='scripts/background.js'></script>
    </body>
</html>

manifest.json

  {
    "name": "Gmail Typo Analyzer",
    "version": "0.1",
    "description": "Gmail Typo Analyzer",
    "permissions": [
      "identity",
      "storage"
    ],
    "content_security_policy": "script-src 'self' https://apis.google.com; object-src 'self'",
    "oauth2": {
      "client_id": "82879116-k3luktdc1li8u.apps.googleusercontent.com",
      "scopes": [
          "https://www.googleapis.com/auth/gmail.readonly",
          "https://www.googleapis.com/auth/userinfo.email"
      ]
    },
    "browser_action": {
      "default_popup": "popup.html",
      "default_icon": "images/Icon_16.png"
    },
    "background": {
      "page": "background.html",
      "persistent": false
    },
    "icons": {
      "16": "images/Icon_16.png",
      "32": "images/Icon_32.png",
      "48": "images/Icon_48.png",
      "128": "images/Icon_128.png"
    },
    "manifest_version": 2,
    "key": "c0Kn5f+t92r4P8lmmoDlKtQ6X9Q42UfFtkkiSRBAVMPHnIHqOQvYC67VczJefSNTGpUYa8+wQDFoFj/clH9SfR+BvOGgI6BUVKBNGGoFS"
  }

I'm super lost right now as their doesn't seem to be a definitive guide on achieving what I'm trying to do anywhere. Does anyone know what I might be doing wrong?

like image 688
MarksCode Avatar asked Nov 14 '18 22:11

MarksCode


1 Answers

You didn't post your manifest.json file, where you would set the oauth2 credentials, so I would try something like:

manifest.json

...
"oauth2" : "client_id": "800382879116-k3luktdc1lmb1e1fml8i8u.apps.googleusercontent.com",
           "scopes": [
               "https://www.googleapis.com/auth/gmail.readonly"
           ]
...

background.js

chrome.identity.getAuthToken({interactive: true}, authorize);

function authorize(token) {
    if (token) {
         //user has given authorization, use token for requests.
         //...
    } else {
         //no authorization received.
         console.log('No authorization. Error: ' + chrome.runtime.lastError);
    }
};

And you don't need to load Google API client, you can access Gmail's Restful API with XMLHttpRequests or Fetch API.

like image 177
Iván Nokonoko Avatar answered Oct 31 '22 06:10

Iván Nokonoko