Google日历API响应:暂时移动

时间:2011-10-27 10:31:47

标签: php cakephp oauth google-calendar-api

当我向api请求获取特定日历的事件时,有时会获得302重定向。奇怪的是,这种情况有时只会发生,通常是5分之一。

发现这个:http://code.google.com/p/googlecl/issues/detail?id=167显然它与http和https有关。

我在这个范围内使用oauth; 'scope'=> 'https://www.google.com/calendar/feeds/'和我正在测试的网站是http,我正在使用https api url,如下所述:http://code.google.com/apis/calendar/data/2.0/reference.html

这是我得到的回复:

HttpResponse Object
(
    [body] => 

Moved Temporarily


Moved Temporarily

The document has moved here. [headers] => Array ( [Expires] => Thu, 27 Oct 2011 10:10:02 GMT [Date] => Thu, 27 Oct 2011 10:10:02 GMT [Set-Cookie] => S=calendar=Vi6DcnO0BrcmQr-qJAQj7A;Expires=Fri, 26-Oct-2012 10:10:02 GMT;Secure [Location] => https://www.google.com/calendar/feeds/6okn9orqcq5kgd2ktssvq675k8%40group.calendar.google.com/private/full?alt=jsonc&oauth_consumer_key=paintballboerderij.nl&oauth_nonce=631ebbb152d8f07466fb3f529973b0ce&oauth_signature=VGIfdFlHFOob/TUAO1ArVeeRQ9U%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1319710200&oauth_token=1/EGOUox6t9u1yOZpRXG7FHFfGwP6bRLTZgUjs6dWSTVk&oauth_version=1.0&start-max=2011-11-19T23:59:59&start-min=2011-11-13T00:00:00&gsessionid=Vi6DcnO0BrcmQr-qJAQj7A [Content-Type] => text/html; charset=UTF-8 [Cache-Control] => private, max-age=0 [X-Content-Type-Options] => nosniff [X-Frame-Options] => SAMEORIGIN [X-XSS-Protection] => 1; mode=block [Server] => GSE [Connection] => close ) [cookies] => Array ( [S] => Array ( [value] => calendar=Vi6DcnO0BrcmQr-qJAQj7A [expires] => Fri, 26-Oct-2012 10:10:02 GMT [secure] => 1 ) ) [httpVersion] => HTTP/1.1 [code] => 302 [reasonPhrase] => Moved Temporarily [raw] => HTTP/1.1 302 Moved Temporarily Expires: Thu, 27 Oct 2011 10:10:02 GMT Date: Thu, 27 Oct 2011 10:10:02 GMT Set-Cookie: S=calendar=Vi6DcnO0BrcmQr-qJAQj7A;Expires=Fri, 26-Oct-2012 10:10:02 GMT;Secure Location: https://www.google.com/calendar/feeds/6okn9orqcq5kgd2ktssvq675k8%40group.calendar.google.com/private/full?alt=jsonc&oauth_consumer_key=p***&oauth_nonce=631ebbb152d8f07466fb3f529973b0ce&oauth_signature=VGIfdFlHFOob/TUAO1ArVeeRQ9U%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1319710200&oauth_token=1/EGOUox6t9u1yOZpRXG7FHFfGwP6bRLTZgUjs6dWSTVk&oauth_version=1.0&start-max=2011-11-19T23:59:59&start-min=2011-11-13T00:00:00&gsessionid=Vi6DcnO0BrcmQr-qJAQj7A Content-Type: text/html; charset=UTF-8 Cache-Control: private, max-age=0 X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block Server: GSE Connection: close Moved Temporarily

Moved Temporarily

The document has moved here. )

2 个答案:

答案 0 :(得分:3)

我终于知道如何知道了。我读了这一节:http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#RetrievingEvents


当您发送该GET请求时,Calendar可能会返回HTTP 302重定向;重定向URL附加了一个新的查询参数gsessionid。 (请注意,默认情况下,某些发送GET请求的方法可能不会显示响应标头;如果收到空白响应,请检查HTTP实用程序的文档以了解如何查看响应标头。)gsessionid参数是Calendar的方式跟踪您的会话,以提高响应速度。 (某些发送GET请求的方法可能会自动跟踪重定向,在某些情况下,Calendar可能根本不会发送重定向;在这种情况下,您不需要发送下面描述的第二个请求。) 因此,在您发送GET请求之后,您必须读取响应的HTTP标头以查找附加了会话ID的URL;那么你需要用新的URL发送另一个GET请求。 (请注意,如果您使用UNIX命令行发送请求,则可能必须在新URL中的问号前面加上反斜杠,以防止shell解释它。)为了响应第二个GET请求,Calendar返回HTTP 200 OK状态代码和包含日历中所有事件的Feed。如果您的日历中只有一个事件,则日历会返回类似于以下Feed的内容。我们稍微编辑了以下示例,以使人类更具可读性;特别是,真正的日历Feed包含实际的魔术Cookie值和条目ID。


因此,当我向API发出请求时,我会检查响应头。如果响应代码= 302,我会捕获gsessionid的值。然后我像第一次做的那样做了完全相同的请求,但随后附加了gsessionid参数键和值。看起来这对我有用。

对于想要查看我的代码的人,我使用PHP与cakephp 2.0框架和一些oauth类:

                $consumer   = $this->createConsumer();
                    $response   = $consumer->get(
                        $settings['Setting']['access_token_key'], 
                        $settings['Setting']['access_token_secret'], 
                        $find['Calendar']['eventFeedLink'],
                        array(
                            'alt'       => 'jsonc',
                            'start-min' => $_sunday . 'T00:00:00',
                            'start-max' => $_saturday . 'T23:59:59',
                            //'singleevents'    => false
                        )
                    );

                    if($response->code == 302 && isset($response->headers['Location']) && !empty($response->headers['Location'])) {

                        $url    = $response->headers['Location']; 
                        $vars = explode('&',$url);
                        foreach($vars as $string){
                             list($is,$what) = explode('=',$string);

                             if($is == "gsessionid") {
                                 $gsessionid    = $what;
                                 break;
                             }
                        }

                        if(isset($gsessionid) && !empty($gsessionid)) {

                            $response   = $consumer->get(
                                $settings['Setting']['access_token_key'], 
                                $settings['Setting']['access_token_secret'], 
                                $find['Calendar']['eventFeedLink'],
                                array(
                                    'alt'           => 'jsonc',
                                    'start-min'     => $_sunday . 'T00:00:00',
                                    'start-max'     => $_saturday . 'T23:59:59',
                                    'gsessionid'    => $gsessionid
                                    //'singleevents'    => false
                                )
                            );

                        }

                    }

答案 1 :(得分:1)

您只需按照指定的Location重定向即可。

如果你正在使用cURL。您将此选项CURLOPT_FOLLOWLOCATION设置为true