A
A
Alexander Lazarenko2022-04-07 11:45:42
Google Apps Script
Alexander Lazarenko, 2022-04-07 11:45:42

Why is CDEK issuing v2_bad_request?

I wrote the following script to make the calculator work:

function logInCDEK() {
  var url = "https://api.edu.cdek.ru/v2/oauth/token"
  let options = {
    method: "POST",
    muteHttpExceptions: true,
    headers: {
      "Content-type": "application/json",
      "Accept": "application/json"
    }
  };

  let queryParams = {
      grant_type : "client_credentials",
      client_id : "EMscd6r9JnFiQ3bLoyjJY6eM78JrJceI",
      client_secret : "PjLZkKBHEiLK3YsjtNrt3TGNG0ahs3kG",
  };

  url+=Object.keys(queryParams).length?`?${getQueryString(queryParams)}`:"";

  let response = UrlFetchApp.fetch(url, options);
  return JSON.parse(response.getContentText());
}

/**
 * Преобразует object в query-строку для подстановки в url
 *
 * @author Boew Grigory ([email protected])
 * @param {Object} data Объект для преобразования
 * @return Возвращает query строку
 */
function getQueryString(payload) {
  let payloadData = Object.entries(payload);
  return encodeURI(
    payloadData
      .filter(o=>o[1]!==undefined)
      .map(v=>`${v[0]}=${v[1]}`)
      .join("&")
    );
};

function getCDEK(pointA, pointB, userId) {
  var token = logInCDEK().access_token;
  var url = "https://api.edu.cdek.ru/v2/calculator/tarifflist";

  var options = {
    method: "POST",
    muteHttpExceptions: true,
    headers: {
      "Content-type": "application/json",
      "Accept": "application/json",
      "Authorization": "Bearer " + token,
    },
    "type": 1,
    "currency": 1,
    "lang": "rus",
    "from_location": {
        "code": 270
    },
    "to_location": {
        "code": 44
    },
    "packages": [
        {
            "height": 10,
            "length": 10,
            "weight": 4000,
            "width": 10
        }
    ]
  }

  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response)
  return response;
}


I would be grateful if someone could help me

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2022-04-07
@InFiNiTy2707

You have the same error.
All these parameters must be passed somehow. Most likely like this:

function getCDEK(pointA, pointB, userId) {
  var token = logInCDEK().access_token;
  var url = "https://api.edu.cdek.ru/v2/calculator/tarifflist";

let payload = {
    "type": 1,
    "currency": 1,
    "lang": "rus",
    "from_location": {
        "code": 270
    },
    "to_location": {
        "code": 44
    },
    "packages": [
        {
            "height": 10,
            "length": 10,
            "weight": 4000,
            "width": 10
        }
    ]
};

  var options = {
    method: "POST",
    muteHttpExceptions: true,
    headers: {
      "Content-type": "application/json",
      "Accept": "application/json",
      "Authorization": "Bearer " + token,
    },
    payload : payload, // или JSON.strigify(payload), проверяйте
  }

  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response)
  return response;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question