API密钥并发现Google床单功能 [英] API key and discovered Google sheets functions

查看:240
本文介绍了API密钥并发现Google床单功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题涉及使用Python写入Google工作表时使用API​​密钥进行身份验证。遵循入门的示例,我使用发现API URL:

  service = discovery.build('sheets','v4',discoveryServiceUrl = 
'https:/ /sheets.googleapis.com/$discovery/rest?version=v4',
developerKey = APIKEY)

以这种方式传递API密钥似乎可行(它不会引发异常)。之后,我尝试使用 batchUpdate 更新电子表格:

  service.spreadsheets()。batchUpdate(spreadsheetId = SHEETID,
body = {'requests':requests})。execute()

这会引发401 HTTP错误:请求缺少必需的身份验证凭据。其他函数像 values()。clear()会导致相同的错误。



如何传递API发现Python函数的关键?

解决方案

我认为你的服务是正确。问题在于,使用API​​密钥不能使用用于使用POST和PUT方法的Sheets API。为了使用它们,请使用OAuth2访问令牌。我通过我的经历了解了这一点。尽管当时我已经查找了关于此的文档,但我一直无法找到它。对不起。

另一方面,您可以使用Sheets API进行使用API​​密钥的GET方法。例如,您可以通过以下示例使用API​​ key检索值。为了使用它,请分享电子表格。当电子表格不共享时,返回调用者没有权限。如果未使用API​​密钥,则返回请求缺少有效的API密钥。这表明API密钥是必需的。

  service = discovery.build('sheets','v4',discoveryServiceUrl = 
'https://sheets.googleapis.com/$discovery/rest?version=v4',
developerKey = APIKEY)
res = service.spreadsheets()。values()。get(spreadsheetId = SHEETID,range ='sheet1!A1:A1')。execute()

如果这不是对你有用,我误解你的问题,对不起。


This question is about authenticating using an API key when using Python to write to a Google sheet. Following the Getting Started example, I'm "discovering" the API using an URL:

service = discovery.build('sheets', 'v4', discoveryServiceUrl=
    'https://sheets.googleapis.com/$discovery/rest?version=v4',
    developerKey=APIKEY) 

Passing an API key this way seems to work (it throws no exception.) After that, I try to update the spreadsheet with batchUpdate:

service.spreadsheets().batchUpdate(spreadsheetId=SHEETID,
    body={'requests': requests}).execute()

This throws a 401 HTTP error: "Request is missing required authentication credential." Other functions like values().clear() result in the same error.

How can I pass an API key to discovered Python functions?

解决方案

I think that your service is correct. The problem is that Sheets API for using POST and PUT methods cannot be used using API key. In order to use them, please use access token by OAuth2. I knew this by my experiences. Although I had looked for the document about this at that time, I had not been able to find it. I'm sorry.

On the other hand, you can use Sheets API for GET method using API key. For example, you can retrieve values using API key by the following sample. In order to use this, please share the Spreadsheet. When the Spreadsheet is not shared, "The caller does not have permission" is returned. If API key is not used for this, "The request is missing a valid API key" is returned. This indicates that API key is required.

service = discovery.build('sheets', 'v4', discoveryServiceUrl=
    'https://sheets.googleapis.com/$discovery/rest?version=v4',
    developerKey=APIKEY)
res = service.spreadsheets().values().get(spreadsheetId=SHEETID, range='sheet1!A1:A1').execute()

If this is not useful for you and I misunderstand your question, I'm sorry.

这篇关于API密钥并发现Google床单功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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