ret*_*ere 36 javascript xul firefox-addon google-drive-api
我正在尝试从Firefox扩展程序访问(CRUD)Google云端硬盘.扩展程序以Javascript编码,但现有的两个javascript SDK似乎都不合适; 客户端SDK期望"窗口"可用,扩展中不是这种情况,服务器端SDK似乎依赖于特定于节点的设施,因为在加载时不再适用于节点的脚本它通过browserify运行后在chrome中.我是否坚持使用原始REST调用?有效的Node脚本如下所示:
var google = require('googleapis');
var readlineSync = require('readline-sync');
var CLIENT_ID = '....',
CLIENT_SECRET = '....',
REDIRECT_URL = 'urn:ietf:wg:oauth:2.0:oob',
SCOPE = 'https://www.googleapis.com/auth/drive.file';
var oauth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
var url = oauth2Client.generateAuthUrl({
access_type: 'offline', // 'online' (default) or 'offline' (gets refresh_token)
scope: SCOPE // If you only need one scope you can pass it as string
});
var code = readlineSync.question('Auth code? :');
oauth2Client.getToken(code, function(err, tokens) {
console.log('authenticated?');
// Now tokens contains an access_token and an optional refresh_token. Save them.
if(!err) {
console.log('authenticated');
oauth2Client.setCredentials(tokens);
} else {
console.log('not authenticated');
}
});
Run Code Online (Sandbox Code Playgroud)
我在此脚本上使用browserify包装节点GDrive SDK:
var Google = new function(){
this.api = require('googleapis');
this.clientID = '....';
this.clientSecret = '....';
this.redirectURL = 'urn:ietf:wg:oauth:2.0:oob';
this.scope = 'https://www.googleapis.com/auth/drive.file';
this.client = new this.api.auth.OAuth2(this.clientID, this.clientSecret, this.redirectURL);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在单击按钮后调用它(如果文本字段没有代码则启动浏览器以获取一个):
function authorize() {
var code = document.getElementById("code").value.trim();
if (code === '') {
var url = Google.client.generateAuthUrl({access_type: 'offline', scope: Google.scope});
var win = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow('navigator:browser');
win.gBrowser.selectedTab = win.gBrowser.addTab(url);
} else {
Google.client.getToken(code, function(err, tokens) {
if(!err) {
Google.client.setCredentials(tokens);
// store token
alert('Succesfully authorized');
} else {
alert('Not authorized: ' + err); // always ends here
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
但这会产生错误 Not authorized: Invalid protocol: https:
不过,根据用例,它的兴趣也可能有限。
Firefox 附带了一个小型的 http 服务器,只是一个简单的框架。包含它是为了测试目的,但这并不是忽略它的理由。
让我们按照快速入门指南在 Javascript 中运行云端硬盘应用程序
棘手的部分是设置重定向 URI 和 Javascript 起源。显然正确的设置是http://localhost,但是如何确定每个用户都有可用的端口 80 ?
你不能,除非你能控制你的用户,否则任何端口都不能保证适合所有人。考虑到这一点,让我们选择端口 49870 并祈祷。
现在重定向 URI 和 Javascript Origins 设置为http://localhost:49870
假设您使用 Add-on SDK,请将quickstart.html(记住添加您的客户端 ID)保存在data您的扩展目录中。现在编辑您的main.js
const self = require("sdk/self");
const { Cc, Ci } = require("chrome");
const tabs = require("sdk/tabs");
const httpd = require("sdk/test/httpd");
var quickstart = self.data.load("quickstart.html");
var srv = new httpd.nsHttpServer();
srv.registerPathHandler("/gdrive", function handler(request, response){
response.setHeader("Content-Type", "text/html; charset=utf-8", false);
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
response.write(converter.ConvertFromUnicode(quickstart));
})
srv.start(49870);
tabs.open("http://localhost:49870/gdrive");
exports.onUnload = function (reason) {
srv.stop(function(){});
};
Run Code Online (Sandbox Code Playgroud)
请注意,它quickstart.html不是作为带有resource:URI 的本地文件打开的。Drive API 不会喜欢这样。它通过 url 提供http://localhost:49870/gdrive。不用说,我们可以使用模板或其他任何东西来代替静态 html。还http://localhost:49870/gdrive可以使用常规 PageMod 编写脚本。
我不认为这是一个真正的解决方案。这比什么都没有好。
| 归档时间: |
|
| 查看次数: |
1310 次 |
| 最近记录: |