1

我嘗試從Chrome擴展程序連接到我的本地主機服務的Google Endpoints API。我設法將OAuth轉換爲Google Contact API,而不是通過其JS客戶端,但使用https://apis.google.com/js/client.js?onload=loadF從不調用回調函數。當我試圖手動調用它時,在控制檯中出現錯誤: Refused to execute inline event handler because it violates the following Content Security Policy directive。看起來後臺工作者沒有足夠的權限來加載API,但是從控制檯沒有關於它的信息。如何從Chrome擴展程序使用Google Enpoints

我的JS代碼

var apiRoot='//localhost:8080/_ah/api'; 

var loadF = function() { 
    console.log("api init"); 
    gapi.client.load('my_api', 'v1', function(){ 
    console.log("callback"); 
}, apiRoot); 
} 

我的清單:

{ 
    "name": "Linkeer", 
    "version": "1.0", 
    "description": "Linkeer ", 
    "manifest_version": 2, 
    "icons": { 
    "128": "icon128.png" 
    }, 
    "browser_action": { 
    "default_title": "OAuth 2.0", 
    "default_icon": "icon128.png", 
    "default_popup": "options.html" 
    }, 
    "options_page": "options.html", 
    "content_scripts": [ 
    { 
     "matches": ["http://www.google.com/robots.txt*"], 
     "js": ["oauth2/oauth2_inject.js"], 
     "run_at": "document_start" 
    } 
    ], 
    "permissions": [ 
    "https://accounts.google.com/o/oauth2/token", 
    "http://localhost:8080/_ah/api" 
    ], 
    "web_accessible_resources" : [ 
    "oauth2/oauth2.html" 
    ], 
    "content_security_policy": "script-src 'self' http://localhost https://apis.google.com https://www.googleapis.com; object-src 'self'" 

} 

會很高興,如果有人可以幫助我這個問題。

+0

如果您將最低限度的代碼量進行了複製,我會仔細研究一下。 – abraham

回答

1

今天,我不相信這是可能的。至少有兩個問題:

  • 包含腳本的安全策略是一個重要的障礙。
  • 對於使用OAuth的應用程序,OAuth流程中的回調似乎從未正常工作。出現彈出窗口,但不會將憑據傳回給擴展。
1

您可能需要更新您的CSP並使用'unsafe-inline',這會降低安全性,但可能需要Google JS。

"script-src 'self' http://localhost https://apis.google.com https://www.googleapis.com 'unsafe-inline'; object-src 'self'"

+0

不幸的是,'內聯事件'是不允許的。試過'不安全評估',仍然沒有奏效。 – ArturSkowronski

2

對於身份驗證,我建議使用Chrome Identity API,它允許比當前正在使用的擴展庫的OAuth2更簡潔,更容易的實現。

我還沒有找到一個很好的方式來獲取JS擴展中的客戶端庫(儘管在一段時間內沒有嘗試過)。儘管如此,還是有一個open feature request

直接創建API請求爲XMLHttpRequest應該很容易,儘管不使用庫。

+0

我可以證實這種方法有效。我帶了Identity API示例,並稍微調整了它,指向我的Endpoints URL,它運行良好。 –

相關問題