2017-12-07 2 views
0

GoogleのAPIを使用してGmailのユーザーとloggearmeに接続し、私が持っているカレンダーを取得しようとしています。 私が得るものはloggearmeで、そのoauth2はTOKENを返しますが、このトークンを取得すると、Googleカレンダーの詳細を取得する方法はわかりません。Google Api PHPクライアント - ログイン方法

は、Googleにログインを要求すると

のsettings.php

/* Google App Client Id */ 
define('CLIENT_ID', 'XXXXXXXX.apps.googleusercontent.com'); 

/* Google App Client Secret */ 
define('CLIENT_SECRET', 'XXXXXXX'); 

/* Google App Redirect Url */ 
define('CLIENT_REDIRECT_URL', 
'http://localhost:8081/googleTesting/oauth2callback.php'); 


    <html> 
<head>....</head> 

<body> 



<a href="<?= 'https://accounts.google.com/o/oauth2/auth?scope=' . urlencode('https://www.googleapis.com/auth/userinfo.profile') . '&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online' ?>">Login with Google</a> 
<br> 

    </body> 
</html> 

Calendar.php

// We have access we can now create our service 
if (isset($_SESSION['datosGoogle']['access_token'])) { 

$client = new Google_Client(); 
$client->setAccessToken($_SESSION['datosGoogle']['access_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; 
    } 
} 

}

答えて

1

あなたはスコープを追加ありがとうその要求。

Googleが良い答えです。 Google tutorial,Google php implementation

この部分は重要です。

//import libraries php composer.phar require google/apiclient:^2.0 
require_once __DIR__ . '/vendor/autoload.php'; 

define('SCOPES', implode(' ', array(
    Google_Service_Calendar::CALENDAR_READONLY) 
)); 
$client->setScopes(SCOPES); 
+0

Thxs、これは私が必要とするものです。 – ilernet

関連する問題