简体   繁体   English

如何在没有 oAuth 同意屏幕的情况下使用 Google 表格 API 写入工作表

[英]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.您可以使用服务帐户,然后与该服务帐户共享工作表。

  1. Go to console.cloud.google.com and login with the account containing the sheet Go 到 console.cloud.google.com 并使用包含工作表的帐户登录
  2. Add the Google Sheets API (go to APIs and Services, look up Google Sheets API, add it)添加 Google 表格 API(转到 API 和服务,查找 Google 表格 API,添加它)
  3. Go back to APIs and Services and click on Credentials Go 返回 API 和服务并单击凭据
  4. Click on Create credentials -> Service account单击创建凭据-> 服务帐户

凭证图片

  1. Fill in required stuff (mainly Service account name, and give it Owner role) and press done填写所需的东西(主要是服务帐户名称,并赋予其所有者角色)并按完成
  2. Click on the service account you just created单击您刚刚创建的服务帐户
  3. Go to keys tab -> Add key -> Create new key -> JSON Go 到密钥选项卡 -> 添加密钥 -> 创建新密钥 -> JSON

键选项卡

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]);
  1. Share the sheet from your drive with the created service account and give it editorial permissions与创建的服务帐户共享驱动器中的工作表并授予其编辑权限

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Google Sheets API 写入工作表 - Write to Sheet with Google Sheets API Google AnalyticsAPI - 使用“同意屏幕”获取访问令牌而无需进行身份验证 - Google Analytics API - get Access Token without authenticate with the “consent screen” 通过 oauth2(同意屏幕)从 Google Analytics Data API (Ga4) 中提取数据 - Extract data from Google Analytics Data API (Ga4) via oauth2 (consent screen) Google API:使用 OAuth 验证请求,但没有用户同意窗口,然后请求特定用户名或用户 ID 的 Google 数据 - Google API: Authenticate request with OAuth but without user consent window then request Google data for particular username or user ID Google Sheets API客户端-如何获取单个图纸数据 - Google Sheets API Client - How to get single sheet data 如何使用 Google Classroom API 密钥而不是 Auth 同意屏幕创建 class? - How to create class using Google Classroom API Keys instead of Auth Consent Screen? 使用API​​同意屏幕通过PHP将文件上传到Google云端硬盘 - Upload file via PHP to Google Drive using API no consent screen 使用 Google Sheets API 查找图纸尺寸 - Find sheet size using Google Sheets API 如何在php中使用API​​ 获取与谷歌表链接的谷歌表单。 或与谷歌表单链接的表格 - How to get google form linked with google sheet using API in php. or Sheets linked with google form 用户拒绝/取消同意屏幕时的Google OAuth 2.0重定向URL - Google OAuth 2.0 redirect URL when user refuses/cancel consent screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM