我可以使用开发人员密钥通过不带Oauth的Google Sheets API写入Google Sheet吗? [英] Can I write to Google Sheet via Google Sheets API without Oauth, using developer key?

查看:237
本文介绍了我可以使用开发人员密钥通过不带Oauth的Google Sheets API写入Google Sheet吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Google Sheets API读取和更新Google Spreadsheet中的值的应用程序.我可以使用开发人员密钥进行读取,但是尝试写入会返回此错误:

I am working on an app that reads and updates values in a Google Spreadsheet using Google Sheets API. I am able to read using my developer key, however attempting to write returns this error:

请求缺少必需的身份验证凭据.预期的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据."

阅读(效果很好):

$client = new Google_Client();
$client->setApplicationName("XXX");
$client->setDeveloperKey("XXX");
$service = new Google_Service_Sheets($client);
$spreadsheetId = "XXX";
$range = 'promocodes';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();

写代码(错误):

$client = new Google_Client();
$client->setApplicationName("XXX");
$client->setDeveloperKey("XXX");
$service = new Google_Service_Sheets($client);
$spreadsheetId = "XXX";
$range = 'promocodes!C4';
$values = [1];
$body = new Google_Service_Sheets_ValueRange([
  'values' => $values
]);

$params = [
  'valueInputOption' => $valueInputOption
];
$result = $service->spreadsheets_values->update($spreadsheetId, $range,
    $body, $params);
printf("Cells updated.", $result->getUpdatedCells());

推荐答案

据我了解,Google API将允许您在没有访问令牌的情况下进行读取(使用开发人员密钥获取凭据),但是您无法更新或添加信息不使用oauth2身份验证方法,该方法涉及向Google发送凭据,从凭据接收代码,使用该代码获取访问令牌,然后使用该访问令牌作为凭据来添加或更新信息.

As I understand it, the Google API will allow you to read without an access token (using a developer key for credentials) however you can not update or add information without an oauth2 authentication method which involves sending credentials to google, receiving back a code from them, using that code to get an access token, then using that access token as your credentials to add or update information.

这篇关于我可以使用开发人员密钥通过不带Oauth的Google Sheets API写入Google Sheet吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆