0
votes

When I make an request to the api for getting events from a specific calender sometimes it get an 302 redirect. The weird things is that this only happens sometimes, often 1 out of 5.

in found this: http://code.google.com/p/googlecl/issues/detail?id=167 apparently it has something to do with http and https.

I'm using oauth with this scope; 'scope' => 'https://www.google.com/calendar/feeds/' and the website I'm testing on is http and I'm using https api url just like described here: http://code.google.com/apis/calendar/data/2.0/reference.html

This is the response I get:

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

2 Answers

3
votes

I finally know how to do it know. I read this section: http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#RetrievingEvents


When you send that GET request, Calendar may return an HTTP 302 redirect; the redirect URL has a new query parameter, gsessionid, appended to it. (Note that some methods of sending the GET request may not show you the response headers by default; if you receive a blank response, check your HTTP utility's documentation to find out how to view response headers.) That gsessionid parameter is the way that Calendar keeps track of your session, to improve speed of response. (Some methods of sending the GET request may automatically follow redirects, and in some cases Calendar may not send a redirect at all; in such cases, you don't need to send the second request described below.) So after you've sent the GET request, you have to read the HTTP headers of the response to find the URL with the session ID appended; then you need to send another GET request with that new URL. (Note that if you're using the UNIX command line to send requests, you may have to precede the question mark in the new URL with a backslash to keep your shell from interpreting it.) In response to the second GET request, Calendar returns an HTTP 200 OK status code and a feed containing all the events in your calendar. If there's only one event in your calendar, then Calendar returns something similar to the following feed. We've edited the following example a little to make it a little more readable by humans; in particular, a real Calendar feed contains actual magic-cookie values and entry IDs.


So when I make a request to the API I check the response header. If the response code = 302 I catch the value of the gsessionid. Then i make exactly the same request like I did the first time but then with the gsessionid parameter key and value attached. Looks like this is working for me.

For the poeple who want to see my code, im using PHP with the cakephp 2.0 framework and some oauth class:

                $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
votes

You simply follow the redirect to the Location specified.

If you're using cURL. you set this option CURLOPT_FOLLOWLOCATION to true.