2016-12-07 111 views
0

我嘗試在JS中創建一些Chrome擴展,並且需要在內容腳本的擴展中使用Gmail API。 我正在試着用gmail.js gitHub庫和inboxSdk來使用它。在內容腳本的Chrome擴展中使用Gmail Api

因此,我在background.js文件中創建oauth2,並嘗試在我的content.js文件中使用gapi.client.gmail ..但無論我嘗試什麼,它總是未定義gapi.client。 我成功發送請求,並在加載Api後在background.js頁面中從Gmail API獲取響應,但未在內容腳本中。

這是我的清單文件:

{ 
"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 
} 

時client.js包含的JavaScript API的Gmail libary。 我使用這個Loading Google API Javascript Client Library into Chrome Extension像這樣加載它。

我的背景文件只包含Gmail API的oauth和負載。當它加載時,它會重新調整gapi.client,我可以創建請求並獲取響應。

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

我做錯了什麼? 我也嘗試在內容腳本中設置client.js,但它不是幫助相同的錯誤。

謝謝大家。

+0

加載並執行擴展時,在[適用於您的擴展的各種適當的控制檯](http://stackoverflow.com/a/38920982/3773011)中顯示* exact *是什麼? – Makyen

+0

gapi沒有定義.. – OriEng

+0

正如[here](http://stackoverflow.com/a/29487829/5832311)所述,您只能通過調用'executeScript'或在manifest中聲明它們來加載內容腳本上下文中的內容; GAPI將嘗試使用'

相關問題