Google sheet 使用服务帐户密钥更新和 php
Google sheet update using service account key and php
使用 php api 更新 google sheet 我使用 https://console.developers.google.com/apis 创建了一个服务帐户密钥并将该文件保存在我所在的根文件夹中使用 composer 下载 api 客户端结构。并与服务帐户中提供的客户端电子邮件共享 sheet,并授予其完全访问权限。下面是用于更新的代码:
<?php
require 'vendor/autoload.php';
$service_account_file = 'credentials.json';
$spreadsheet_id = '13zWB1_uY5CdPGLuSXRWAgD0lE7QVrbTDW_9V5lqdNAY';
$spreadsheet_range = 'Sheet1';
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $service_account_file);
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Sheets::SPREADSHEETS);
$service = new Google_Service_Sheets($client);
$options = array('valueInputOption' => 'RAW');
$values = [
["Name", "Roll No", "Contact"],
["AAA", "001", "12345"],
["BBB", "002", "6789"]
];
$body = new Google_Service_Sheets_ValueRange(['values' => $values]);
$result = $service->spreadsheets_values->update($spreadsheet_id, $spreadsheet_range, $body, $options);
?>
问题是它在我的本地主机上工作正常。但是当我将文件夹上传到 000webhost 并从那里 运行 它给出以下错误时:
Caught exception: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information
我需要使用 gcloud 吗?据我所知,我不需要 gcloud 来使用 Google API。如果我需要使用如何使用 gcloud 来让这段代码在我的 000webhost 网站上工作 (https://www.000webhost.com)。
您正在为凭据文件使用相对路径名。您需要使用完整路径。您的代码中的行:
$service_account_file = 'credentials.json';
应改为:
$service_account_file = '/mysecrets/credentials.json';
将目录 mysecrets
更改为您想要的任何内容。如果这是网络服务器,请不要在服务器的 html 目录中找到该文件。
您也可以手动指定文件:
$client->setAuthConfig($service_account_file);
并删除这些行:
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $service_account_file);
$client->useApplicationDefaultCredentials();
使用 php api 更新 google sheet 我使用 https://console.developers.google.com/apis 创建了一个服务帐户密钥并将该文件保存在我所在的根文件夹中使用 composer 下载 api 客户端结构。并与服务帐户中提供的客户端电子邮件共享 sheet,并授予其完全访问权限。下面是用于更新的代码:
<?php
require 'vendor/autoload.php';
$service_account_file = 'credentials.json';
$spreadsheet_id = '13zWB1_uY5CdPGLuSXRWAgD0lE7QVrbTDW_9V5lqdNAY';
$spreadsheet_range = 'Sheet1';
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $service_account_file);
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Sheets::SPREADSHEETS);
$service = new Google_Service_Sheets($client);
$options = array('valueInputOption' => 'RAW');
$values = [
["Name", "Roll No", "Contact"],
["AAA", "001", "12345"],
["BBB", "002", "6789"]
];
$body = new Google_Service_Sheets_ValueRange(['values' => $values]);
$result = $service->spreadsheets_values->update($spreadsheet_id, $spreadsheet_range, $body, $options);
?>
问题是它在我的本地主机上工作正常。但是当我将文件夹上传到 000webhost 并从那里 运行 它给出以下错误时:
Caught exception: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information
我需要使用 gcloud 吗?据我所知,我不需要 gcloud 来使用 Google API。如果我需要使用如何使用 gcloud 来让这段代码在我的 000webhost 网站上工作 (https://www.000webhost.com)。
您正在为凭据文件使用相对路径名。您需要使用完整路径。您的代码中的行:
$service_account_file = 'credentials.json';
应改为:
$service_account_file = '/mysecrets/credentials.json';
将目录 mysecrets
更改为您想要的任何内容。如果这是网络服务器,请不要在服务器的 html 目录中找到该文件。
您也可以手动指定文件:
$client->setAuthConfig($service_account_file);
并删除这些行:
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $service_account_file);
$client->useApplicationDefaultCredentials();