是否可以从 GMail 小工具中获取 Google 文档电子表格的内容?
-
25-09-2019 - |
题
到目前为止,这是我的想法:
要获取 ID 为“0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc”的电子表格“od6”的内容,您必须访问以下 URL:https://spreadsheets.google.com/feeds/cells/0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJcmc/od6/private/full
(看 应用程序编程接口)
如果我将 URL 输入浏览器(因为我已登录 GDocs 并且已设置 cookie),则效果很好。
如果我想从我的 GMail Gadget 中访问上面的 URL,我想我有 2 个选择。
- 通过 Ajax 加载
- 直接在生成小工具的 PHP 中加载
使用第一种方法时,我遇到了问题,即 Ajax 调用中不知道 cookie(我猜这是因为涉及不同的域)。
背景信息:我的 Gadget-PHP-Script 在我的主机上运行(https://sslsites.de),这个小工具由 GMail 加载并通过某种代理提供(因此 IFrame 中我的小工具的主机是 http://kipb0cvv7sa9gunc3o5eii57sr1eoem5-a-gm-opensocial.googleusercontent.com/gadgets)。地址栏中的 URL(IFrames 父级)是 https://mail.google.com/mail
因此,来自此 IFrame 的 Ajax 调用无法将 cookie 发送到域 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 我遇到了问题,我无法传递 $_COOKIE['token']。如果我注册 https://sslsites.de 使用 OAuth,我的浏览器不会通过 GMails 小工具代理传递 cookie。如果我注册代理(http://kipb0cvv7sa9gunc3o5eii57sr1eoem5-a-gm-opensocial.googleusercontent.com/gadgets) OAuth 的 Cookie 本身就在那里,但我的 PHP 脚本也不知道它。
那么,有什么方法可以通过代理传递 Cookie(这对我来说就像一个黑匣子,因为我找不到任何关于它的文档)?
或者还有其他 API 可以访问电子表格吗?我正在谈论的电子表格对所有人都是公开的,因此我不认为需要进行身份验证。
然而,电子表格是公开的:https://spreadsheets.google.com/ccc?key=0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc&hl=en&authkey=CJ2ppJsP
但数据 API 调用却不同:https://spreadsheets.google.com/feeds/cells/0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJcmc/od6/private/full
解决方案
我终于弄清楚如何在小工具中使用 OAuth。
您必须在小工具 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
}
}
}