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 .