I'm performing a demo for syncing my events with Google calendar from a event adding form. I have completed login with Google but now I'm not able to sync with Google Calendar. Its giving me 401 error. help me out please Here is my code help would be appreciate thanks :)
scheduler
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script>
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log(profile);
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
$('#email').val(profile.getEmail());
}
$('#addScheduler').click(function (e) {
var error = '';
var email=$('#email').val();
var pkShareProfileId=$('#pkShareProfileId').val();
var profileName=$('#profileName').val();
var startDate=$('#startDate').val();
var endDate=$('#endDate').val();
var description=$('#description').val();
$.ajax({
url: 'schedulerAjax.php',
type: 'POST',
data: {'pkShareProfileId': pkShareProfileId, 'profileName':profileName, 'startDate':startDate, 'endDate': endDate, 'description': description, 'email': email},
dataType:'json',
success: showResponse
});
}
</script>
<form method="POST" id="addSchedulerForm" name="addSchedulerForm" enctype="multipart/form-data">
<div class="row">
<p id="errorMsg"></p>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<input type="hidden" id="pkShareProfileId" name="pkShareProfileId">
<input type="hidden" id="email" name="email">
<div class="form-group">
<div class="checkbox g-signin2"data-onsuccess="onSignIn">
Sync With Google Calendar
</div>
</div>
<div class="form-group">
<label for="profileName" class="boxShadowEffect label-basic">Profile Name</label>
<input type="text" class="form-control" id="profileName" placeholder="Profile Name" name="profileName">
</div>
<div class="form-group">
<label for="startDate" class="boxShadowEffect label-basic">Start Date</label>
<input type="text" class="form-control" id="startDate" placeholder="Start Date" name="startDate">
<div class="form-group">
<label for="endDate" class="boxShadowEffect label-basic">End Date</label>
<input type="text" class="form-control" id="endDate" placeholder="End Date" name="endDate">
</div>
</div>
<div class="form-group">
<label for="description" class="boxShadowEffect label-basic">Description</label>
<textarea style="resize:none;" placeholder="Description" id="description" name="description" class="form-control"></textarea>
</div>
<div class="col-xs-10 col-md-12 col-sm-10 col-lg-12">
<input type="button" class="btn hidden-xs btn-primary btn-block btn-flat save" name="addScheduler" value="Save" id="addScheduler" >
</div>
</div>
</div>
</form>
SchedulerAjax.php
require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName("switchit");
$service = new Google_Service_Calendar($client);
$apiCall = new ApiCall();
if(isset($_POST))
{
$shareProfileId=$_POST['pkShareProfileId'];
$profileName=$_POST['profileName'];
$startDate=$_POST['startDate'];
$startDate=strtotime($startDate);
$endDate=$_POST['endDate'];
$endDate=strtotime($endDate);
$description=$_POST['description'];
$email=$_POST['email'];
if ($email) {
$event = new Google_Service_Calendar_Event(array(
'summary' => $profileName,
'description' => $description,
'start' => array(
'dateTime' => '2017-03-28T00:00:00-07:00',
'timeZone' => 'America/Los_Angeles',
),
'end' => array(
'dateTime' => '2017-03-29T00:00:00-07:00',
'timeZone' => 'America/Los_Angeles',
)
));
$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
}