在文件系统中使用谷歌API

Using google APIs in the file system?

本文关键字:谷歌 API 文件系统      更新时间:2023-09-26

您必须在 Google 开发者控制台中设置 javascript origins 以允许某些 origin 访问 google API。问题是我正在制作一个在使用 file://协议的node-webkit中运行的应用程序,而google不允许 file://成为javascript的起源。有什么解决方法吗?我尝试在node-webkit中运行本地http服务器,但这不起作用。

好吧,

您可以使用 http://127.0.0.1/或本地主机URL在Google Console上注册API,但在客户端将侦听器附加到浏览器的窗口,这样当Google api重定向到我们的终端时,我们可以拦截该操作并从URL获取Authorization Code。我已经在phonegap/cordova应用程序上尝试过这个,它工作得很好。检查代码库以供参考。

var authUrl = AUTH_URL + '?' +
'&client_id=' + options.client_id +
'&redirect_uri=' + options.redirect_uri +
'&response_type=code' +
'&scope=' + options.scopez +
'&approval_prompt=force';
//Open the OAuth consent page in the InAppBrowser
var authWindow = window.open(authUrl, '_blank', 'location=no,toolbar=no,clearcache=yes,clearsessioncache=yes,closebuttoncaption="Close"');
authWindow.addEventListener('loadstart', function(e) {
   var url = e.url;
   var code = /'?code=(.+)$/.exec(url);
   var error = /'?error=(.+)$/.exec(url);
   //alert("code : " + JSON.stringify(code));
   if (code || error) {
   //Always close the browser when match is found
      console.log("code : " + code);
      console.log("Closing OAuth Window.");
      authWindow.close();
   }
   if (code) {
   //Exchange the authorization code for an access token
   OAuthService.getAccessTokenByCode(code[1]).then(function(data) {
      deferred.resolve(data);
   }, function(error) {
      deferred.reject(error);
   });
} else if (error) {
   //The user denied access to the app
      deferred.reject({
        error: error[1]
      });
    }
  });