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

使用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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号