17

在针对公共(即“发布到网络”并与“网络上的任何人”共享)电子表格运行时,我没有得到来自 Google Sheets API v4 的响应。

相关文件指出:

“如果请求不需要授权(例如对公共数据的请求),那么应用程序必须提供 API 密钥或 OAuth 2.0 令牌,或者两者都提供——无论哪个选项对您来说最方便。”

为了提供 API 密钥,文档指出:

“获得 API 密钥后,您的应用程序可以将查询参数 key=yourAPIKey 附加到所有请求 URL。”

因此,我应该能够在以下 URL 的公共电子表格中获得列出工作表的响应:

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

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

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

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

其他人可以让它针对公共工作簿工作吗?如果没有,任何人都可以从谷歌方面监控这个线程或者评论或提供一个工作样本吗?

4

4 回答 4

18

我设法让这个工作。甚至一开始我也很沮丧。而且,这不是错误。我是这样做的:

  1. 首先,在您的 GDC 中启用这些以消除身份验证错误。

-Google Apps 脚本执行 API

-Google 表格 API

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

  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 继续并单击发送请求按钮

注意:确保您的 URL 请求是Spreadsheetv4 文档中指定的请求。

这是我的示例 URL 请求:

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

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

希望这可以帮助。

于 2016-05-20T09:07:56.250 回答
6

我们最近修复了这个问题,它现在应该可以工作了。很抱歉给您带来麻烦,请再试一次。

该文档必须共享给“知道链接的任何人”或“在网络上公开”。(注意:与 v3 API 不同,“文件 -> 发布到网络”中的发布设置无关紧要。)

于 2016-05-19T13:50:48.777 回答
1

这不是问题的解决方案,但我认为这是实现目标的好方法。在网站http://embedded-lab.com/blog/post-data-google-sheets-using-esp8266/上,我发现了如何使用 Google Apps 脚本更新电子表格。这是 GET 方法的示例。我将尝试向您展示 JSON 格式的 POST 方法。

如何发布:创建 Google 电子表格,在工具 > 脚本编辑器选项卡中粘贴以下脚本。通过输入适当的电子表格 ID 和工作表选项卡名称来修改脚本(脚本中的第 27 和 28 行)。

function doPost(e)
{
  var success = false;
  if (e != null)
  {
    var JSON_RawContent = e.postData.contents;
    var PersonalData = JSON.parse(JSON_RawContent);

    success = SaveData(
      PersonalData.Name, 
      PersonalData.Age, 
      PersonalData.Phone
    );
  }
  // Return plain text Output
    return ContentService.createTextOutput("Data saved: " + success);
}

function SaveData(Name, Age, Phone)
{
  try 
  {
    var dateTime = new Date();

    // Paste the URL of the Google Sheets starting from https thru /edit
    // For e.g.: https://docs.google.com/---YOUR SPREADSHEET ID---/edit 
    var MyPersonalMatrix = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/---YOUR SPREADSHEET ID---/edit");
    var MyBasicPersonalData = MyPersonalMatrix.getSheetByName("BasicPersonalData");


    // Get last edited row
    var row = MyBasicPersonalData.getLastRow() + 1;

    MyBasicPersonalData.getRange("A" + row).setValue(Name);
    MyBasicPersonalData.getRange("B" + row).setValue(Age); 
    MyBasicPersonalData.getRange("C" + row).setValue(Phone); 

    return true;
  }
  catch(error) 
  {
    return false;
  }
}

现在保存脚本并转到选项卡Publish > Deploy as Web App

执行应用程序为:我 xyz@gmail.com

谁有权访问该应用程序:任何人,甚至是匿名的

然后进行测试,您可以使用 Postman 应用程序。在此处输入图像描述

或使用 UWP:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    using (HttpClient httpClient = new HttpClient())
    {
        httpClient.BaseAddress = new Uri(@"https://script.google.com/");
        httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
        httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("utf-8"));
        string endpoint = @"/macros/s/---YOUR SCRIPT ID---/exec";

        try
        {
            PersonalData personalData = new PersonalData();
            personalData.Name = "Jarek";
            personalData.Age = "34";
            personalData.Phone = "111 222 333";

            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(personalData), Encoding.UTF8, "application/json");
            HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(endpoint, httpContent);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                string jsonResponse = await httpResponseMessage.Content.ReadAsStringAsync();
            //do something with json response here
            }
        }
        catch (Exception ex)
        {

        }
    }
}

public class PersonalData
{
    public string Name;
    public string Age;
    public string Phone;
}

以上代码 NuGet Newtonsoft.Json 是必需的。

结果: 在此处输入图像描述

于 2018-03-24T00:15:22.160 回答
0

如果您的提要是公开的并且您正在使用 api 密钥,请确保您正在抛出一个 http GET 请求。如果是 POST 请求,您将收到此错误。我也面临同样的情况。使用方法获取数据:电子表格.getByDataFilter 有 POST 请求

于 2019-02-12T17:54:29.310 回答