【问题标题】:UrlFetchApp works in script editor, not in documentUrlFetchApp 在脚本编辑器中工作,而不是在文档中
【发布时间】:2016-08-05 18:42:17
【问题描述】:

我有一个用于表格的 Google Apps 脚本,它向 Google Books API 发出请求以按标题/作者查找 ISBN,并且在脚本编辑器中进行测试时请求可靠地工作,但是当我使用相同的数据对其进行测试时在电子表格本身中,它会因以下错误而失败:

Request failed for https://www.googleapis.com/books/v1/volumes?q=Deedy%2C%20Carmen%20Agra%2014%20Cows%20for%20America returned code 403. Truncated server response: { "error": { "errors": [ { "domain": "global", "reason": "unknownLocation",
 "message": "Cannot determine user location for geogra... (use muteHttpExceptions option to examine full response) (line 55).

这是在脚本编辑器中使用相同数据成功运行的相同函数的执行记录:

[16-08-05 14:39:09:772 EDT] Starting execution
[16-08-05 14:39:10:558 EDT] UrlFetchApp.fetch([https://www.googleapis.com/books/v1/volumes?q=Deedy%2C%20Carmen%20Agra%2014%20Cows%20for%20America]) [0.778 seconds]
[16-08-05 14:39:10:559 EDT] HTTPResponse.getResponseCode() [0 seconds]
[16-08-05 14:39:10:559 EDT] HTTPResponse.getContentText() [0 seconds]
[16-08-05 14:39:10:569 EDT] Logger.log([found title match for '%s': '%s', [14 Cows for America, 14 Cows for America]]) [0 seconds]
[16-08-05 14:39:10:583 EDT] Logger.log([result: %s, [9781561454907]]) [0 seconds]
[16-08-05 14:39:10:584 EDT] Execution succeeded [0.804 seconds total runtime]

如果相关,我已将 source code 设为可用。

【问题讨论】:

    标签: google-apps-script google-sheets google-spreadsheet-api


    【解决方案1】:

    网址提取服务使用 Google 的网络基础架构 效率和扩展目的。

    并且“Google Books API 结果受限于您的服务器或客户端应用程序的 IP 地址”(doc)。由于某种原因,它无法确定运行 Google Apps 脚本的服务器位置并返回错误 Cannot determine user location for geographically restricted operation。可以试试发送参数country,改这行就好了:

    var isbnEndpoint = "https://www.googleapis.com/books/v1/volumes?q=";
    

    到这里:

    var isbnEndpoint = "https://www.googleapis.com/books/v1/volumes?country=US&q=";
    

    【讨论】:

    • 令人惊讶的是,它可以检测到开发服务器上的位置,但不能检测到实时电子表格上的位置,但是是的,这行得通,谢谢。
    猜你喜欢
    • 2022-06-16
    • 2021-10-08
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多