2015-03-31 81 views
0

据我所知,以下应该工作。它是从示例代码中提取的,并且类似请求like this one解析得很好。是的,范围设置;是的,这个日历是特别分享的。下面的代码 - 你会注意到,我只是试图让事情在这一点上工作,但错误的数据并没有太大帮助:为什么试图拉取事件列表总是返回404错误?

error_reporting(E_ALL); 
session_start(); 

require_once 'google_settings.php'; 
require_once 'google-api-php-client/autoload.php'; 

$client_id = GA_CLIENT_ID; //Client ID 
$service_account_name = GA_SERVICE_EMAIL; //Email Address 
$key_file_location = GA_KEY_FILE_LOCATION; //key.p12 
if ($client_id == '<YOUR_CLIENT_ID>' || !strlen($service_account_name) || !strlen($key_file_location)) { 
    echo missingServiceAccountDetailsWarning(); 
} 
try { 
    $client = new Google_Client(); 
    $client->setApplicationName("Just Testing"); 
    $client->setDeveloperKey(GA_API_KEY); 
    $service = new Google_Service_Calendar($client); 
    if (isset($_SESSION['service_token'])) { 
     $client->setAccessToken($_SESSION['service_token']); 
    } 
    $key = file_get_contents($key_file_location); 
    $cred = new Google_Auth_AssertionCredentials(
     $service_account_name, 
     array('https://www.googleapis.com/auth/calendar'), 
     $key 
    ); 
    $client->setAssertionCredentials($cred); 
    if ($client->getAuth()->isAccessTokenExpired()) { 
     $client->getAuth()->refreshTokenWithAssertion($cred); 
    } 
    $_SESSION['service_token'] = $client->getAccessToken(); 
    $optParams = array(); 
    $results = $service->events->listEvents('xyzxyz', $optParams); 
    echo "<h3>Results Of Call:</h3>\n<pre>"; 
    if (is_array($results)) { 
     foreach ($results as $item) { 
      var_dump($item); 
      echo "\n\n"; 
     } 
    } else var_dump($results); 
    echo "\n</pre>"; 
} 
catch (Exception $e) { 
    echo $e->getMessage(); 
} 

错误我总是这个样子的(无论哪个测试日历我用 - 是的,我已经单独实现的共享):

Error calling GET https://www.googleapis.com/calendar/v3/calendars/xyzxyz?key=<key_appears_here>: (404) Not Found

应该

这项工作?我只是没有看到什么?

谢谢!

+0

“xyzxyz”是您想要列出事件的日历标识吗?您是否已向允许访问用户数据的服务帐户授予域名范围授权(https://developers.google.com/drive/web/delegation)?此外,您可以在Google API Explorer中尝试您的请求https://developers.google.com/google-apps/calendar/v3/reference/events/list – SGC 2015-04-01 17:46:25

+0

是的,“xyzxyz”是日历ID(实际是当然,不同)。是的,我将域名范围授权给了我的服务帐户。我的请求在API浏览器中工作 - 对不起,我也尝试过 - 当我在页面后面查看时,它甚至使用相同的URL与Google进行通信。我完全失望。 – yodarunamok 2015-04-03 02:42:51

+0

再次查看[OAuth文档](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority)后,我想也许我需要指定一个用户,我是这样冒充的:'$ cred - > sub ='[email protected]';',但这没有帮助。那里有任何想法? – yodarunamok 2015-04-09 20:41:28

回答

0

经过一些更多的挖掘我发现this getting started guide。我跟着link in Step 1, part 1;但Calendar API已经在Developer's Console中显示为已启用,所以我不确定这是否做了任何事情。 然而,,我从来没有进入知情同意屏幕在步骤1,第2部分中描述的信息,并且当我测试我的代码在那个点,没有进一步修改,它的工作。

我已经离开了同意画面信息空白,因为我的应用程序只使用公共API访问,但现在看来,同意画面数据必须输入不管你是否打算使用与否。