์๋ ํ์ธ์, mi ๋ฌธ์ ๋ ๋ ๋ง์ ์ง๋ฌธ์ ๋๋ค. axios๊ฐ ์ด ์ปฌ์ด ์ํํ๋ ๊ฒ๊ณผ ๋์ผํ ์์ ์ ์ํํ๋ POST ์์ฒญ์ ์ํํ๋๋ก ํ๋ ค๊ณ ํฉ๋๋ค.
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
-d "grant_type=client_credentials"
๋ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
var instance = axios.create({
baseURL: 'https://api.sandbox.paypal.com/v1/oauth2/',
timeout: 1000,
method: 'post',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'content-type': 'application/x-www-form-urlencoded',
'EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp':'EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp'
},
data: {
"grant_type": "client_credentials",
},
});
instance.post('/token')
.then(function(response){
console.log(response.data); // ex.: { user: 'Your User'}
console.log(response.status); // ex.: 200
});
๋ด ๋ก์ปฌ ํธ์คํธ์์ ํธ์ถํ๊ณ ์์ผ๋ฉฐ 401 ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ด๊ฒ์ PAYPAL API https://developer.paypal.com/docs/integration/direct/make-your-first-call/์ ๋ํ ๋งํฌ์ ๋๋ค.
๋ฌธ์๋ฅผ ์ฝ์ด๋ณด๋ ์ฌ์ฉ์ ์ด๋ฆ๊ณผ ์ํธ๋ฅผ ์
๋ ฅํ ์์น ๋๋ ์๋ํ๋ฅผ basicOuth๋ก ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ์ดํดํ ์ ์์์ต๋๋ค.
instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;
AUTH_TOKEN์ด ๋ฌด์์ธ๊ฐ์? ์์์
๋๊น? ์ด๋ค ๊ฐ์ ์ฃผ์ด์ผ ํฉ๋๊น?
์ด์จ๋ ๋ฏธ๋ฆฌ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
๊ธฐ๋ณธ ์ธ์ฆ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ์ด๋ฆ๊ณผ ๋น๋ฐ๋ฒํธ๋ก ์ธ์ฆํด์ผ ํฉ๋๋ค. ๋ํ ํด๋น ์์ฒญ๊ณผ ํจ๊ป JSON์ ๋ณด๋ด๊ณ ์์ผ๋ฉฐ ์ปฌ ๋ฒ์ ์ "application/x-www-form-urlencoded"๋ฅผ ์ฌ์ฉํฉ๋๋ค. ๋ฌธ์๋ฅผ ์ฝ์ผ์ญ์์ค:
@Spartano ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๊น? ๋์ผํ ์์ ์ ์ํํ๋ ค๊ณ ํ๋๋ฐ 401๋ ํ์๋๊ณ ์ด ๋ฉ์์ง๋ "์ธ์ฆ ์๊ฒฉ ์ฆ๋ช ์ด ์๋ชป๋์๊ฑฐ๋ Authorization ํค๋๊ฐ ๋๋ฝ๋์ด ์ธ์ฆ์ ์คํจํ์ต๋๋ค."๋ผ๋ ๋ฉ์์ง๊ฐ ํ์๋ฉ๋๋ค.
๋ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
axios.post('https://api.sandbox.paypal.com/v1/oauth2/token', querystring.stringify({
grant_type: 'client_credentials',
withCredentials: true,
headers:{
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type': 'application/x-www-form-urlencoded',
},
auth: {
username: 'ClientID',
password: 'Secret'
}
})).then((response) => {
console.log(response);
}).catch((err) => {console.log(err);});
๋๋ ๊ทธ๊ฒ์ ํด๊ฒฐํ๋ค! ๋๊ตฐ๊ฐ ๋์์ด ํ์ํ ๊ฒฝ์ฐ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด Gist๋ฅผ ๋ง๋ค์์ต๋๋ค. https://gist.github.com/TigaxMT/b57c11df3e4ebd0510858ec3df775efa
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋๋ ๊ทธ๊ฒ์ ํด๊ฒฐํ๋ค! ๋๊ตฐ๊ฐ ๋์์ด ํ์ํ ๊ฒฝ์ฐ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด Gist๋ฅผ ๋ง๋ค์์ต๋๋ค. https://gist.github.com/TigaxMT/b57c11df3e4ebd0510858ec3df775efa