0
<?php


if(isset($_GET['token']))
{


    $url="http://www.google.com/calendar/feeds/default/allcalendars/full";
    $useragent="PHP 5.2";
    $header=array(  "GET /accounts/AuthSubSessionToken HTTP/1.1",
                    "Content-Type: application/x-www-form-urlencoded",
                    "Authorization: AuthSub token=".$_GET['token'],
                    "User-Agent: PHP/5.2",
                    "Host: https://www.google.com",
                    "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
                    "Connection: keep-alive"
                );

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_URL, $url); 

    $data = curl_exec($ch); 

    print_r($data);
}
?>

The result is page not found. However, I call http://www.google.com/calendar/feeds/default/allcalendars/full from firefox , it's return XML file. So, I think, my code may wrong. But I can't find the error. :(

saturngod
  • 24,649
  • 17
  • 62
  • 87
  • $_GET['token'] - is it an authorization token or a session token? As I understand, you should first request /accounts/AuthSubSessionToken that returns a session token in a header. Then you use that session token in all subsequent requests. – Kniganapolke Jan 19 '10 at 09:38
  • Try to remove the Host header, however it should be ignored anyway because of the absolute url. – Kniganapolke Jan 19 '10 at 09:58
  • came from https://www.google.com/accounts/AuthSubRequest?scope=http%3A%2F%2Fwww.google.com%2Fcalendar%2Ffeeds%2F&session=1&secure=1&next=http%3A%2F%2Fmydomain.com – saturngod Jan 19 '10 at 14:59
  • remove HOST header. return Unknown authorization header – saturngod Jan 19 '10 at 15:00

3 Answers3

0

That is because you are accessing Google Calendar via your personal port. Whenever you access that specific URL, Google checks to see if you are logged in. If not, it sends a 404. If you are, it outputs the calendar based on the settings you provided. That URL does not specify a specific calendar that it's supposed to pull from the site, and it cannot use the cookies stored on the user's computer because it is being fetched from your server, which will not have any cookies for a calendar. When I try to access that page without logging on, I get a 401 Authorization Required error, which I bet is what PHP is getting and you just don't realize it.

You need to go into your Google Calendar settings and find the embedding options to find a URL that is specific to your account so that it will always fetch an XML feed for your calendar.

Read more about the Google 'Calendar Address' here: http://www.google.com/support/calendar/bin/answer.py?answer=34578

View from other applications: http://www.google.com/support/calendar/bin/answer.py?hl=en&answer=37648

animuson
  • 53,861
  • 28
  • 137
  • 147
  • I change the address and use with my private xml calendar address. I want to use Google Calendar API. I tried from http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#Auth – saturngod Jan 19 '10 at 05:22
0

I think that you may be overriding the URL with this line in the header:

GET /accounts/AuthSubSessionToken HTTP/1.1

I think that will point CURL to http://www.google.com/accounts/AuthSubSessionToken

What happens when you remove it?

Cetra
  • 2,593
  • 1
  • 21
  • 27
  • same problem. I change like that "GET http://www.google.com/accounts/AuthSubSessionToken HTTP/1.1" . but error is 404. – saturngod Jan 19 '10 at 05:17
0

I got it.... I changed like this

<?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(isset($_GET['token']))
{
$sessiontoken=get_session_token($_GET['token']);
$accountxml = make_api_call("http://www.google.com/m8/feeds/contacts/yourmail@gmail.com/full", $sessiontoken);
print_r($accountxml);

}
else
{
$next=urlencode("http://www.mysteryzillion.org/gdata/index.php");
$scope=urlencode("http://www.google.com/m8/feeds/contacts/yourmail@gmail.com/full");
?>
<a href="https://www.google.com/accounts/AuthSubRequest?next=<?= $next ?>&scope=<?= $scope ?>&secure=0&session=1">Click here to authenticate through Google.</a>

<?
}
?>
saturngod
  • 24,649
  • 17
  • 62
  • 87