Google表格API v4接收公共Feed的HTTP 401响应

问题描述 投票:14回答:4

我在运行针对公众(即“发布到网络”并与“网络上的任何人”共享)电子表格时,从Google表格API的v4获得回复是没有运气的。

相关文件说明:

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

并提供API密钥,文档说明:

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

因此,我应该能够在以下网址的公共电子表格中找到列出工作表的回复:

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

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

但是,当我这样做时,我得到一个HTTP 401响应:

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

任何人都可以让这个对公共工作簿起作用吗?如果没有,那么从谷歌那边监控这个主题的人是否可以评论或提供工作样本?

google-sheets-api
4个回答
11
投票

我设法让这个工作。即使我一开始感到沮丧。而且,这不是一个错误。我是这样做的:

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

-Google Apps脚本执行API

-Google Sheets API

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

  1. https://developers.google.com/oauthplayground,你将获得授权令牌。
  2. 在第1步,选择Google表格API v4并选择https://www.googleapis.com/auth/spreadsheets范围,这样您就拥有了机器人读写权限。
  3. 单击“授权API”按钮。允许身份验证,然后您将继续执行第2步。
  4. 在步骤2中,单击“令牌的Exchange授权代码”按钮。之后,继续执行步骤3。
  5. 在第3步,粘贴您的网址请求的时间。由于默认服务器方法是GET继续并单击发送请求按钮。

注意:确保您的网址请求是Spreadsheetv4 docs中指明的网址。

这是我的示例网址请求:

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

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

希望这可以帮助。


6
投票

我们最近解决了这个问题,它现在应该可行了。对不起,请再试一次。

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


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选项卡。

执行应用程序:Me [email protected]

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

然后测试你可以使用Postman应用程序。 enter image description here

或者使用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是必需的。

结果:enter image description here


0
投票

如果您的Feed是公共的并且您正在使用api密钥,请确保您正在抛出http GET请求。如果是POST请求,您将收到此错误。我面对同样的。使用方法获取数据:spreadsheets.getByDataFilter具有POST请求

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.