Google Sheets API v4收到公共提要的HTTP 401响应 [英] Google Sheets API v4 receives HTTP 401 responses for public feeds

查看:104
本文介绍了Google Sheets API v4收到公共提要的HTTP 401响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在公开(即发布到网络"并与网络上的任何人"共享)电子表格中运行时,我很幸运无法从v4的Google Sheets API获得响应.

I'm having no luck getting a response from v4 of the Google Sheets API when running against a public (i.e. "Published To The Web" AND shared with "Anyone On The Web") spreadsheet.

相关文档指出:

如果请求不需要授权(例如, 例如公开数据请求 ),则应用程序必须提供API密钥或OAuth 2.0令牌或两者兼而有之—无论哪种选择对您来说都是最方便的."

"If the request doesn't require authorization (such as a request for public data), then the application must provide either the API key or an OAuth 2.0 token, or both—whatever option is most convenient for you."

要提供API密钥,文档应说明:

And to provide the API key, the documentation states:

拥有API密钥后,您的应用程序可以将查询参数key = yourAPIKey附加到所有请求URL."

"After you have an API key, your application can append the query parameter key=yourAPIKey to all request URLs."

因此,我应该能够通过以下网址获得公开电子表格中列出工作表的回复:

So, I should be able to get a response listing the sheets in a public spreadsheet at the following URL:

https://sheets.googleapis.com/v4/spreadsheets/ {spreadsheetId}?key = {myAPIkey }

https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}?key={myAPIkey}

(显然,分别在路径和查询字符串中提供了id和key)

(with, obviously, the id and key supplied in the path and query string respectively)

但是,当我这样做时,会收到HTTP 401响应:

However, when I do this, I get an HTTP 401 response:

{
  error: {
    code: 401,
    message: "The request does not have valid authentication credentials.",
    status: "UNAUTHENTICATED"
  }
}

其他任何人都可以将其与公共工作簿一起使用吗?如果不是,那么从Google端监视此线程的任何人都可以发表评论或提供有效示例吗?

Can anyone else get this to work against a public workbook? If not, can anyone monitoring this thread from the Google side either comment or provide a working sample?

推荐答案

我设法使它正常工作.一开始我什至感到沮丧.而且,这不是错误.这是我的操作方式:

I managed to get this working. Even I was frustrated at first. And, this is not a bug. Here's how I did it:

  1. 首先,在您的GDC中启用这些功能以摆脱身份验证错误.

-Google Apps脚本执行API

-Google Apps Script Execution API

-Google Sheets API

-Google Sheets API

注意:请确保您在GDC中使用的Google帐户必须与在电子表格项目中使用的帐户相同,否则您可能会收到"The API Key and the authentication credential are from different projects"错误消息.

Note: Make sure the Google account you used in GDC must be the same account you're using in Spreadsheet project else you might get a "The API Key and the authentication credential are from different projects" error message.

  1. 转到​​ https://developers.google.com/oauthplayground ,您将在其中获取授权令牌.
  2. 在第1步中,选择 Google Sheets API v4 ,然后选择https://www.googleapis.com/auth/spreadsheets作用域,以便您具有漫游器读写权限.
  3. 单击授权API"按钮.允许身份验证,然后继续执行步骤2.
  4. 在第2步中,点击交换令牌的授权代码按钮.之后,请继续执行步骤3.
  5. 在第3步中,是时候粘贴您的URL请求了.由于默认服务器方法是GET,请继续,然后单击发送请求按钮.
  1. Go to https://developers.google.com/oauthplayground where you will acquire authorization tokens.
  2. On Step 1, choose Google Sheets API v4 and choose https://www.googleapis.com/auth/spreadsheets scope so you have bot read and write permissions.
  3. Click the Authorize APIs button. Allow the authentication and you'll proceed to Step 2.
  4. On Step 2, click Exchange authorization code for tokens button. After that, proceed to Step 3.
  5. On Step 3, time to paste your URL request. Since default server method is GET proceed and click Send the request button.

注意:确保您的URL请求是 Spreadsheetv4文档.

Note: Make sure your URL requests are the ones indicated in the Spreadsheetv4 docs.

这是我的示例网址请求:

Here's my sample URL request:

https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID?includeGridData=false

我得到了HTTP/1.1 200 OK,它显示了我请求的数据.这适用于所有Spreadsheetv4服务器端进程.

I got a HTTP/1.1 200 OK and it displayed my requested data. This goes for all Spreadsheetv4 server-side processes.

希望这会有所帮助.

这篇关于Google Sheets API v4收到公共提要的HTTP 401响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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