代码之家  ›  专栏  ›  技术社区  ›  JochenJung

有没有可能从GMail小工具中获取GoogleDocs电子表格的内容?

  •  0
  • JochenJung  · 技术社区  · 14 年前

    到目前为止我是这么想的:

    要获取ID为“0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc”的电子表格“od6”的内容,必须访问以下URL: https://spreadsheets.google.com/feeds/cells/0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc/od6/private/full

    API

    如果我把URL放进浏览器中,这就可以了(因为我已经登录到GDocs,cookies已经设置好了)。

    1. 通过Ajax加载
    2. 直接将其加载到生成gadget的PHP中

    背景信息:我的小工具PHP脚本在我的主机上运行( https://sslsites.de ),这个小工具由GMail加载,并通过某种代理提供(因此IFrame中我的小工具的主机是 http://kipb0cvv7sa9gunc3o5eii57sr1eoem5-a-gm-opensocial.googleusercontent.com/gadgets ). 位置栏(IFrames父级)中的URL是 https://mail.google.com/mail

    因此,来自此IFrame的Ajax调用无法向域发送cookies https://spreadsheets.google.com . 正确的?响应为空。

    然后我尝试了第二种方法。如果我想从PHP访问电子表格,我需要OAuth。这与此代码配合得很好:

    <?php
    function make_api_call($url, $token) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      $curlheader[0] = sprintf("Authorization: AuthSub token=\"%s\"/n", $token);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $curlheader);
      $output = curl_exec($ch);
      curl_close($ch);
      return $output;
    }
    
    function get_session_token($onetimetoken) {
      $output = make_api_call("https://www.google.com/accounts/AuthSubSessionToken", $onetimetoken);
      if (preg_match("/Token=(.*)/", $output, $matches)) $sessiontoken = $matches[1];
      else {
        echo "Error authenticating with Google.";
        exit;
      }
      return $sessiontoken;
    }
    
    if ($_GET['token'] and !$_COOKIE['token']) {
      $_COOKIE['token'] = get_session_token($_GET['token']);
      setcookie("token", $_COOKIE['token']);
    }
    
    if ($_COOKIE['token']) {
      $accountxml = make_api_call("https://spreadsheets.google.com/feeds/cells/tP6VbnjjHP1svP0EL-HBZrg/od6/private/full", $_COOKIE['token']);
      print_r($accountxml);
    }
    ?>
    <a href="https://www.google.com/accounts/AuthSubRequest?scope=https%3A%2F%2Fspreadsheets.google.com%2Ffeeds%2F&session=1&secure=0&next=https%3A%2F%2Fsslsites.de%2Fknox.orgapage.de%2Fdocget.php" target="_blank">Sign in</a>
    

    不过,这里的域名也有问题。这段代码在计算机上运行得很好 https://sslsites.de . 但从GMail我有一个问题,那就是我不能传递$\u COOKIE['token']。如果我注册 https://sslsites.de )它自己为OAuth提供了Cookie,但是我的PHP脚本也不知道它。

    那么,有没有什么方法可以让我把Cookie通过代理服务器传递给我(它就像一个黑盒子,因为我在上面找不到任何文档)?

    或者是否有其他API来访问电子表格?我所说的电子表格对每个人都是公开的,所以我不希望有人需要验证。

    但是电子表格是公开的: https://spreadsheets.google.com/ccc?key=0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc&hl=en&authkey=CJ2ppJsP

    但数据API调用不同: https://spreadsheets.google.com/feeds/cells/0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc/od6/private/full

    1 回复  |  直到 7 年前
        1
  •  0
  •   JochenJung    14 年前

    您必须在gadgets XML中定义它,如下所示:

    <OAuth>
      <Service name="google">
        <Access url="https://www.google.com/accounts/OAuthGetAccessToken" method="GET" />
        <Request url="https://www.google.com/accounts/OAuthGetRequestToken?scope=https://spreadsheets.google.com/feeds/" method="GET" />
        <Authorization url="https://www.google.com/accounts/OAuthAuthorizeToken?oauth_callback=http://oauth.gmodules.com/gadgets/oauthcallback" />
      </Service>
    </OAuth>
    

    function initSSData() {
      var params = {};
      params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
      params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH;
      params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = "google";
      params[gadgets.io.RequestParameters.OAUTH_USE_TOKEN] = "always";
      params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
    
      gadgets.io.makeRequest("https://spreadsheets.google.com/feeds/spreadsheets/private/full?title=GIdea&alt=json", function(response) {
        // Show sign in
        if (response.oauthApprovalUrl) {
          var popup = shindig.oauth.popup({
            destination: response.oauthApprovalUrl,
            windowOptions: null,
            onOpen: function() { document.getElementById('approval').style.display = "none"; document.getElementById('waiting').style.display = "block"; },
            onClose: function() { initSSData(); }
          });
          var personalize = document.getElementById('personalize');
          personalize.onclick = popup.createOpenerOnClick();
          var approvaldone = document.getElementById('approvaldone');
          approvaldone.onclick = popup.createApprovedOnClick();
    
          document.getElementById('approval').style.display = "block";
    
        // Show result
        } else if (response.data) {
          // Access the spreadsheet with response.data
        }
      }
    }