1

我正在尝试将我网站上的新任务添加到 Google 任务。我检查了 Google Tasks Api 文档,这是我发现的代码:

<?php
session_start();
require_once 'google-api-php-client-master/autoload.php';
//Google credentials
$client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
$service_account_name = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.gserviceaccount.com';
$key_file_location = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.p12';
if (!strlen($service_account_name) || !strlen($key_file_location))
    echo missingServiceAccountDetailsWarning();
$client = new Google_Client();
$client->setApplicationName("Whatever the name of your app is");
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/tasks'),
    $key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
    try {
      $client->getAuth()->refreshTokenWithAssertion($cred);
    } catch (Exception $e) {
      var_dump($e->getMessage());
    }
}
$_SESSION['service_token'] = $client->getAccessToken();

// Set task data
$task = new Task();
$task->setTitle('New Task');
$task->setNotes('Please complete me');
$task->setDue(new TaskDateTime('2015-02-26T12:00:00.000Z'));

$result = $service->insertTasks('@default', $task);
echo $result->getId();

?>

它说我“致命错误:在第 34 行的 /customers/1/b/5/xxxxxxx.xx/httpd.www/test2/test2.php 中找不到类‘任务’”。

我做错了什么?我还应该怎么做才能让这段代码正常工作?

4

1 回答 1

0

该文档用于稳定的 api。如果您使用的是主版本,它必须是这样的:

$task = new Google_Service_Tasks_Task();
$task->setTitle('New Task');
$task->setNotes('Please complete me');
$task->setDue(new TaskDateTime('2015-02-26T12:00:00.000Z'));

$result = $service->tasks->insert('@default', $task);
echo $result->getId();
于 2017-05-15T08:20:57.027 回答