Google登录后如何将我的活动与Google日历同步

时间:2017-03-29 07:00:56

标签: php google-api google-calendar-api google-api-php-client google-signin

我正在执行一个演示,用于通过事件添加表单将我的事件与Google日历同步。我已经完成了与Google的登录,但现在我无法与Google日历同步。它给我401错误。请帮帮我 这是我的代码帮助将非常感谢:)

调度

<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);
}

1 个答案:

答案 0 :(得分:0)

您尚未正确验证php客户端库。因此,它无法访问您正在尝试做的事情。

$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setAccessType('offline');

您可能想尝试检查Calendar PHP quickstart tutorial

我不知道是否可以将Google登录(客户端登录)与PHP(服务器端)登录链接起来。我严重怀疑它。您可能需要考虑删除PHP并使用JavaScript,请参阅:JavaScript quick start