dongxia19772008 2017-03-29 07:00
浏览 67

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

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);
}
  • 写回答

1条回答 默认 最新

  • dongzhou1901 2017-03-29 07:39
    关注

    You have not properly authenticated the php client library. So it doesn't have access to do what you are trying to do.

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

    You might want to try checking the Calendar PHP quickstart tutorial.

    I have no idea if its even possible to link Google sign-in (Client login) with PHP (server sided) login. I seriously doubt it. You may want to consider dropping PHP and just going with JavaScript see : JavaScript quick start

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探