【问题标题】:Google Calendar API and php谷歌日历 API 和 php
【发布时间】:2015-08-19 13:14:39
【问题描述】:

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

<?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:88 堆栈跟踪:#0 C:\ xampp\htdocs\lapi\odesk8\google_calendar\src\Google\Client.php(128): Google_Auth_OAuth2->authenticate('', false) #1 C:\xampp\htdocs\lapi\odesk8\google_calendar\test.php( 33): Google_Client->authenticate('') #2 C:\xampp\htdocs\lapi\odesk8\google_calendar\test.php(66): getClient() #3 {main} 在 C:\xampp\htdocs\ lapi\odesk8\google_calendar\src\Google\Auth\OAuth2.php 在第 88 行

【问题讨论】:

  • 我认为 php 无法解析 $HOME:define('CREDENTIALS_PATH', '~/.credentials/calendar-api-quickstart.json'); - nvm。在那堵代码墙里没有看到expandHomeDirectory
  • 你建议做什么?
  • 检查php是否可以读取定义为CLIENT_SECRET_PATH的文件内容
  • 你能给我一个更好的解决方案或任何链接吗?
  • 不,但我会稍微描述一下。根据您的堆栈跟踪和OAuth2.php line 88,带有此特定异常消息 (Invalid code) 的Google_Auth_Exception 被抛出当且仅当$code var 是 emtpy - 我不知道关于OAuth2,我只是建议您检查 php 是否真的能够找到并读取所述文件 (client_secret.json)。

标签: php google-calendar-api


【解决方案1】:

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

我假设您没有输入授权码,并且示例代码无法找到~/.credentials/calendar-api-quickstart.json,这会导致异常,因为它没有获得授权码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多