Google Calendar API和php

时间:2015-08-19 13:14:40

标签: php google-calendar-api

我使用过谷歌日历api,但用户身份验证porocess显示错误,我没有知道这个错误的原因是什么我严格按照谷歌开发者代码的说明请给我答案。 这是我的身份验证代码

<?php
require 'src/Google/autoload.php';
define('APPLICATION_NAME', 'Google Calendar API Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/calendar-api-quickstart.json');
define('CLIENT_SECRET_PATH', 'client_secret.json');
define('SCOPES', implode(' ', array(
    Google_Service_Calendar::CALENDAR_READONLY)
));

/**
* 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->setAuthConfigFile(CLIENT_SECRET_PATH);
    $client->setAccessType('offline');

    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    if (file_exists($credentialsPath)) {
        $accessToken = file_get_contents($credentialsPath);
    } 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->authenticate($authCode);

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

    // Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {
        $client->refreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, $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();
$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);
    }
}

输出显示错误:

  

致命错误:C:\ xampp \ htdocs \ lapi \ odesk8 \ google_calendar \ src \ Google \ Auth \ OAuth2.php中未捕获的异常'Google_Auth_Exception',消息'无效代码':88堆栈跟踪:#0 C:\ xampp \ htdocs \ lapi \ odesk8 \ google_calendar \ src \ Google \ Client.php(128):Google_Auth_OAuth2-&gt; authenticate('',false)#1 C:\ xampp \ htdocs \ lapi \ odesk8 \ google_calendar \ test.php (33):Google_Client-&gt; authenticate('')#2 C:\ xampp \ htdocs \ lapi \ odesk8 \ google_calendar \ test.php(66):getClient()#3 {main}抛出C:\ xampp \第88行的htdocs \ lapi \ odesk8 \ google_calendar \ src \ Google \ Auth \ OAuth2.php

1 个答案:

答案 0 :(得分:1)

您提供的示例代码从STDIN($authCode = trim(fgets(STDIN));)读取IF ~/.credentials/calendar-api-quickstart.json不可读或不存在

我假设您没有在auth-code中输入,并且samplecode无法找到~/.credentials/calendar-api-quickstart.json,这会导致异常,因为它没有auth-code