2016-11-11 82 views
0

我想發送事件到谷歌日曆使用API​​從PHP。但總是有一些錯誤。無法理解下一步該做什麼。這裏是我的代碼:如何解決錯誤發送事件到谷歌日曆使用API​​在PHP

致命錯誤:未捕獲的異常'Google_ServiceException'帶消息'Error calling POST https://www.googleapis.com/calendar/v3/calendars/[email protected]/events?key= {MY is here}:(401)需要登錄'/ home/abcd/public_html/mouthworks/gplus-verifytoken- php-master/google-api-php-client/src/io/Google_REST.php:66堆棧跟蹤:#0/home/abcd/public_html/mouthworks/gplus-verifytoken-php-master/google-api-php-client /src/io/Google_REST.php(36):Google_REST :: decodeHttpResponse(Object(Google_HttpRequest))#1/home/abcd/public_html/mouthworks/gplus-verifytoken-php-master/google-api-php-client/src /service/Google_ServiceResource.php(186):Google_REST :: execute(Object(Google_HttpRequest))#2/home/abcd/public_html/mouthworks/gplus-verifytoken-php-master/google-api-php-client/src/contrib /Google_CalendarService.php(494):Google_ServiceResource - > __ call('insert',Array)#3/home/abcd/public_html/mouthworks/t est.php(24):Google_EventsServiceResource-> insert('some_calendar @ g ...',Object(Google_Even in/home/abcd/public_html/mouthworks/gplus-verifytoken-php-master/google-api-php-client/SRC/IO/Google_REST.php上線66

 require_once './gplus-verifytoken-php-master/ 
     google-api-php-client/src/Google_Client.php'; 
     require_once ' 
     ./gplus-verifytoken-php-master/ 
     google-api-php- client/src/contrib/Google_CalendarService.php'; 

     session_start(); 

     ob_start(); 
     $client = new Google_Client(); 
     $client->setApplicationName('demo'); 
     $client-> 
     setClientId('client id'); 
     $client->setClientSecret('secret'); 
     $client->setRedirectUri('http://someurl.com'); 
     $client-> 
     setDeveloperKey('dev key'); 
     $cal = new Google_CalendarService($client); 

     $event = new Google_Event(); 
     $event->setSummary('Pi Day'); 
     $event->setLocation('Math Classroom'); 
     $start = new Google_EventDateTime(); 
     $start->setDateTime('2016-11-14T10:00:00.000-05:00'); 
     $event->setStart($start); 
     $end = new Google_EventDateTime(); 
     $end->setDateTime('2016-11-14T10:25:00.000-05:00'); 
     $event->setEnd($end); 

     // error is on this next line 
     $createdEvent = 
     $cal->events->insert('[email protected]',$event); 

     echo $createdEvent->id; 

     ?> 
+0

究竟是什麼錯誤? – DaImTo

+0

未捕獲的異常'Google_ServiceException'帶有消息'錯誤調用POST一些URL – shalder

+1

你能否複製完全錯誤並將其置於你的問題中。 – DaImTo

回答

0

在這裏,我注意到的第一件事是,你是不是驗證您的API調用,這就是爲什麼你所得到的錯誤(401)需要登錄。您必須先認證用戶以訪問用戶數據。請參考這裏的文檔https://developers.google.com/api-client-library/php/auth/web-app。用戶成功通過身份驗證後,您可以進行API調用。我注意到的第二件事是您將電子郵件地址放在日曆ID上。如果您想訪問當前登錄用戶的主日曆,請使用「主」關鍵字。你的代碼應該是這樣的:

session_start(); 

$client = new Google_Client(); 
$client->setAuthConfig("client_secrets.json"); 
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/event.php'); 
$client->addScope("https://www.googleapis.com/auth/calendar"); 

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 

    $client->setAccessToken($_SESSION['access_token']); 

    $cal = new Google_Service_Calendar($client); 

    $event = new Google_Service_Calendar_Event(array(
     'summary' => 'Pi Day', 
     'location' => 'Math Classroom', 
     'description' => 'Pi History in detail', 
     'start' => array(
      'dateTime' => '2016-11-14T10:00:00-05:00' 
     ), 
     'end' => array(
      'dateTime' => '2016-11-14T10:25:00-05:00' 
     ), 
     'reminders' => array(
     'useDefault' => FALSE, 
     'overrides' => array(
      array('method' => 'email', 'minutes' => 24 * 60), 
      array('method' => 'popup', 'minutes' => 10), 
     ), 
    ), 
    )); 

    $calendarId = 'primary'; 
    $event = $cal->events->insert($calendarId, $event); 
    printf('Event created: %s\n', $event->htmlLink); 

} else { 

    if (!isset($_GET['code'])) {  

      $auth_url = $client->createAuthUrl(); 
      header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); 

    } else { 

     $client->authenticate($_GET['code']); 
     $_SESSION['access_token'] = $client->getAccessToken(); 

     $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/event.php'; 
     header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); 

    } 

} 

我希望你覺得這個信息很有幫助。我也推薦你閱讀這裏找到的參考文檔https://developers.google.com/google-apps/calendar/v3/reference/events/insert