谷歌日历插入出现403权限错误



我正在尝试创建一个事件。我的谷歌日历列出事件正确,但当我试图插入一个事件,它给了我一个错误。我做错了什么

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events: (403) Insufficient Permission' in /Library/WebServer/Documents/vendor/google/apiclient/src/Google/Http/REST.php:110 Stack trace: #0 /Library/WebServer/Documents/vendor/google/apiclient/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) #1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request)) #2 /Library/WebServer/Documents/vendor/google/apiclient/src/Google/Task/Runner.php(174): call_user_func_array(Array, Array) #3 /Library/WebServer/Documents/vendor/google/apiclient/src/Google/Http/REST.php(46): Google_Task_Runner->run() #4 /Library/WebServer/Documents/vendor/google/apiclient/src/Google/Client.php(590): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) #5 /Library/WebServer/Documents/vendor/google/apiclient/src/Google/Service in /Library/WebServer/Documents/vendor/google/apiclient/src/Google/Http/REST.php on line 110

这是我的代码。

<?php
require 'vendor/autoload.php';
date_default_timezone_set('America/Denver');
define('APPLICATION_NAME', 'Google Calendar API Quickstart');
define('CREDENTIALS_PATH', '/tmp/calendar-api-quickstart.json');
define('CLIENT_SECRET_PATH', 'client_secret.json');
define('SCOPES', implode(' ', array(
  Google_Service_Calendar::CALENDAR)
));
/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient() {
  $client = new Google_Client();
  $client->setApplicationName(APPLICATION_NAME);
  $client->setScopes(SCOPES);
  $client->setAuthConfigFile(CLIENT_SECRET_PATH);
  $client->setAccessType('offline');
  // Load previously authorized credentials from a file.
  $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
  if (file_exists($credentialsPath)) {
    $accessToken = file_get_contents($credentialsPath);
  } else {
    // Request authorization from the user.
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:n%sn", $authUrl);
    print 'Enter verification code: ';
    $authCode = '4/ubgInVASpWF5A3zk0hf0ugqwQt__4GOWEGQ7xzHYlTQ';
    // Exchange authorization code for an access token.
    $accessToken = $client->authenticate($authCode);
error_log(json_encode($credentialsPath));
    // Store the credentials to disk.
    if(!file_exists(dirname($credentialsPath))) {
      mkdir(dirname($credentialsPath), 0700, true);
    }
    file_put_contents($credentialsPath, $accessToken);
    printf("Credentials saved to %sn", $credentialsPath);
  }
  $client->setAccessToken($accessToken);
  // Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
    $client->refreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, $client->getAccessToken());
  }
  return $client;
}
/**
 * Expands the home directory alias '~' to the full path.
 * @param string $path the path to expand.
 * @return string the expanded path.
 */
function expandHomeDirectory($path) {
  $homeDirectory = getenv('HOME');
  if (empty($homeDirectory)) {
    $homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
  }
  return str_replace('~', realpath($homeDirectory), $path);
}
error_log("here");
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event(array(
    'summary' => 'Google I/O 2015',
    'location' => '800 Howard St., San Francisco, CA 94103',
    'description' => 'A chance to hear more about Google's developer products.',
    'start' => array(
        'dateTime' => '2015-08-28T09:00:00-07:00',
        'timeZone' => 'America/Los_Angeles',
    ),
    'end' => array(
        'dateTime' => '2015-08-28T17:00:00-07:00',
        'timeZone' => 'America/Los_Angeles',
    ),
    'recurrence' => array(
        'RRULE:FREQ=DAILY;COUNT=2'
    ),
    'attendees' => array(
        array('email' => 'lpage@example.com'),
        array('email' => 'sbrin@example.com'),
    ),
    'reminders' => array(
        'useDefault' => FALSE,
        'overrides' => array(
            array('method' => 'email', 'minutes' => 24 * 60),
            array('method' => 'popup', 'minutes' => 10),
        ),
    ),
));
$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %sn', $event->htmlLink);


// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
$optParams = array(
  'maxResults' => 10,
  'orderBy' => 'startTime',
  'singleEvents' => TRUE,
  'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
if (count($results->getItems()) == 0) {
  print "No upcoming events found.n";
} else {
  print "Upcoming events:n";
  foreach ($results->getItems() as $event) {
    $start = $event->start->dateTime;
    if (empty($start)) {
      $start = $event->start->date;
    }
    printf("%s (%s)n", $event->getSummary(), $start);
  }
}

我想我明白了

所以当你请求token (scopes)

Google_Service_Calendar: CALENDAR_READONLY

它在我的TMP目录中创建了一个凭证文件,所有的令牌都来自那里。我必须删除它,用

重新生成令牌

Google_Service_Calendar:日历

然后我可以创建事件

谢谢

最新更新