Baptiste Bauer
Baptiste Bauer

Reputation: 57

Status code 403 ...only when request from my serveur not in local

Hello i am trying to use an online API . And read the file in JSON ... I am using nodejs with AXIOS.

This is the code :

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)
        }



})();

From my local machine , with node JS ... it works well !! I can contact the albiononline serveur ...and get the JSON.

But when i upload the file in my OVH server ... i receive this status error...

Do you have any idea ?

Returned error :

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)]
}

Upvotes: 0

Views: 1088

Answers (1)

ricdizio
ricdizio

Reputation: 49

The code runs well and doesn't have any error. The error code that you are getting is 403. just make sure that you have access to the API from your server where you are running the node js code.

Upvotes: 1

Related Questions