Google Calendar API访问令牌

时间:2018-06-01 12:53:00

标签: json token credentials google-calendar-api

尝试关注PHP快速入门Google Calendar API我遇到错误: 'InvalidArgumentException',消息为“无效的令牌格式”

我有以下内容:

function getClient()
{
    $client = new Google_Client();
    $client->setApplicationName('Google Calendar API PHP Quickstart');
    $client->setScopes('https://www.googleapis.com/auth/calendar');
    $client->setAuthConfig('client_secret.json');
    $client->setAccessType('offline');

    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory('credentials.json');

    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;
}

我不明白。根据PHP Quickstart示例,有两个JSON: client_secret.json credentials.json 。 我所做的是从我创建的凭据中获取JSON以使用API​​。我将此JSON用于client_secret和凭据。 我认为这个JSON只适用于两个文件中的一个,但我不知道哪一个。有人可以澄清我必须使用哪些文件, 以及如何取悦他们?

0 个答案:

没有答案