2016-10-12 142 views
2

我希望你能幫助我。Google Calendar API,Google_Service類

我正在嘗試使用Oauth2身份驗證連接到Google(日曆)API。

爲此,我已按照下列步驟操作:

  • 通過谷歌開發者控制檯註冊應用
  • 使用作曲家(谷歌API的PHP客戶端)
  • 在供應商下面放置腳本安裝的客戶端庫文件夾:
 

    require_once 'autoload.php'; 
    require('google/apiclient-services/src/Google/Service/Oauth2.php'); 
    session_start(); 

    // ******************************************************** // 
    // Get these values from https://console.developers.google.com 
    // Be sure to enable the Analytics API 
    // ******************************************************** // 
    $client_id = 'myclientid'; 
    $client_secret = 'myclientsecret'; 
    $redirect_uri = 'https://domain.nl/dev/vendor/google/apiclient-services/src/Google/Service/Oauth2.php'; //identical as in Google console 

    $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 = 'https://' . $_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 "Connect Me!"; 
    }  


    // Step 3: We have access we can now create our service 
    if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']); 
    print "LogOut
"; $service = new Google_Service_Calendar($client); $calendarList = $service->calendarList->listCalendarList();; while(true) { foreach ($calendarList->getItems() as $calendarListEntry) { echo $calendarListEntry->getSummary()."
\n"; // get events $events = $service->events->listEvents($calendarListEntry->id); foreach ($events->getItems() as $event) { echo "-----".$event->getSummary()."
"; } } $pageToken = $calendarList->getNextPageToken(); if ($pageToken) { $optParams = array('pageToken' => $pageToken); $calendarList = $service->calendarList->listCalendarList($optParams); } else { break; } } }


的Un幸運的是,我點擊「接受」按鈕爲Authentication後立即得到一個錯誤:

 
Fatal error: Class 'Google_Service' not found in /home/user/domains/domain.nl/private_html/dev/vendor/google/apiclient-services/src/Google/Service/Oauth2.php on line 32 

Google research沒有幫助至今。

可能的解決方案:

  • 設置自動加載的正確路徑。 檢查

     
    require_once 'autoload.php'; 
    
  • 運行受支持的PHP版本。 檢查(嘗試5.6 + 7.1)。

  • 檢查您在composer.json中的庫和實際正在自動加載的庫之間是否有不同。 檢查

您的幫助表示感謝,謝謝!

回答

0

更新:此問題已修復。我使用Oauth2.php作爲我的redirect_url而不是返回到我的腳本。初學者錯誤:)