来自 Firebase Cloud Function 的 403 状态 -> Google 书籍 API

403 status from Firebase Cloud Function -> Google Books API

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

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

我的密钥仅限用于 Google 本书 API 并且可以在任何地方使用(iOS、网络、android...等)

构建函数后,我在本地测试所有内容并正确填充数据,尽管在 firebase 上部署该函数后,我在尝试访问 API.[=14= 时收到 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

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

注意:Referer 应与密钥上的 http 限制值相匹配。

最终 URL :

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

我遇到了类似的问题。当我在本地 运行 我的函数时(即 firebase serve),我能够成功地使用书籍 API。但是当我将函数和应用程序部署到托管时,我遇到了一个神秘的 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书籍API键
  • 删除了 google 云控制台中的所有 Application restrictions

希望这对那里的人有帮助,因为我已经花了好几个小时来解决这个问题!

Google 书籍 apis 需要了解国家才能显示相关书籍。要么通过国家参数传递它,要么您可以使用客户端直接调用 api。