2015-05-13 43 views
0

我想嘗試在我的網站上顯示用戶的Google日曆。我添加了一個顯示的代碼鏈接來添加谷歌日曆。谷歌訪問和返回網址工作正常,但它會在重定向後在我的網站上顯示404錯誤。如何使用PHP向用戶顯示他們的Google日曆?

我還添加了所有正確的細節,如客戶端API,return_uri等

這裏是我的代碼

require_once 'Google/autoload.php'; 
    session_start(); 

    // ******************************************************** // 
    // Get these values from https://console.developers.google.com 
    // Be sure to enable the Analytics API 
    // ******************************************************** // 
    $client_id = '[Your client Id]'; 
    $client_secret = '[Your Client Secret]'; 
    $redirect_uri = '[Your Redirect URI]'; 

    $client = new Google_Client(); 
    $client->setApplicationName("Client_Library_Examples"); 
    $client->setClientId($client_id); 
    $client->setClientSecret($client_secret); 
    $client->setRedirectUri($redirect_uri); 
    $client->setAccessType('offline'); // Gets us our refreshtoken 

    $client->setScopes(array('https://www.googleapis.com/auth/calendar.readonly')); 


    //For loging out. 
    if (isset($_GET['logout'])) { 
    unset($_SESSION['token']); 
    } 


    // Step 2: The user accepted your access now you need to exchange it. 
    if (isset($_GET['code'])) { 

    $client->authenticate($_GET['code']); 
    $_SESSION['token'] = $client->getAccessToken(); 
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; 
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); 
    } 

    // Step 1: The user has not authenticated we give them a link to login  
    if (!isset($_SESSION['token'])) { 

    $authUrl = $client->createAuthUrl(); 

    print "<a class='login' href='$authUrl'>Connect Me!</a>"; 
    }  


    // Step 3: We have access we can now create our service 
    if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']); 
    print "<a class='logout' href='http://www.daimto.com/Tutorials/PHP/GCOAuth.php?logout=1'>LogOut</a><br>"; 

    $service = new Google_Service_Calendar($client);  

    $calendarList = $service->calendarList->listCalendarList();; 

    while(true) { 
     foreach ($calendarList->getItems() as $calendarListEntry) { 

      echo $calendarListEntry->getSummary()."<br>\n"; 


      // get events 
      $events = $service->events->listEvents($calendarListEntry->id); 


      foreach ($events->getItems() as $event) { 
       echo "-----".$event->getSummary()."<br>"; 
      } 
     } 
     $pageToken = $calendarList->getNextPageToken(); 
     if ($pageToken) { 
      $optParams = array('pageToken' => $pageToken); 
      $calendarList = $service->calendarList->listCalendarList($optParams); 
     } else { 
      break; 
     } 
    } 
    } 
+0

重定向後是什麼意思? – DaImTo

+0

是重定向到網站後。我想在日曆 –

+0

中顯示所有用戶事件,如果它登錄後顯示404我懷疑你使用錯誤的重定向uri。註銷仍然有我的網站你有沒有編輯它? – DaImTo

回答

0

檢查是否已正確設置了重定向URI的腳本,並在谷歌開發者控制檯你可能忘記從你複製的教程中改變它。

相關問題