2011-10-27 6 views
0

특정 캘린더에서 이벤트를 가져 오기 위해 API를 요청할 때 간혹 302 리디렉션이 발생합니다. 이상한 일은 가끔씩 만 발생하는 경우가 종종 있습니다.Google 캘린더 API 응답 : moved moved temporarily

in : 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. )

답변

3

나는 마침내 그것을 어떻게 알 수 있습니다. 나는이 부분 읽어 http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#RetrievingEvents


하는 것은 당신이이 요청을 GET 전송, 일정은 HTTP 302 리디렉션을 반환 할 수 있습니다; 리디렉션 URL에는 새 쿼리 매개 변수 인 gsessionid가 추가됩니다. (GET 요청을 보내는 몇 가지 방법은 기본적으로 응답 헤더를 표시하지 않을 수도 있지만 빈 응답을 받으면 HTTP 유틸리티 설명서에서 응답 헤더를 보는 방법을 확인하십시오.) gsessionid 매개 변수는 Calendar 응답 속도를 향상시키기 위해 세션을 추적합니다. (GET 요청을 보내는 일부 방법은 자동으로 리디렉션을 따를 수 있으며 경우에 따라 캘린더가 리디렉션을 전혀 보내지 않을 수도 있습니다.이 경우 아래에 설명 된 두 번째 요청을 보낼 필요가 없습니다.) GET 요청을 보낸 경우 세션 ID가 추가 된 URL을 찾으려면 응답의 HTTP 헤더를 읽어야합니다. 새 URL로 다른 GET 요청을 보내야합니다. (UNIX 명령 행을 사용하여 요청을 보내는 경우 새 URL의 물음표 앞에 백 슬래시를 두어 쉘이 해석하지 못하게 할 수 있습니다). 두 번째 GET 요청에 대한 응답으로 Calendar는 다음을 반환합니다. HTTP 200 OK 상태 코드와 캘린더의 모든 이벤트가 포함 된 피드. 캘린더에 이벤트가 하나만있는 경우 캘린더는 다음 피드와 비슷한 것을 반환합니다. 우리는 다음 예제를 조금 더 인간이 읽을 수 있도록 약간 편집했습니다. 특히 실제 캘린더 피드에는 실제 마법 값과 항목 ID가 들어 있습니다.


그래서 API에 요청할 때 응답 헤더를 확인합니다. 응답 코드가 302이면 gsessionid의 값을 잡습니다. 그럼 내가 처음으로했지만 그때 gsessionid 매개 변수 키 및 값 첨부와 정확히 같은 요청을합니다. 이게 날 위해 일하는 것 같아. CakePHP의 2.0 프레임 워크와 몇 가지의 OAuth 클래스와 PHP를 사용하여 내 코드를 표시 할의 poeple, 메신저를 들어

:

   $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

Location으로 리디렉션을하면됩니다.

cURL을 사용하는 경우. 이 옵션을 CURLOPT_FOLLOWLOCATION에서 true으로 설정합니다.

+0

내가는 HTTP 소켓 CakePHP는 2.0을 사용하고 있습니다. 또한 첫 번째 요청에서 header.location 및 쿠키를 사용하여 새 요청을 시도했지만 Google에서 인증 실패 오류가 발생했습니다. – Melvin