truepush 웹 사이트에서 일부 데이터를 스크랩하려고하는데 먼저 인증을 받아야합니다. 그래서 여기에 내가하는 일이 있습니다 :
const loginUrl = 'https://app.truepush.com/api/v1/login'
let loginResult = await axios.get(loginUrl)
.then(({ headers }, err) => {
if (err) console.error(err);
return headers['set-cookie'][0];
})
.then((cookie, err) => {
if (err) console.error(err);
const splitByXsrfCookieName = cookie.split("XSRF-TOKEN=")[1]
return splitByXsrfCookieName.split(';')[0];
}).then(xsrfToken => {
return axios.post(loginUrl, {
headers: {
"Content-Type": "application/json",
"X-XSRF-TOKEN": xsrfToken
}
})
}).then(res => console.log(res))
두 번째 then
응답에서 xrsfToken이 발생하고 해당 xsrf 토큰으로 세 번째 응답으로 로그인하려고하면 다음 오류가 표시됩니다.
{
"status_code": "XSRF-ERROR",
"status": "ERROR",
"message": "Cross domain requests are not accepting to this endpoint. If you cleared the cookies, please refresh your browser."
}
나는 내가 뭘 잘못하고 있는지 잘 모르겠습니다 :(
주요 문제는 호출시 원래 쿠키를 전송해야한다는 것입니다. set-cookie
헤더 에서 가져온 원래 쿠키를 유지 cookie
하고 두 번째 호출 에서 헤더에 전달해야합니다 cookie: originalCookie
. 또한 코드에는 POST 호출에서 보낸 본문이 없습니다.
다음 코드는 로그인을 재현합니다.
const axios = require("axios");
const originalUrl = 'https://app.truepush.com';
const loginUrl = 'https://app.truepush.com/api/v1/login';
const email = "your-email@xxxxxx";
const password = "your-password";
(async () => {
await axios.get(originalUrl)
.then(({ headers }, err) => {
if (err) console.error(err);
const cookie = headers['set-cookie'][0];
return {
cookie: cookie,
xsrfToken: cookie.split("XSRF-TOKEN=")[1].split(";")[0]
};
})
.then((data, err) => {
if (err) console.error(err);
return axios.post(loginUrl, {
"email": email,
"password": password,
"keepMeLoggedIn": "yes"
}, {
headers: {
"X-XSRF-TOKEN": data.xsrfToken,
"cookie": data.cookie
}
})
})
.then(res => console.log(res.data))
})();
산출:
{
status_code: 'SUCCESS',
status: 'SUCCESS',
message: 'Login Successful',
data: {
id: 'xxxxxxxxxxxxxxxxxxx',
name: 'xxxxx',
email: 'xxxxxxx@xxxxxx'
}
}
cookie
및 둘 다 두 xsrfToken
번째 약속에 의해 소비됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다