1

I try to create some chrome extension in JS and I need to use Gmail API on the extension in my content script . I am trying with gmail.js gitHub library and with inboxSdk to use it .

So , I create the oauth2 in background.js file and I try to use gapi.client.gmail in my content.js file .. but it's always undefined gapi.client no matter what I try . I success to send request and get response from Gmail API in the background.js page after I load the Api , but not in the content script .

This is my manifest file :

{
"name": "Example Gmail Extension",
 "content_scripts": [
  {
    "matches": [
     "https://mail.google.com/*",
     "http://mail.google.com/*" ],  
    "js": ["inboxsdk.js","content.js"],
    "run_at": "document_end"
  }
],  
"background": {
  "scripts": ["background.js" ,"client.js"]
},
"content_security_policy": "script-src https://*.google.com 'unsafe-eval';       object-src 'self'",
"oauth2": {
  "client_id": <clientid>,
  "scopes": ["https://www.googleapis.com/auth/gmail.modify"] 
 },
"permissions": [
    "background",
    "identity",
    "storage",
    "https://www.googleapis.com/*",
    "https://*.googleusercontent.com/*",
     "https://mail.google.com/",
     "http://mail.google.com/" ], 
 "manifest_version": 2
 }

when client.js contain the Gmail API libary for javaScript . I using this Loading Google API Javascript Client Library into Chrome Extension to load it like this.

my background file contain just the oauth and the load of Gmail Api .. when it's load it's recgonize gapi.client and I can create request and to get response.

chrome.identity.getAuthToken(
{'interactive': true},
function(token){
}  

window.gapi_onload = function(){
gapi.auth.authorize(
    {
        client_id: '<clientid',
        immediate: true,
        scope: 'https://www.googleapis.com/auth/gmail.modify'
    },
    function(){
      gapi.client.load('gmail', 'v1', function(){ 

       *here I sucess to call to gmail API with gapi.client....* 
    });
    }
);

Am I doing something wrong ? I try also to set the client.js in the content scripts but it's not help-same error.

Thanks all .

Community
  • 1
  • 1
OriEng
  • 1,424
  • 18
  • 34
  • What *exactly* is shown in the [various appropriate consoles for your extension](http://stackoverflow.com/a/38920982/3773011) when you load and execute your extension? – Makyen Dec 07 '16 at 16:31
  • gapi is not defined .. – OriEng Dec 08 '16 at 07:49
  • As stated [here](http://stackoverflow.com/a/29487829/5832311), you can only load things in the content script context by calling `executeScript` or declaring them in the manifest; and GAPI will try to load more libraries with the ` – abielita Dec 10 '16 at 18:42

1 Answers1

2

While convenient, Google's Javascript client library (gapi.*) is not required to call Google APIs from within your extension. In fact, it may not be a terribly good idea to use that library. You have to add the "unsafe-eval" policy (and it is a called unsafe for a reason) and then dynamically load the gapi script (which you do not do in your code, which is probably the root cause of your troubles) or alternatively package the gapi library in your extension, which is a bad idea purely from a software lifecycle point of view.

Bottomline: you should directly call the HTTP ("REST") Google APIs using XMLHttpRequest or (preferably) the HTML5 Fetch API.

Soeren
  • 691
  • 5
  • 12
  • Thank you . I think that you are right . The best idea is to get user token in background.js page and to forward it to the content script and then create XMLHttpRequest requests for the gmail API. Any way I get some answer here to my question: [Here](https://github.com/KartikTalwar/gmail.js/issues/350) . They says there: " You cannot use the Gmail.js API inside a content-script. If you need to do separate processing in the browser context, content-script and background-scripts, you will need to rely on cross-context messaging .... " – OriEng Dec 18 '16 at 16:10