Google Sheet Update Api:出现错误[请求缺少所需的身份验证凭据] [英] Google Sheet Update Api : Getting error [Request is missing required authentication credential]

查看:51
本文介绍了Google Sheet Update Api:出现错误[请求缺少所需的身份验证凭据]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下参数在 Postman 中调用 Google Sheet Update Request:

网址:

得到回应:

注意:为了更新到电子表格,我按照以下步骤:

  1. 使用此链接在 Google Api 控制台上创建应用程序,然后转到凭据部分.单击创建凭据,然后单击 OAuth 客户端 ID,将应用程序类型创建为 Web 应用程序.现在离开限制部分.然后创建.现在应用程序已创建.

  2. OAuth 同意屏幕 标签中添加一些项目名称

  3. 现在,使用此链接授权 API.在右上角,打开设置图标.选择使用您自己的 OAuth 凭据为真.从 Google Api 控制台上的项目添加客户端 ID 和客户端密钥.选择范围为 https://www.googleapis.com/auth/spreadsheets 在下拉列表中的 Google Sheet Api v4 下,然后授权 API.

如果获取redirect_mismatch_uri.然后将重定向 URI 复制到 oathPlayground 或 prompt 参数之前,然后将其粘贴到 Google Api Console 中项目的限制部分下的授权重定向 URI 中.然后会生成Access Token,

  1. 获取访问令牌.在我之前定义的请求中复制此访问令牌.

但是出现错误.我无法弄清楚问题所在.提前致谢..

解决方案

由于您已经拥有刷新令牌,因此可以使用刷新令牌检索访问令牌.作为示例,它展示了如何通过 curl 使用刷新令牌检索访问令牌的方法.刷新令牌检索到的访问令牌具有过期时间.请确认.

您可以在此处查看详细信息.https://developers.google.com/identity/protocols/OAuth2WebServer

卷曲命令:

curl -L \--data "refresh_token=### 刷新令牌###" \--data "client_id=### 客户端 ID ###" \--data "client_secret=### 客户端密码 ###" \--data "grant_type=refresh_token" \https://www.googleapis.com/oauth2/v4/token

结果:

<代码>{"access_token": "### 访问令牌 ###","token_type": "承载",expires_in":3600}

下面的 curl 命令用于检索访问令牌的信息.如果访问令牌的过期时间已过,则响应为 {"error_description": "Invalid Value"}.如果不是,您可以看到以下回复.

获取访问令牌信息:

curl -L \--data "access_token=### 访问令牌###" \https://www.googleapis.com/oauth2/v2/tokeninfo

结果:

<代码>{"issued_to": "#####",观众": "#####",范围": "#####",expires_in":1234,访问类型":离线"}

编辑 1

此示例代码用于将 sheet1!A1:C2 的范围更新为 [[["test1","test2","test3"],["test4","test5","test6"]].

示例代码:

curl -L -X PUT \-H "授权:不记名### 访问令牌###" \-H "内容类型:应用程序/json" \-d '{"values":[["test1","test2","test3"],["test4","test5","test6"]]}' \https://sheets.googleapis.com/v4/spreadsheets/### 电子表格 ID ###/values/sheet1%21a1%3ac2?valueInputOption=USER_ENTERED"

结果:

<代码>{"spreadsheetId": "### 电子表格 ID ###","updatedRange": "sheet1!A1:C2",更新行":2,更新列":3,更新单元格":6}

I am calling Google Sheet Update Request in Postman with following parameters:

Url:https://sheets.googleapis.com/v4/spreadsheets/SpreadSheetId/values/SheetName!A1:D5 Request Type : PUT
Headers : Authorization - MyAccessToken
Request Body :

Getting Response :

Note : For Updating into Spreadsheet, I follow these steps :

  1. Create an application on Google Api Console using this link and then move to Credentials section. Click Create Credentials, then OAuth Client ID, Create Application type as Web Application. For now leave the Restrictions Section. Then Create. Now Application is created.

  2. Add some project Name in the OAuth Consent Screen Tab

  3. Now, Authorize the API using this link. In the right corner, open the Settings Icon. Select Use your own OAuth Credentials as true. Add the Client Id and Client secret from the Project on Google Api Console. Select the Scope as https://www.googleapis.com/auth/spreadsheets under the Google Sheet Api v4 in the dropdown, then Authorize the API.

If Get redirect_mismatch_uri. then Copy the redirect URI till the oathPlayground or before the prompt parameter and paste it into the Authorized Redirect URI under the restrictions Section of project in Google Api Console. Then Access Token will be generated,

  1. Get Access Token. Copy this access token in the request which I defined earlier.

But Getting error. I am not able to figure out the problem. Thanks in Advance..

解决方案

Since you already have a refresh token, you can retrieve access token using refresh token. As a sample, it shows a method of how to retrieve access token using refresh token by curl. The access token retrieved by refresh token has the expiration time. Please confirm it.

You can see the detail infomation here. https://developers.google.com/identity/protocols/OAuth2WebServer

Curl command :

curl -L \
  --data "refresh_token=### refresh token ###" \
  --data "client_id=### client id ###" \
  --data "client_secret=### client secret ###" \
  --data "grant_type=refresh_token" \
  https://www.googleapis.com/oauth2/v4/token

Result :

{
 "access_token": "### access token ###",
 "token_type": "Bearer",
 "expires_in": 3600
}

Below curl command is for retrieving the information of access token. If the expiration time of access token had been over, the response is {"error_description": "Invalid Value"}. If it's not, you can see a following response.

Retrieve information of access token :

curl -L \
 --data "access_token=### access token ###" \
https://www.googleapis.com/oauth2/v2/tokeninfo

Result :

{
 "issued_to": "#####",
 "audience": "#####",
 "scope": "#####",
 "expires_in": 1234,
 "access_type": "offline"
}

EDIT1

This sample code is for updating the range of sheet1!A1:C2 to [["test1","test2","test3"],["test4","test5","test6"]].

Sample code :

curl -L -X PUT \
-H "Authorization: Bearer ### access token ###" \
-H "Content-Type: application/json" \
-d '{"values":[["test1","test2","test3"],["test4","test5","test6"]]}' \
"https://sheets.googleapis.com/v4/spreadsheets/### spreadsheet ID ###/values/sheet1%21a1%3ac2?valueInputOption=USER_ENTERED"

Result :

{
  "spreadsheetId": "### spreadsheet ID ###",
  "updatedRange": "sheet1!A1:C2",
  "updatedRows": 2,
  "updatedColumns": 3,
  "updatedCells": 6
}

这篇关于Google Sheet Update Api:出现错误[请求缺少所需的身份验证凭据]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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