【发布时间】:2021-03-15 23:46:19
【问题描述】:
我成功检索到令牌。以下是我尝试使用 OAuth 的步骤。
-
创建你的 manifest.json
{ "name": "Sample Extension", "version": "1.0", "description": "Hello World", "permissions": ["identity", "https://docs.google.com/spreadsheets/"], "author": "Jess", "background": { "scripts": ["background.js"], "persistent": true }, "content_scripts": [{ "matches": ["file:///*"], "js" : ["popup.js"] }], "browser_action": { "default_popup": "popup.html", "default_icon": "images/get_started16.png", "default_title": "This is a sample extension" }, "oauth2": { "client_id": "client_ID", "scopes": [ "https://www.googleapis.com/auth/spreadsheets" ]}, "content_security_policy": "script-src 'self' 'unsafe-eval' https://apis.google.com/; object-src 'self'", "manifest_version": 2 } Client_ID在https://console.developers.google.com中可用。
- 您需要创建 OAuth 客户端 ID 凭据并选择 Chrome 应用程序作为应用程序类型。确保应用程序 ID 与扩展程序的 ID 相似。然后复制生成的
clientID。我已经从控制台启用了 Sheets API。
生成clientID。
-
这是
background.js:chrome.identity.getAuthToken({ 'interactive': true }, getToken); function getToken(token) { console.log('this is the token: ', token); }
我将在此处添加其他文件:
pop.js
function popup(e) {
var henlo = "I\'m here for you :)";
alert("Him : " +henlo );
}
var plusBtn = document.querySelector('#clickMe');
plusBtn.addEventListener('click', popup);
pop.html
<!DOCTYPE html>
<html>
<head>
<style>
body {
width: 100px;
height: 100px
}
button {
background-color:#577FC6;
color: #ffffff;
margin:0 auto;
border: 1px solid #000000;
display:block;
width:80px;
}
</style>
</head>
<body>
<span>Try and click the button for surprise :) </span>
<button id="clickMe">Click Me!</button>
<script src="popup.js"></script>
</body>
</html>
这是令牌的日志,已成功检索。
目前,我的问题是如何使用 GAPI 客户端访问电子表格。我也试过这个github post的方法,最终我遇到了这个错误:
在“https://apis.google.com/js/client.js”访问 XMLHttpRequest 从原产地 'chrome-extension://fajgeimckmkdokmdbpkglamjpfcekcpp' 有 被 CORS 策略阻止:没有“Access-Control-Allow-Origin”标头 存在于请求的资源上。
有人可以解决这个问题吗?
【问题讨论】:
-
我也从documentation 尝试了这种方法,为了访问 Google 帐户的安全目的,它需要我的扩展程序被发布/列入白名单。我仍处于我的应用程序的测试阶段,所以我还不打算在 CWS 中注册我的扩展程序。
标签: google-chrome google-chrome-extension google-sheets