2016-04-28 102 views
0

我想通过cron访问由我的应用程序创建的日历,并且我得到与应用程序创建的日历具有相同名称的日历,但ID完全不同......这是我的代码:与Cron的Google API日历

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

当我在命令行中执行它(模拟的cron):

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

我得到这个:

Calendar 
Auto-Citas with id:[email protected] 
found 

但是,使用此应用程序创建的日历ID不符合此ID ...
在尝试使用ron之前,我必须学习如何使用API​​,这样我需要删除有时相同的日历。所以我所做的是再次删除我的日历上的Auto-Citas,并在我的应用程序上调用该函数以创建一个具有不同名称的新日历,然后再次提出“模拟cron”的请求(wget www .domain.com/prototipo/alien/cronTest),结果与之前相同:只有一个名为Auto-Citas的日历,但没有任何关于新日历的日历。
功能是创建一个模块反旷工,发送电子邮件或短信给用户约两小时前(cita =约会)
对于这项任务,我必须运行更多...但他们不是重要的为案件:

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

回答

0

好的...我找到解决方案只是here(谷歌官方文档)。我需要使用user_to_impersonate选项。

$client_email = '[email protected]'; 
$private_key = file_get_contents('MyProject.p12'); 
$scopes = implode(' ', array(Google_Service_Calendar::CALENDAR)); 
$user_to_impersonate = '[email protected]'; 
$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, 
);