doude1917 2016-04-28 13:20
浏览 74
已采纳

使用Cron的Google API日历

I am trying to access to a calendar created by my application via cron, and I get a calendar with the same name that the calendar the application creates, but the id is absolutely different... This is my code:

public function cronTest()
{
    $this->g_client = new Google_Client();
    $this->g_client->setApplicationName($this->config->item("APPLICATION_NAME"));
    $service    = $this->getCronService("CalendarTest-46bde015a16.p12");
    $calendar   = $this->getCalendar($service);
}
private function getCronService($file)
{
    $key  = file_get_contents(CREDENTIALS_PATH.$file); 
    $cred = new Google_Auth_AssertionCredentials($this->config->item("google_service_id"), SCOPES, $key);
    $this->g_client->setAssertionCredentials($cred);
    if($this->g_client->getAuth()->isAccessTokenExpired())
         $this->g_client->getAuth()->refreshTokenWithAssertion($cred);
    return new Google_Service_Calendar($this->g_client);
}
private function getCalendar($service)
{
    $calendarList = $service->calendarList->listCalendarList();
    echo "getCalendar<br>";
    foreach ($calendarList->getItems() as $calendarListEntry)
    {
        echo $calendarListEntry->getSummary()." with id:".$calendarListEntry->getId()."<br>";
        echo "<br>";
        if($calendarListEntry->getSummary()=="Auto-Citas")
            echo "found";
            //return $calendarListEntry->getId();
    }
    die;
}

When I execute it from the command line (simulating the cron):

wget www.domain.com/prototipo/alien/cronTest

I get this:

Calendar  
Auto-Citas with id:h0gefmo7vjqlr4lp0r2n93vk9c@group.calendar.google.com  
found

But, the id of calendar created with this application doesn´t match with this id...
Before this attempt with ron, I had to learn how to use the API, in this way I needed to remove sometimes the same calendar. So what I do, was remove one more time the Auto-Citas on my calendar, and call the function on my app to create a new calendar with a different name, then I made again the request of the "simulated cron" (wget www.domain.com/prototipo/alien/cronTest) and the result is the same than before: only one calendar called Auto-Citas, but nothing about the new calendar.
The functionality is to create a module anti-absenteeism, sending an email or sms to the user two hours before the appointment(cita=appointment)
For this tasks I have to function more... but they aren´t important for the case:

$events     = $this->getDates($service,$calendar,$min,$max);
$this->transformDates($events, $service, ",phone");
  • 写回答

1条回答 默认 最新

  • doqdcks3948 2016-05-01 11:01
    关注

    Ok... I find solution just here (the google official docs). I need to use the option of user_to_impersonate.

    $client_email = '1234567890-a1b2c3d4e5f6g7h8i@developer.gserviceaccount.com';
    $private_key = file_get_contents('MyProject.p12');
    $scopes = implode(' ', array(Google_Service_Calendar::CALENDAR));
    $user_to_impersonate = 'user@example.org';
    $credentials = new Google_Auth_AssertionCredentials(
        $client_email,
        $scopes,
        $private_key,
        'notasecret',                                 // Default P12 password
        'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
        $user_to_impersonate,
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题