[英]How to write to a sheet with Google Sheets API without oAuth consent screen
I'm writing a script in PHP to automate the process of grabbing data from a Database and inserting it into a google sheet.我正在 PHP 中编写一个脚本,以自动化从数据库中获取数据并将其插入谷歌表格的过程。 Since it's going to be running at 15 minute intervals and Oauth consent screen isn't really the best option.由于它将以 15 分钟的间隔运行,并且 Oauth 同意屏幕并不是最好的选择。 Is there another option to get the necessary authorization to write to the sheet without the need of signing in via browser?是否有另一种选择来获得必要的授权来写入工作表而无需通过浏览器登录?
Most of the examples I've seen in the documentation and online use a consent screen.我在文档和在线看到的大多数示例都使用同意屏幕。 If I try this:如果我试试这个:
$client = new Google_Client();
$service = new Google_Service_Sheets($client);
Which would work with the Text-to-Speech API I get this (naturally):这将与文本到语音转换 API 我得到这个(自然):
> PHP Fatal error: Uncaught Google\Service\Exception: { "error": {
> "code": 401,
> "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid
> authentication credential. See
> https://developers.google.com/identity/sign-in/web/devconsole-project.",
> "errors": [
> {
> "message": "Login Required.",
> "domain": "global",
> "reason": "required",
> "location": "Authorization",
> "locationType": "header"
> }
> ],
> "status": "UNAUTHENTICATED" } }
The link in the error message just guides you through making a consent screen.错误消息中的链接只是引导您完成同意屏幕。 From what I could find online if i were to only be reading a file there wouldn't be a problem, but writing to it, even if i set the sheet to public, requires authentication.从我在网上可以找到的内容来看,如果我只读取一个文件不会有问题,但是写入它,即使我将工作表设置为公开,也需要身份验证。
Note: This is all running in a Gcloud VM instance注意:这一切都在 Gcloud VM 实例中运行
You can use a service account and then share the sheet with that service account.您可以使用服务帐户,然后与该服务帐户共享工作表。
This should give you a key to use.这应该给你一个使用的钥匙。 This is your client_secret.json.这是您的 client_secret.json。 Use利用
$client->setAuthConfig('./client_secret.json')
or $client->setAuthConfigFile('./client_secret.json')
$client->setAuthConfig('./client_secret.json')
或$client->setAuthConfigFile('./client_secret.json')
depending on the version of the php google api client you are using.取决于您使用的 php google api 客户端的版本。
Also, make sure you set the appropriate scope using此外,请确保使用
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.