是否可以从 GMail 小工具中获取 Google 文档电子表格的内容?
这是我到目前为止的想法:
要获取 ID 为“0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc”的电子表格“od6”的内容,您必须访问此 URL: https://spreadsheets.google.com/feeds/cells/0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc/ od6/private/full
(请参阅 API)
如果我将 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(IFrame 父级)为 https://mail.google.com/mail
因此,来自此 IFrame 的 Ajax 调用无法将 cookie 发送到域 https://spreadsheets.google.com< /a> .正确的?响应为空。
然后我尝试了第二种方法。如果我想从 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']。如果我使用 OAuth 注册 https://sslsites.de,我的浏览器将不会通过 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/0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc/ od6/私人/完整
Here is what I figured so far:
To get content of the spreadsheet "od6" with the ID "0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc" you have to acces this URL:
https://spreadsheets.google.com/feeds/cells/0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc/od6/private/full
(see API)
This works fine, if I put the URL into my browser (because I'm logged in to GDocs and the cookies are set).
If I like to access the URL above from within my GMail Gadget, I guess I have 2 options.
- Load it via Ajax
- Load it directly in the PHP that generates the gadget
With the first approach I run into the problem, that the cookies are not known in the Ajax call (I guess this is because of different Domains involved).
Background Info: My Gadget-PHP-Script runs at my host (https://sslsites.de), this Gadget is loaded by GMail and provided trough some kind of proxy (so the Host of my Gadget within the IFrame is http://kipb0cvv7sa9gunc3o5eii57sr1eoem5-a-gm-opensocial.googleusercontent.com/gadgets). Where the URL in the location bar (The IFrames parent) is https://mail.google.com/mail
So it seams an Ajax call from this IFrame can not send cookies to the domain https://spreadsheets.google.com . Right? The response is empty.
Then I tried the second approach. If I like to access the Spreadsheet from PHP I will need OAuth. Which works fine with this code:
<?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>
But again, I have the issue with the domains here. This code runs well on https://sslsites.de . But from GMail I have the issue, that I can not pass the $_COOKIE['token']. If I register https://sslsites.de with OAuth, my browser won't pass the cookie through GMails gadget proxy. If I register the proxy (http://kipb0cvv7sa9gunc3o5eii57sr1eoem5-a-gm-opensocial.googleusercontent.com/gadgets) it selfe for OAuth the Cookie is there, but my PHP script does not know of it as well.
So, is there any way I can pass the Cookie trough the proxy (which seams like a black box to me, cause I can not find any documentation on it)?
Or are there other APIs to access spreadsheets? The spreadsheets I'm talking about are public to everyone, so I would not expect one to need to authenticate.
However the Spreadsheet is public:
https://spreadsheets.google.com/ccc?key=0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc&hl=en&authkey=CJ2ppJsP
But not the Data API call to the same:
https://spreadsheets.google.com/feeds/cells/0Aj3x4n7SOPMRdFA2VmJuampIUDFzdlAwRUwtSEJacmc/od6/private/full
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于弄清楚如何在小工具中使用 OAuth。
您必须在小工具 XML 中定义它,如下所示:
然后您将能够查询电子表格提要:
I finaly figured out how to use OAuth within the gadgets.
You have to define it within the gadgets XML, like this:
Then you will be able to query the spreadsheet feeds: