连接到Google Spreadsheet API:无效的令牌格式

时间:2018-09-01 15:17:16

标签: php wordpress google-api

我正在运行php 7.1.8,并且尝试使用Google Sheets API

首先,我启用了Google电子表格API,并下载了提供的credentials.json。在我的文件结构下面找到:(出于安全原因,值已更改;-))

{
  "installed": {
    "client_id": "804286221604-tesdsdasdfasd.apps.googleusercontent.com",
    "project_id": "google-sprea-1535812717813",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://www.googleapis.com/oauth2/v3/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "aSy2313edsidobx9KKJV_aSs",
    "redirect_uris": [
      "urn:ietf:wg:oauth:2.0:oob",
      "http://localhost"
    ]
  }
}

我正尝试通过以下方式访问电子表格:

$client = getClient();
$sheets = new Google_Service_Sheets($client);

$data = [];
$currentRow = 2;

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient()
{
    $client = new Google_Client();
    $client->setApplicationName('Google Sheets API PHP Quickstart');
    $client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY);
    $client->setAccessType('offline');

    // Load previously authorized credentials from a file.
    $credentialsPath = get_theme_file_path() . '/credentials.json';
    if (file_exists($credentialsPath)) {
        $accessToken = json_decode(file_get_contents($credentialsPath), true);
        try {
            $client->setAccessToken($accessToken);
        } catch (Exception $e) {
            print_r($e); // HERE I GET AN ERROR!
        }
    } else {
        return "File not found.";
    }

    return $client;
}

我的凭据路径如下:

C:\Users\admin\Desktop\My Projects\wordpress_project/wp-content/themes/test-child/credentials.json

我得到的错误如下:

Invalid token format
File: C:\Users\admin\Desktop\My Projects\wordpress_project\wp-content\themes\test-child\vendor\google\apiclient\src\Google\Client.php

有人建议我在做什么错吗?

感谢您的答复!

0 个答案:

没有答案