Php 将google日历API与laravel链接以创建日历和事件

Php 将google日历API与laravel链接以创建日历和事件,php,laravel,google-calendar-api,Php,Laravel,Google Calendar Api,我正在尝试使用谷歌的日历API创建日历,并通过我的应用程序将它们直接保存到我的谷歌日历中,我也想创建活动,但我似乎无法使其正常工作,没有任何错误,但我仍然没有在我的列表中看到日历 $client=new\Google_client(); $client->setApplicationName(“谷歌日历”); $client->addScope(\Google\u Service\u Calendar::Calendar); $client->setAuthConfig('pr-test-4ad

我正在尝试使用谷歌的日历API创建日历,并通过我的应用程序将它们直接保存到我的谷歌日历中,我也想创建活动,但我似乎无法使其正常工作,没有任何错误,但我仍然没有在我的列表中看到日历

$client=new\Google_client();
$client->setApplicationName(“谷歌日历”);
$client->addScope(\Google\u Service\u Calendar::Calendar);
$client->setAuthConfig('pr-test-4ad4a00e3031.json');
$client->setAccessType(“脱机”);
$service=new\Google\u service\u日历($client);
$calendars=new\Google\u Service\u Calendar\u Calendar();
$Calendars->setDescription('ramzi');
$Calendars->setSummary(“测试”);
$service->calendars->insert($calendars);
印刷(日历);
JSON文件具有凭据

这是我打印变量
$calendars

Google\u服务\u日历\u日历对象
(
[conferencePropertiesType:protected]=>Google\u服务\u日历\u ConferenceProperties
[conferencePropertiesDataType:protected]=>
[说明]=>拉姆齐
[etag]=>
[id]=>
[种类]=>
[位置]=>
[摘要]=>测试
[时区]=>
[内部\u gapi\u映射:受保护]=>数组
(
)
[modelData:受保护]=>阵列
(
)
[已处理:受保护]=>阵列
(
)
)

此代码将帮助您实现您的目标(在其中创建新日历和活动):


首先检查
insert
方法调用实际返回了什么…?
<?php
require __DIR__ . '/vendor/autoload.php';

function getClient(){
    $client = new Google_Client();
    $client->setApplicationName('Calendar API PHP');
    $client->addScope(Google_Service_Calendar::CALENDAR);
    $client->setAuthConfig('credentials.json');
    $client->setAccessType('offline');
    $client->setPrompt('select_account consent');

    // Load previously authorized token from a file, if it exists.
    // The file token.json stores the user's access and refresh tokens, and is
    // created automatically when the authorization flow completes for the first
    // time.
    $tokenPath = 'token.json';
    if (file_exists($tokenPath)) {
        $accessToken = json_decode(file_get_contents($tokenPath), true);
        $client->setAccessToken($accessToken);
    }

    // If there is no previous token or it's expired.
    if ($client->isAccessTokenExpired()) {
        // Refresh the token if possible, else fetch a new one.
        if ($client->getRefreshToken()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        } else {
            // Request authorization from the user.
            $authUrl = $client->createAuthUrl();
            printf("Open the following link in your browser:\n%s\n", $authUrl);
            print 'Enter verification code: ';
            $authCode = trim(fgets(STDIN));

            // Exchange authorization code for an access token.
            $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
            $client->setAccessToken($accessToken);

            // Check to see if there was an error.
            if (array_key_exists('error', $accessToken)) {
                throw new Exception(join(', ', $accessToken));
            }
        }
        // Save the token to a file.
        if (!file_exists(dirname($tokenPath))) {
            mkdir(dirname($tokenPath), 0700, true);
        }
        file_put_contents($tokenPath, json_encode($client->getAccessToken()));
    }
    return $client;
}


function createCalendar($service){
    $calendar = new Google_Service_Calendar_Calendar();
    $calendar->setSummary('calendarSummary');
    $calendar->setTimeZone('America/Los_Angeles');
    try {
        $createdCalendar = $service->calendars->insert($calendar);
        echo $createdCalendar->getId();
        return $createdCalendar->getId();
    } catch(Exception $e) {
        printf('An error occured creating the Calendar ' . $e->getMessage());
        return null;
    }    
}

function insertMyevents($service, $calendarId){
    $event = new Google_Service_Calendar_Event(array(
        'summary' => 'Google I/O 2019',
        'location' => '800 Howard St., San Francisco, CA 94103',
        'description' => 'A chance to hear more about Google\'s developer products.',
        'start' => array(
          'dateTime' => '2019-11-13T09:00:00-07:00',
          'timeZone' => 'America/Los_Angeles',
        ),
        'end' => array(
          'dateTime' => '2019-11-14T17:00:00-07:00',
          'timeZone' => 'America/Los_Angeles',
        )
    ));
    try{
        $event = $service->events->insert($calendarId, $event);
    } catch(Exception $e) {
        printf('An error occured inserting the Events ' . $e->getMessage());
    }   
}

if (php_sapi_name() != 'cli') {
    throw new Exception('This application must be run on the command line.');
}

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Calendar($client);
// Creeate new calendar
$calendarId = createCalendar($service);
// Insert events into new Calendar if it was created succesfully
if($calendarId){
    insertMyevents($service, $calendarId);
}