简体   繁体   English

将 Google Sheet 的 v4 JSON Endpoint 与我的 API 一起使用,但仍然收到“未授权”消息

[英]Using Google Sheet's v4 JSON Endpoint with my API but still getting "Unauthorized" message

I am trying to get a simple JSON package from putting the URL of my Google spreadsheet so I can use it on my webpage;我试图通过放置我的 Google 电子表格的 URL 来获取一个简单的 JSON 包,以便我可以在我的网页上使用它; I am using my Google Cloud Console API as it is said on the documentation of the v4 Google API format, but I still get the error of "Unauthorized API"我正在使用我的 Google Cloud Console API,正如 v4 Google API 格式的文档中所说,但我仍然收到“未经授权的 API”错误

Documentation: https://developers.google.com/sheets/api/guides/migration#v4-api文档: https : //developers.google.com/sheets/api/guides/migration#v4-api

I am using this URL: https://sheets.googleapis.com/v4/spreadsheets/SHEET_ID/values/Sheet1?key=API_KEY我正在使用这个网址: https : //sheets.googleapis.com/v4/spreadsheets/SHEET_ID/values/Sheet1?key=API_KEY

My google sheet is set as published on the web.我的谷歌表设置为在网络上发布。 And also I am the creator of the google sheet.我也是谷歌表格的创建者。

What could I be missing?我可能会错过什么? I am new to API's!我是 API 的新手!

信息

First Edit: Answering the comment of ABDULLOKH MUKHAMMADJONOV Here is the code I am using to make a GET request to the google sheet, you can see the Sheet ID is there, and also the API of the google cloud platform.第一次编辑:回答ABDULLOKH MUKHAMMADJONOV的评论这里是我用来向谷歌表发出GET请求的代码,你可以看到表ID在那里,还有谷歌云平台的API。

fetch("https://sheets.googleapis.com/v4/spreadsheets/1S652uS2FLVoZ1m3apb6R4H783v6GkV58HbQ6Idec5aY/values/Sheet1?key=AIzaSyCpFZ7mcqMNc6Q_bP6h1kCEfAi6c_fd8AM", {"method": "get"})
.then( (httpResponse) => {
    if (httpResponse.ok) {
    console.log(httpResponse.json());
    return httpResponse.json();
    } else {
    return Promise.reject("Fetch did not succeed");
    }
} )
.then(json => console.log(json.someKey))
.catch(err => console.log(err));

This code is from the Wix code editor.此代码来自 Wix 代码编辑器。

The caller doesnt not have permissions调用者没有权限

Means that the user you are authenticated as does not have permission to do what it is you are trying to do.意味着您通过身份验证的用户无权执行您正在尝试执行的操作。

The method方法

https://sheets.googleapis.com/v4/spreadsheets/SHEET_ID/values/Sheet1?key=API_KEY

I believe is this method spreadsheets.values/get if you check the documentation you will notice that it requires authorization with one of these scopes我相信这个方法是电子表格.values/get如果您检查文档,您会注意到它需要对这些范围之一进行授权

在此处输入图片说明

You appear to only be sending an api key.您似乎只发送了一个 api 密钥。 You need to be authorized to access that sheet.您需要获得授权才能访问该工作表。 You cant access it with an API key that only grants access to public data.您无法使用仅授予对公共数据访问权限的 API 密钥访问它。

Ok, so I investigated about the OAuth 2.0 authentication and authorization for using Google Sheet's REST API.好的,所以我调查了使用 Google Sheet 的 REST API 的 OAuth 2.0 身份验证和授权。 I added one of the needed scopes.我添加了所需的范围之一。

But I am stuck at the point on how to do the authorization process...但是我在如何进行授权过程方面陷入困境......

I am looking at this google documentation - How to use Google OAuth 2.0 authorization我正在查看这个 google 文档 - 如何使用 Google OAuth 2.0 授权

But I haven't been able to get to the answer I seek.但我一直无法得到我寻求的答案。 I am stuck at the part where it says "When your application needs access to user data, it asks Google for a particular scope of access."我被困在它说“当您的应用程序需要访问用户数据时,它会要求谷歌提供特定的访问范围”的部分。 I do now know how to code this request or to do this request through the Insomnia software.我现在知道如何编码此请求或通过 Insomnia 软件执行此请求。

I am trying to do the GET request with a wix testing website, and also with Insomnia, but I haven't been able to achieve it.我正在尝试使用 wix 测试网站以及 Insomnia 执行 GET 请求,但我无法实现。

These are the images from Insomnia, which tell me "Invalid authorization URL"这些是来自 Insomnia 的图片,它告诉我“授权 URL 无效”

Insomnia's OAuth 2.0 authentication parameters Insomnia 的 OAuth 2.0 认证参数

Insomnia's API Query parameter Insomnia 的 API Query 参数

Thanks for all the responses so far感谢到目前为止的所有回复

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM