Firebase Cloud Function -> Google Books API 的 403 状态

Posted

技术标签:

【中文标题】Firebase Cloud Function -> Google Books API 的 403 状态【英文标题】:403 status from Firebase Cloud Function -> Google Books API 【发布时间】:2019-01-18 12:33:33 【问题描述】:

我目前正在创建一个 Firebase 云函数,该函数从 NYTimes API 检索书籍列表并从 Google Books API 获取一些额外信息。通过使用以下 URL:“https://www.googleapis.com/books/v1/volumes?q=isbn:&key=" 我可以根据 ISBN 检索图书的详细信息。

我对 Google Books API 的免费配额是每天 1000 个请求,每个用户每 100 秒 100 个请求。我只发送了 15 个请求,我当前的配额低于 200。

我的密钥仅限于与 Google Books API 一起使用,并且可以在任何地方(ios、网络、android...等)使用

构建函数后,我在本地测试所有内容,并且数据被正确填充,尽管一旦我在 firebase 上部署该函数,我在尝试访问 API 时收到 403 状态代码。

很遗憾,我没有获得有关该错误的更多详细信息。就是这样:

 Error: Request failed with status code 403
    at createError (/user_code/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/user_code/node_modules/axios/lib/core/settle.js:18:12)
    at IncomingMessage.handleStreamEnd (/user_code/node_modules/axios/lib/adapters/http.js:201:11)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)
  config: 
    adapter: [Function: httpAdapter],
     transformRequest:  '0': [Function: transformRequest] ,
     transformResponse:  '0': [Function: transformResponse] ,
     timeout: 0,
     xsrfCookieName: 'XSRF-TOKEN',
     xsrfHeaderName: 'X-XSRF-TOKEN',
     maxContentLength: -1,
     validateStatus: [Function: validateStatus],
     headers: 
       Accept: 'application/json, text/plain, */*',
        'User-Agent': 'axios/0.18.0' ,
     method: 'get',
     url: 'https://www.googleapis.com/books/v1/volumes?q=isbn:0525520384&key=##########’,
     data: undefined ,
  request: 
   ClientRequest 
     domain: null,
     _events: 
       socket: [Function],
        abort: [Function],
        aborted: [Function],
        error: [Function],
        timeout: [Function],
        prefinish: [Function: requestOnPrefinish] ,
     _eventsCount: 6,
     _maxListeners: undefined,
     output: [],
     outputEncodings: [],
     outputCallbacks: [],
     outputSize: 0,
     writable: true,
     _last: true,
     upgrading: false,
     chunkedEncoding: false,
     shouldKeepAlive: false,
     useChunkedEncodingByDefault: false,
     sendDate: false,
     _removedHeader: ,
     _contentLength: 0,
     _hasBody: true,
     _trailer: '',
     finished: true,
     _headerSent: true,
     socket: 
      TLSSocket 
        _tlsOptions: [Object],
        _secureEstablished: true,
        _securePending: false,
        _newSessionPending: false,
        _controlReleased: true,
        _SNICallback: null,
        servername: null,
        npnProtocol: false,
        alpnProtocol: false,
        authorized: true,
        authorizationError: null,
        encrypted: true,
        _events: [Object],
        _eventsCount: 10,
        connecting: false,
        _hadError: false,
        _handle: null,
        _parent: null,
        _host: 'www.googleapis.com',
        _readableState: [Object],
        readable: false,
        domain: null,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: false,
        destroyed: true,
        _bytesDispatched: 210,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: undefined,
        _server: null,
        ssl: null,
        _requestCert: true,
        _rejectUnauthorized: true,
        parser: null,
        _httpMessage: [Circular],
        read: [Function],
        _consuming: true,
        write: [Function: writeAfterFIN],
        _idleNext: null,
        _idlePrev: null,
        _idleTimeout: -1 ,
     connection: 
      TLSSocket 
        _tlsOptions: [Object],
        _secureEstablished: true,
        _securePending: false,
        _newSessionPending: false,
        _controlReleased: true,
        _SNICallback: null,
        servername: null,
        npnProtocol: false,
        alpnProtocol: false,
        authorized: true,
        authorizationError: null,
        encrypted: true,
        _events: [Object],
        _eventsCount: 10,
        connecting: false,
        _hadError: false,
        _handle: null,
        _parent: null,
        _host: 'www.googleapis.com',
        _readableState: [Object],
        readable: false,
        domain: null,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: false,
        destroyed: true,
        _bytesDispatched: 210,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: undefined,
        _server: null,
        ssl: null,
        _requestCert: true,
        _rejectUnauthorized: true,
        parser: null,
        _httpMessage: [Circular],
        read: [Function],
        _consuming: true,
        write: [Function: writeAfterFIN],
        _idleNext: null,
        _idlePrev: null,
        _idleTimeout: -1 ,
     _header: 'GET /books/v1/volumes?q=isbn:0525520384&key=##### HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nUser-Agent: axios/0.18.0\r\nHost: www.googleapis.com\r\nConnection: close\r\n\r\n',
     _headers: 
       accept: 'application/json, text/plain, */*',
        'user-agent': 'axios/0.18.0',
        host: 'www.googleapis.com' ,
     _headerNames:  accept: 'Accept', 'user-agent': 'User-Agent', host: 'Host' ,
     _onPendingData: null,
     agent: 
      Agent 
        domai

【问题讨论】:

错误可能是禁止访问该端点,可能是由于错误的 apikey 【参考方案1】:

我遇到了类似的问题。当我在本地运行我的函数时,我能够成功使用 Books API(即firebase serve)。但是当我将函数和应用程序部署到托管时,我收到了一个神秘的403: Forbidden 错误。

我尝试了几件事,所以我不确定哪个有帮助,所以我只列出所有内容:

更新了 Firebase 函数:npm install --save firebase-functions@latest 从使用node-fetch 切换到使用axios 调用API 将国家/地区添加到 URL:https://www.googleapis.com/books/v1/volumes?country=US&q=insubject:$randomWord&key=$functions.config().books.api.key 重新生成的 Google Books API 密钥 删除了谷歌云控制台中的所有Application restrictions

希望这对那里的人有所帮助,因为我已经为此苦苦挣扎了好几个小时!

【讨论】:

有没有办法将api键限制为只有http云功能?我不想删除所有应用程序限制 哇,谢谢。这是国家参数。在本地调用 API 工作正常,但从函数调用给了我一个 403。当我添加我似乎无法找到文档的国家参数时才开始在函数中工作。【参考方案2】:

通过向密钥添加更多限制(仅限 HTTP)并在我的请求中添加引用标头,最后在请求末尾添加国家代码,我的原始问题现在消失了。

注意:Referer 应该与你的 key 上的 http 限制值匹配。

最终网址:

https://www.googleapis.com/books/v1/volumes?q=isbn:#book-isbn#&key=#app-key#&country=US

【讨论】:

【参考方案3】:

Google 图书 api 需要知道国家/地区才能显示相关图书。或者通过 country 参数传递,也可以使用客户端直接调用 api。

【讨论】:

这应该是一个问题而不是一个答案。

以上是关于Firebase Cloud Function -> Google Books API 的 403 状态的主要内容,如果未能解决你的问题,请参考以下文章

保护 Firebase Cloud Function HTTP 端点

是否可以检索 Firebase Cloud Function 源代码?

在 Cloud Function for Firebase 中设置 Firebase 消息的优先级

Firebase Cloud Function 部署 tslint 错误

Firebase 中的 Function Cloud 是不是免费(Cloud Functions 部署需要按需付费 (Blaze) 计费方案)

如何直接从您的 Angular 项目中调用 Firebase Cloud Function