2016-11-11 11 views
0

PHPからapiを使用してGoogleカレンダーにイベントを送信しようとしています。これには常にいくつかのエラーがあります。次に何をすべきか理解できません。phpでapiを使用してGoogleカレンダーにイベントを送信するエラーを修正するには

致命的なエラー:メッセージ「エラー呼び出しPOST https://www.googleapis.com/calendar/v3/calendars/[email protected]/events?key= {私はここにあります}:(401)ログインが必要です」の「Google_ServiceException」のキャッチされない例外が見つかりました/ 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 (/ home/abcd/public_html/mouthworks/gplus-verifytoken-php-master/google-api-php-client/google_Even内のGoogle_Even)私はここに気づいてい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 some url – shalder

+1

を呼び出す際にエラーが発生しました。完全なエラーを完全にコピーして質問に入れることができますか? – DaImTo

答えて

0

行の最初の事はあなたのAPI呼び出しを認証されていないとエラー(401)ログインが必要になっている理由があるということです。最初にユーザーを認証してユーザーデータにアクセスする必要があります。詳細はhttps://developers.google.com/api-client-library/php/auth/web-appのドキュメントを参照してください。ユーザーが正常に認証されたら、API呼び出しを行うことができます。もう1つ目は、カレンダーIDに電子メールアドレスを入力していることです。現在ログインしているユーザーのプライマリカレンダーにアクセスする場合は、「primary」キーワードを使用します。あなたのコードは次のようになります:

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

関連する問題