2016-10-19 8 views
0

GoogleカレンダーAPIを使用してフルカレンダーでイベントを表示しています(私のビューでjsonオブジェクトを使用しています)。私はcodeignitorのPHPフレームワークを使用しています。私は新しいクライアントを作成するために私のコントローラにいくつかの関数を持っています。次に、oauth2callback()関数を使ってaccess_tokenのコードを交換し、gcalendar() gcalendar_events。私はaccessTypeをオフラインに設定しましたが、それはオフラインでイベントにアクセスできるようには見えません。セッションが終了するたびに再度ログインするようにリダイレクトされている点を除けば、素晴らしいことです。私はそれを望んでいない、私は彼らがセッションが終了した後も常に表示するようにします。 access_tokenが期限切れになって問題が解決するかどうかを確認するために、リフレッシュトークンを使用しようとしています。Googleカレンダーの更新トークンとコード作成ツール

これは私のコントローラのコード

function getClient() { 
     $client = new Google_Client(); 
     $client->setApplicationName("DL Calendar"); 
     $client->setAuthConfig('application/client_secrets.json'); 
     $client->addScope('profile'); 
     $client->setIncludeGrantedScopes(true); 
     $client->setAccessType('offline'); 

     return $client; 
} 

    function gcalendar() { 
    $this->load->add_package_path(APPPATH . 'vendor/autoload'); 

    $client = $this->getClient(); 
    //$client->setRedirectUri(site_url('calendar/index')); 
    $client->addScope(Google_Service_Calendar::CALENDAR); 

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

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

     $service = new ]Google_Service_Calendar($client);      
     $calendar = new Google_Service_Calendar_Calendar(); 
       //$calendarList = $service->calendarList->listCalendarList(); 
     $calendar = $service->calendars->get('primary'); 

       $params = array(
        'owner_id' => get_current_user_id(), 
        'title' => get_current_user(). ' ' .'Google Calendar', 
        'type' => 'gcal', 
        'url' => $calendar->id, 
       ); 

     $calendar_id = $this->Calendar_model->add_calendar($params); 
     redirect('calendar/index'); 

    } else { 

     $redirect_uri = site_url('calendar/oauth2callback'); 
     header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); 
    } 
     $this->session->set_flashdata('success', 'Event Successfully Added'); 
} 

関数oauth2callback(){

//Build the client object 
     $client = $this->getClient(); 
     $client->addScope(Google_Service_Calendar::CALENDAR); 
     $service = new Google_Service_Calendar($client); 

     $url = parse_url($_SERVER['REQUEST_URI']); parse_str($url['query'], $params); 
     $code = $params['code']; 

    //To exchange an authorization code for an access token, use the authenticate method: 
     if (! isset($code)) { 
      $auth_url = $client->createAuthUrl(); 
      header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); 

     } else { 

      $token = $client->fetchAccessTokenWithAuthCode($code);  
      $client->setAccessToken($token); 
      $client->authenticate($code);  
      $_SESSION['access_token'] = $client->getAccessToken(); 
      $redirect_uri = site_url('calendar/gcalendar'); 
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); 
     }    

}

function gcalendar_events() { 
    $client = $this->getClient(); 

    $client->addScope(Google_Service_Calendar::CALENDAR); 
    // $client->setRedirectUri(site_url('calendar/gcalendar')); 
    $client->setAccessType('offline'); //need calendar events to appear even if not logged in to google  
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 

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

     $service = new Google_Service_Calendar($client); 
     $id = 'primary'; 
     $calendar = new Google_Service_Calendar_Calendar(); 
     $calendar = $service->calendars->get('primary'); 

     $event = new Google_Service_Calendar_Event(); 

     $events = $service->events->listEvents($id); 

      foreach ($events->getItems() as $event) {   

       $startTime = strtotime($event->getStart()->dateTime) ; 
       $endTime = strtotime($event->getEnd()->dateTime); 
       $start = date('Y-m-d H:i:s', $startTime); 
       $end = date('Y-m-d H:i:s', $endTime); 

       $eventsArr[] = array(
           'title' => $event->getSummary(),    
           'start'=> $start,     
           'end' => $end, 
          ); 
         } 
         // Return a single `events` with all the `$eventsArr` 
         echo json_encode($eventsArr); 


    } 
} 

私のセッションが終了に問題ですか?またはアクセストークンが期限切れになり、リフレッシュトークンが必要ですか?どこでリフレッシュトークンを設定するのですか?なぜなら、1つ以上の場所に配置しようとしましたが、リフレッシュトークンをsetAccessTokenの一部として設定する必要があるというエラーメッセージが表示されます。私はそれをすべて入れて、まだエラーメッセージを持っています。ここで

は、私はちょうどそれがかつて言って私の許可の「オフラインアクセスを許可する」ために使用されることに気づいた

 if ($client->isAccessTokenExpired()) { 
          $refresh_token =  $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); 
          $client->setAccessToken($refresh_token); 
          $_SESSION['access_token'] = $refresh_token; 
          $this->load->helper('file'); 
         write_file('application/client_secrets.json', json_encode($client->getAccessToken())); 
    } else { 
     $access_token = $_SESSION['access_token']; 
      } 

を使用したコードではありませんが、今、それはもはや言及しているGoogleのドキュメントは、ユーザーの後、「 を言いながら、要求されたスコープへのオフラインアクセスを許可すると、ユーザーがオフラインのときにAPIクライアントを使用してユーザーの代わりにGoogle APIにアクセスできます。クライアントオブジェクトは必要に応じてアクセストークンを更新します。 Googleとの最初の認証時

enter image description here

答えて

0

、あなたは3600秒または1時間で期限切れとなるトークンを受け取ることになります。したがって、新しい作業トークンを取得するにはリフレッシュトークンを使用する必要があります。

SO questionのようなものです。

$token = $client->getAccessToken(); 
$authObj = json_decode($token); 
if(isset($authObj->refresh_token)) { 
save_refresh_token($authObj->refresh_token); 
} 

このrefresh_tokenを必ず保存してください。

あなたはでそれを更新することができます。

$client->refreshToken($your_saved_refresh_token); 

そして、セッションへの新しいアクセストークンを設定します。

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

私はまた、あなたがこのquickstart of Google Calendar for PHPを訪問することをお勧めします。

詳細については、この関連する質問を確認してください。

+0

私はポストに答える誰にもあきらめのだが、私は昨日あなたの答えを持って、それがイベントを取得するために働いていたが、今、私は非常にある、このエラーを取得していますごめんなさい簡単ですが、私はドキュメンテーションを見つけることができません。

メッセージ:{ "エラー": "INVALID_REQUEST"、 "しましたerror_description": "必要なパラメータが不足している:grant_type" を、 "error_uri": ""

@KENdi ..あなたは、パラメータについて何を知っていますか。..することができますそれに関するドキュメンテーションは見つかりません。すばらしい答えをありがとう。それは非常に明確だった – nivanmorgan

関連する問題