【问题标题】:Status code 403 ...only when request from my serveur not in local状态码 403 ...仅当来自我的服务器的请求不在本地时
【发布时间】:2020-08-15 21:22:57
【问题描述】:

您好,我正在尝试使用在线 API。 并以 JSON 格式读取文件... 我正在使用带有 AXIOS 的 nodejs。

这是代码:

const axios = require('axios');
const moment = require("moment");
const EVENTS_ENDPOINT = "https://gameinfo.albiononline.com/api/gameinfo/events";
const EVENTS_LIMIT = 51;

(
    async () =>  {

        try 
        {
            let offset = 1;
            //On récupére les 52 événements donnés par l'API Albion Online.
            const res = await axios.get(EVENTS_ENDPOINT, {
                params: {
                  offset,
                  limit: EVENTS_LIMIT,
                  timestamp: moment().unix(),
                },
                timeout: 60000,
              });
           // Sur ces 52 événements, on applique un filtre pour ne conserver que ceux en rapport
           // avec la guilde
            console.log(res);

            console.log("ok");
        }
        catch (err) {
           console.log(err)
        }



})();

从我的本地机器,使用节点 JS ...效果很好! 我可以联系 albiononline serverur ...并获取 JSON。

但是当我将文件上传到我的 OVH 服务器时...我收到此状态错误...

你有什么想法吗?

返回错误:

Error: Request failed with status code 403
    at createError (/var/www/html/discordbots/KillBoard2.0/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/var/www/html/discordbots/KillBoard2.0/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/var/www/html/discordbots/KillBoard2.0/node_modules/axios/lib/adapters/http.js:                                      236:11)
    at IncomingMessage.emit (events.js:215:7)
    at endReadableNT (_stream_readable.js:1198:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  config: {
    url: 'https://gameinfo.albiononline.com/api/gameinfo/events',
    method: 'get',
    params: { offset: 1, limit: 51, timestamp: 1597526804 },
    headers: {
      Accept: 'application/json, text/plain, */*',
      'User-Agent': 'axios/0.19.2'
    },
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 60000,
    adapter: [Function: httpAdapter],
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    validateStatus: [Function: validateStatus],
    data: undefined
  },
  request: <ref *1> ClientRequest {
    _events: [Object: null prototype] {
      socket: [Array],
      abort: [Function (anonymous)],
      aborted: [Function (anonymous)],
      error: [Function (anonymous)],
      timeout: [Array],
      prefinish: [Function: requestOnPrefinish]
    },
    _eventsCount: 6,
    _maxListeners: undefined,
    outputData: [],
    outputSize: 0,
    writable: true,
    _last: true,
    chunkedEncoding: false,
    shouldKeepAlive: false,
    useChunkedEncodingByDefault: false,
    sendDate: false,
    _removedConnection: false,
    _removedContLen: false,
    _removedTE: false,
    _contentLength: 0,
    _hasBody: true,
    _trailer: '',
    finished: true,
    _headerSent: true,
    socket: TLSSocket {
      _tlsOptions: [Object],
      _secureEstablished: true,
      _securePending: false,
      _newSessionPending: false,
      _controlReleased: true,
      _SNICallback: null,
      servername: 'gameinfo.albiononline.com',
      alpnProtocol: false,
      authorized: true,
      authorizationError: null,
      encrypted: true,
      _events: [Object: null prototype],
      _eventsCount: 9,
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: 'gameinfo.albiononline.com',
      _readableState: [ReadableState],
      readable: true,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: false,
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: undefined,
      _server: null,
      ssl: [TLSWrap],
      _requestCert: true,
      _rejectUnauthorized: true,
      parser: null,
      _httpMessage: [Circular *1],
      timeout: 60000,
      [Symbol(res)]: [TLSWrap],
      [Symbol(asyncId)]: 5,
      [Symbol(kHandle)]: [TLSWrap],
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: Timeout {
        _idleTimeout: 60000,
        _idlePrev: [TimersList],
        _idleNext: [TimersList],
        _idleStart: 159,
        _onTimeout: [Function: bound ],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        [Symbol(refed)]: false,
        [Symbol(asyncId)]: 14,
        [Symbol(triggerId)]: 12
      },
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0,
      [Symbol(connect-options)]: [Object]
    },
    _header: 'GET /api/gameinfo/events?offset=1&limit=51&timestamp=1597526804 HTTP/1.1\r\n' +
      'Accept: application/json, text/plain, */*\r\n' +
      'User-Agent: axios/0.19.2\r\n' +
      'Host: gameinfo.albiononline.com\r\n' +
      'Connection: close\r\n' +
      '\r\n',
    _onPendingData: [Function: noopPendingOutput],
    agent: Agent {
      _events: [Object: null prototype],
      _eventsCount: 1,
      _maxListeners: undefined,
      defaultPort: 443,
      protocol: 'https:',
      options: [Object],
      requests: {},
      sockets: [Object],
      freeSockets: {},
      keepAliveMsecs: 1000,
      keepAlive: false,
      maxSockets: Infinity,
      maxFreeSockets: 256,
      maxCachedSessions: 100,
      _sessionCache: [Object]
    },
    socketPath: undefined,
    method: 'GET',
    path: '/api/gameinfo/events?offset=1&limit=51&timestamp=1597526804',
    _ended: true,
    res: IncomingMessage {
      _readableState: [ReadableState],
      readable: false,
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      socket: [TLSSocket],
      httpVersionMajor: 1,
      httpVersionMinor: 1,
      httpVersion: '1.1',
      complete: true,
      headers: [Object],
      rawHeaders: [Array],
      trailers: {},
      rawTrailers: [],
      aborted: false,
      upgrade: false,
      url: '',
      method: null,
      statusCode: 403,
      statusMessage: 'Forbidden',
      client: [TLSSocket],
      _consuming: false,
      _dumped: false,
      req: [Circular *1],
      responseUrl: 'https://gameinfo.albiononline.com/api/gameinfo/events?offset=1&limit=51&timestamp=1597526804',
      redirects: []
    },
    aborted: false,
    timeoutCb: [Function: emitRequestTimeout],
    upgradeOrConnect: false,
    parser: null,
    maxHeadersCount: null,
    reusedSocket: false,
    _redirectable: Writable {
      _writableState: [WritableState],
      writable: true,
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      _options: [Object],
      _redirectCount: 0,
      _redirects: [],
      _requestBodyLength: 0,
      _requestBodyBuffers: [],
      _onNativeResponse: [Function (anonymous)],
      _currentRequest: [Circular *1],
      _currentUrl: 'https://gameinfo.albiononline.com/api/gameinfo/events?offset=1&limit=51&timestamp=1597526804'
    },
    [Symbol(kNeedDrain)]: false,
    [Symbol(isCorked)]: false,
    [Symbol(kOutHeaders)]: [Object: null prototype] {
      accept: [Array],
      'user-agent': [Array],
      host: [Array]
    }
  },
  response: {
    status: 403,
    statusText: 'Forbidden',
    headers: {
      server: 'nginx',
      date: 'Sat, 15 Aug 2020 21:26:44 GMT',
      'content-type': 'text/html',
      'content-length': '146',
      connection: 'close',
      'x-id': 'tp-up-gc4'
    },
    config: {
      url: 'https://gameinfo.albiononline.com/api/gameinfo/events',
      method: 'get',
      params: [Object],
      headers: [Object],
      transformRequest: [Array],
      transformResponse: [Array],
      timeout: 60000,
      adapter: [Function: httpAdapter],
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      validateStatus: [Function: validateStatus],
      data: undefined
    },
    request: <ref *1> ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 6,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      useChunkedEncodingByDefault: false,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: [TLSSocket],
      _header: 'GET /api/gameinfo/events?offset=1&limit=51&timestamp=1597526804 HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'User-Agent: axios/0.19.2\r\n' +
        'Host: gameinfo.albiononline.com\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _onPendingData: [Function: noopPendingOutput],
      agent: [Agent],
      socketPath: undefined,
      method: 'GET',
      path: '/api/gameinfo/events?offset=1&limit=51&timestamp=1597526804',
      _ended: true,
      res: [IncomingMessage],
      aborted: false,
      timeoutCb: [Function: emitRequestTimeout],
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      _redirectable: [Writable],
      [Symbol(kNeedDrain)]: false,
      [Symbol(isCorked)]: false,
      [Symbol(kOutHeaders)]: [Object: null prototype]
    },
    data: '<html>\r\n' +
      '<head><title>403 Forbidden</title></head>\r\n' +
      '<body>\r\n' +
      '<center><h1>403 Forbidden</h1></center>\r\n' +
      '<hr><center>nginx</center>\r\n' +
      '</body>\r\n' +
      '</html>\r\n'
  },
  isAxiosError: true,
  toJSON: [Function (anonymous)]
}

【问题讨论】:

  • 什么状态错误?
  • 你确定你在打 api,返回 "test" 以确保它工作
  • 我编辑并发布了返回的错误

标签: javascript node.js json api axios


【解决方案1】:

代码运行良好,没有任何错误。您得到的错误代码是 403。只需确保您可以从运行节点 js 代码的服务器访问 API。

【讨论】:

  • 也许我的 IP 已被 albiononline 禁止?
猜你喜欢
  • 1970-01-01
  • 2022-08-15
  • 2021-09-17
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 2016-01-18
  • 2020-04-13
  • 1970-01-01
相关资源
最近更新 更多