电子表格库php中的"访问令牌无效"错误

joh*_*doe 6 php cakephp google-sheets

我需要通过google驱动器在cakephp上传电子表格.我使用google-api-php-client生成访问令牌和php-google-spreadsheet-client来访问sheet.Code如下:

function test() {
    require_once '../vendors/google-api-php-client-2.1.1/vendor/autoload.php';
    $client = new \Google_Client();
    $client->setApplicationName("spreadsheet");
    $client->setDeveloperKey("//My developerkey");
    $client = new Google_Client();
    $client->setAuthConfig('client_id.json');
    if( !isset($_GET['code']) ) {

        $client->addScope(Google_Service_Drive::DRIVE);
        $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/test' );
        $auth_url = $client->createAuthUrl();
        header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    } else {

        $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/test' );
        $auth_url = $client->createAuthUrl();
        $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
        $client->setAccessToken($token);

        $serviceRequest = new Google\Spreadsheet\DefaultServiceRequest($token['access_token']);
        Google\Spreadsheet\ServiceRequestFactory::setInstance($serviceRequest);
        $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
        $spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed();
        $spreadsheet = $spreadsheetFeed->getByTitle('Test');
    }
Run Code Online (Sandbox Code Playgroud)

但是我得到访问令牌无效的错误,如下图所示:

在此输入图像描述

我做错了什么?

M A*_*QUI 0

在面对谷歌电子表格的很多问题后,我终于做到了这一点。在您的开发者控制台中创建 web_service 帐户并以 .json 格式下载其凭据,类似于

{
      "type": "service_account",
      "project_id": "rank********er",
      "private_key_id": "*****************************",
      "private_key": "-----BEGIN PRIVATE KEY-----\nkey here\n",
      "client_email": "rank***@appspot.gserviceaccount.com",
      "client_id": "1066978********************",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://accounts.google.com/o/oauth2/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/ra*******r%40appspot.gserviceaccount.com"
    }
Run Code Online (Sandbox Code Playgroud)

然后在您想要使用谷歌电子表格的课程/文件中使用此代码。在使用电子表格之前,您需要将该工作表共享到 json 文件中给出的客户端电子邮件。

<?php
include dirname(__DIR__) . "/vendor/autoload.php";

use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;

$credential = dirname(__FILE__) . "/client_secret.json";
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $credential);
$client = new \Google_Client;
$client->useApplicationDefaultCredentials();

$client->setApplicationName("Some App");
$client->setScopes([
    'https://www.googleapis.com/auth/drive',
    'https://spreadsheets.google.com/feeds'
    ]);

if ($client->isAccessTokenExpired()) {
    $client->refreshTokenWithAssertion();
}
$accessToken = $client->fetchAccessTokenWithAssertion()["access_token"];

$serviceRequest = new DefaultServiceRequest($accessToken);
ServiceRequestFactory::setInstance($serviceRequest);

$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed();
$spreadsheet = $spreadsheetFeed->getByTitle('Spreadsheet Name');
$worksheet = $spreadsheet->getWorksheetFeed()->getEntries()[0];

$listFeed = $worksheet->getListFeed();
$cellFeed = $worksheet->getCellFeed();
$entries = $listFeed->getEntries();

foreach ($listFeed->getEntries() as $entry) {
    $proxy = $entry->getValues();
    $proxy['status'] = $status_message;
    $entry->update($proxy);
}
Run Code Online (Sandbox Code Playgroud)

根据您的更改标题/应用程序名称。您可以通过 Composer 安装以下库来使用上述代码行。

"google/apiclient":"^2.0",
"asimlqt/php-google-spreadsheet-client": "3.0.*",
Run Code Online (Sandbox Code Playgroud)

供参考使用: https: //github.com/asimlqt/php-google-spreadsheet-client