2017-07-17 3 views
0

Je dois utiliser Google Agenda sur mon site. Je l'ai testé sur mon local et il fonctionne:PHP Google Calendar sans ligne de commande

require_once (load_runner::get_dir('LIBS') . "/google-api-php-client-2.2.0/vendor/autoload.php"); 
define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart'); 
define('CREDENTIALS_PATH', '~/.credentials/calendar-php-quickstart.json'); 
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json'); 

// If modifying these scopes, delete your previously saved credentials 
// at ~/.credentials/calendar-php-quickstart.json 
define('SCOPES', implode(' ', array(
     Google_Service_Calendar::CALENDAR) 
)); 
/* 
if (php_sapi_name() != 'cli') { 
    throw new Exception('This application must be run on the command line.'); 
}*/ 

/** 
* Returns an authorized API client. 
* @return Google_Client the authorized client object 
*/ 
function getClient() { 
    $client = new Google_Client(); 
    $client->setApplicationName(APPLICATION_NAME); 
    $client->setScopes(SCOPES); 
    $client->setAuthConfig(CLIENT_SECRET_PATH); 
    $client->setAccessType('offline'); 

// Load previously authorized credentials from a file. 
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH); 
if (file_exists($credentialsPath)) { 
    $accessToken = json_decode(file_get_contents($credentialsPath), true); 
} else { 
    // Request authorization from the user. 
    $authUrl = $client->createAuthUrl(); 
    printf("Open the following link in your browser:\n%s\n", $authUrl); 
    print 'Enter verification code: '; 
    $authCode = trim(fgets(STDIN)); 



    // Exchange authorization code for an access token. 
     $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); 

     // Store the credentials to disk. 
     if(!file_exists(dirname($credentialsPath))) { 
      mkdir(dirname($credentialsPath), 0700, true); 
     } 
     file_put_contents($credentialsPath, json_encode($accessToken)); 
     printf("Credentials saved to %s\n", $credentialsPath); 
    } 
    $client->setAccessToken($accessToken); 

    // Refresh the token if it's expired. 
    if ($client->isAccessTokenExpired()) { 
     $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); 
     file_put_contents($credentialsPath, json_encode($client->getAccessToken())); 
    } 
    return $client; 
} 

/** 
* Expands the home directory alias '~' to the full path. 
* @param string $path the path to expand. 
* @return string the expanded path. 
*/ 
function expandHomeDirectory($path) { 
    $homeDirectory = getenv('HOME'); 
    if (empty($homeDirectory)) { 
     $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); 
    } 
    return str_replace('~', realpath($homeDirectory), $path); 
} 
// Get the API client and construct the service object. 
$client = getClient(); 
$guzzleClient = new \GuzzleHttp\Client(array('curl' => array(CURLOPT_SSL_VERIFYPEER => false,),)); 
$client->setHttpClient($guzzleClient); 
$service = new Google_Service_Calendar($client); 

// Print the next 10 events on the user's calendar. 
$calendarId = 'primary'; 

$optParams = array(
    'maxResults' => 10, 
    'orderBy' => 'startTime', 
    'singleEvents' => TRUE, 
    'timeMin' => date('c'), 
); 
$results = $service->events->listEvents($calendarId, $optParams); 

if (count($results->getItems()) == 0) { 
    print "No upcoming events found.\n"; 
} else { 
    print "Upcoming events:\n"; 
    foreach ($results->getItems() as $event) { 
     $start = $event->start->dateTime; 
     if (empty($start)) { 
      $start = $event->start->date; 
     } 
     printf("%s (%s)\n", $event->getSummary(), $start); 
    } 
} 

Tutorial dit que je dois exécuter mon application avec la ligne de commande (pour obtenir le code de vérification). Mais comment fonctionne mon script sans ligne de commande?

Répondre

1

En supposant que vous suivent ce guide il déclare

Le flux d'autorisation dans cet exemple est conçu pour une application en ligne de commande. Pour plus d'informations sur la procédure d'autorisation dans une application Web, voir Utilisation d'OAuth 2.0 pour les applications de serveur Web.

Voir Using OAuth 2.0 for Web Server Applications:

  1. Ouvrez le Credentials page dans l'API Console.
  2. Cliquez sur "Créer des informations d'identification> ID du client OAuth".
  3. Remplissez le formulaire. Définissez le type d'application sur l'application Web. >> Les applications qui utilisent des langages et des frameworks tels que PHP, Java, Python, Ruby et .NET doivent spécifier des URI de redirection autorisées. Les URI de redirection sont les points d'extrémité auxquels le serveur OAuth 2.0 peut envoyer des réponses. Pour tester, vous pouvez spécifier les URI qui se rapportent à la machine locale, tels que http://localhost:8080.