๐Ÿค” Request headers accept

1986 ๋‹จ์–ด errorerror

๐Ÿค” ๋ฌธ์ œ์ƒํ™ฉ

github์˜ accessToken์„ ์‘๋‹ต๋ฐ›๊ธฐ ์œ„ํ•ด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

  axios({
    method: 'POST',
    url: `https://github.com/login/oauth/access_token`,
    data: {
      client_id: clientID,
      client_secret: clientSecret,
      code: req.body.authorizationCode,
    },
  })
    .then((data) => {
      let accessToken = data.data.split('&').map((v) => v.split('='))[0][1];
      res.status(200).send({ accessToken });
    })
    .catch((err) => {
      //! catch๋กœ ์—๋Ÿฌ ์žก์•„์ฃผ๊ธฐ
      console.log(err);
    });

data.data์˜ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด string ํƒ€์ž…์œผ๋กœ ์‘๋‹ต์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค.

access_token=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&scope=&token_type=bearer

๋‚ด๊ฐ€ ํ•„์š”๋กœ ํ•˜๋Š” ๊ฐ’์€ access_token์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณต์žกํ•œ ์ถ”์ถœ ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•œ๋‹ค.

let accessToken = data.data.split('&').map((v) => v.split('='))[0][1];

๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•˜๋Š” ๊ฒƒ์€ ๋ฌธ์ œ์—†์ง€๋งŒ, ์ข€ ๋” ๋ณต์žกํ•œ response๋ฅผ ๋ฐ›๊ฒŒ๋˜๋ฉด ์›ํ•˜๋Š” ๊ฐ’์˜ ์ถ”์ถœ์ด ์–ด๋ ค์›Œ ๋ณด์˜€๋‹ค.


๐Ÿ˜Ž ๋ฌธ์ œ ํ•ด๊ฒฐ

   axios({
    method: 'POST',
    url: `https://github.com/login/oauth/access_token`,
    headers: {
      accept: 'application/json',
    },
    data: {
      client_id: clientID,
      client_secret: clientSecret,
      code: req.body.authorizationCode,
    },
  })
    .then((data) => {
      let accessToken = data.data.access_token;
      res.status(200).send({ accessToken });
    })
    .catch((err) => {
      //! catch๋กœ ์—๋Ÿฌ ์žก์•„์ฃผ๊ธฐ
      console.log(err);
    });

์œ„์™€ ๊ฐ™์ด ์‘๋‹ต์„ ๋ฐ›๋Š” accept ํƒ€์ž…์„ json ํ˜•์‹์œผ๋กœ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด response๋ฅผ ๋ฐ›๋Š”๋‹ค.

{
  access_token: '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',
  token_type: 'bearer',
  scope: ''
}

json ํ˜•์‹์œผ๋กœ ์‘๋‹ต์„ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”๋กœํ•˜๋Š” value๋ฅผ ์ถ”์ถœํ•˜๊ธฐ๊ฐ€ ๋”์šฑ ํŽธ๋ฆฌํ•ด์ง„๋‹ค.

let accessToken = data.data.access_token;

๊ทธ๋ƒฅ string ํƒ€์ž…์œผ๋กœ ๋ฐ›๋Š” ๊ฒƒ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ๊ทธ๋ƒฅ ํ•„์š”๋กœํ•˜๋Š” ํ‚ค๊ฐ’์œผ๋กœ ๋„์ถœํ•˜๋ฉด ๋œ๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ