【发布时间】:2015-04-13 16:28:03
【问题描述】:
我目前正在使用 PHP 编写一个表格,该表格将合并两个不同的任务:为活动预订空间,以及为在线日历注册活动。我已经用 PHP 的 API 客户端确定了 Google 日历,但 Calendar Resources 仍然被证明是一个问题。
以下是关于我目前所在位置的一些信息:
- 我正在使用通用 API 来处理日历和资源范围的 OAuth2 授权。
- 我使用访问令牌向资源提要发出 cURL GET 请求,以尝试检索所有可用的 XML 数据 资源。
- 假设上述方法有效,我将使用从 XML 中提取的资源电子邮件来邀请选定的资源参加使用日历 API 制作的事件。
我目前的困境涉及我从调用中返回的数据。当我使用 Google OAuth2 Playground 使用上述 URI 发出请求时,它会毫无问题地检索 XML 数据。当我在 PHP 中进行同样的尝试时,我得到了具有以下格式的单个字符串中的数据:
- https://apps-apis.google.com/a/feeds/calendar/resource/2.0/域/5798411496212015-02-12T18:42:23.268Z
- "request_uri"."resource_id"."date_of_access" (x 100)
我知道每个请求每个请求只会返回 100 个资源,并且操场和我的 php 都返回似乎是 100 个条目,所以它似乎不一定是授权问题。话虽这么说,但我不确定我会做哪些与操场不同的事情。
我的 cURL 请求如下(检索到 access_token 后):
$url = 'https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/';
$token = json_decode($_SESSION['access_token'], true);
$access_token = $token['access_token'];
$token_type = $token['token_type'];
//open connection
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array('Authorization: '.$token_type.' '.$access_token,),
));
// $output contains the returned data
$output = curl_exec($ch);
if($output === false) { echo 'Curl error: ' . curl_error($ch); }
else { echo $output; }
// close curl resource to free up system resources
curl_close($ch);
我是否缺少某些东西,cURL 设置或标题?我猜不出它会是什么,尤其是当 Playground 的请求只是以下内容时:
GET /a/feeds/calendar/resource/2.0/domain/ HTTP/1.1
Host: apps-apis.google.com
Content-length: 0
Authorization: Bearer access_token
更新
Hans Z. 的回答促使我(谢谢!)寻找将 XML 数据放入数组的方法,因为我需要使用获得的数据而不是显示它。 (我很抱歉最初没有说清楚。)There are a lot of posts on the subject,在通过 SimpleXML 输出输出后,我检索以下内容作为每个资源 (x100) 的条目:
[0] => SimpleXMLElement Object
(
[id] => https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621
[updated] => 2015-02-18T17:04:16.481Z
[link] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => self
[type] => application/atom+xml
[href] => https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => edit
[type] => header('Content-type: application/xml');
[href] => https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621
)
)
)
)
如上所示,这不提供有关资源本身的任何信息,例如它的名称或电子邮件(或 id,以任何直接无解析方式)。我尝试通过 SimpleXML 从操场运行 XML 输出,并且正在恢复完全相同的输出。假设它与 SimpleXML 相关,我进行了更多搜索,发现了 this question、this question 和 this blog post。我自己一直在研究他们的建议,但到目前为止收效甚微。如果有人想在我之前弄清楚(因为我必须在今天剩下的大部分时间里参加会议),这里是 XML 结构的示例:
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:apps='http://schemas.google.com/apps/2006'>
<id>https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain</id>
<updated>2015-02-18T17:03:03.057Z</updated>
<link rel='next' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/?start=579841149621'/>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain'/>
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain'/>
<link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain'/>
<openSearch:startIndex>1</openSearch:startIndex>
<entry>
<id>https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621</id>
<updated>2015-02-18T17:03:03.056Z</updated>
<link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621'/>
<link rel='edit' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621'/>
<apps:property name='resourceId' value='579841149621'/>
<apps:property name='resourceCommonName' value='Projector'/>
<apps:property name='resourceEmail' value='domain_key@resource.calendar.google.com'/>
</entry>
//99 more times:
<entry>
...
</entry>
【问题讨论】:
-
在 php github.com/google/google-api-php-client 上查看此链接以获取 oauth 客户端
-
你给token_type作为“Bearer”吗??
-
@SGC $token_type 是从使用 PHP 客户端获得的令牌中提取的 -
$_SESSION['access_token']- 其值为 Bearer。这将传递给 cURL 调用。 -
@SGC 更正:
$_SESSION['access_token']是令牌本身,$_SESSION['token_type']的值为“Bearer”。 -
@NickMischler:您的代码表明
$_SESSION['access_token']是一个包含实际访问令牌和令牌类型的JSON 对象;是哪个?
标签: php xml curl google-admin-sdk