xhr запрос: как получить x-csrftoken? - PullRequest
0 голосов
/ 15 сентября 2018

Привет, я пытаюсь сделать XHR-запрос, но мне нужен токен.Когда я пытаюсь получить его с помощью этого кода:

http.setRequestHeader('x-csrftoken', 
window._sharedData.config.csrf_token);

выдает ошибку:

window._sharedData не определено

Doу тебя есть идеи?

вот мой весь код:

function check_insta(username, password){
    var params = "username="+username+"&password="+password;

    new Promise(function (resolve, reject) {
        var http = new XMLHttpRequest();
        console.log(window._sharedData);
        http.open("POST", "https://www.instagram.com/accounts/login/ajax/", true);
        http.setRequestHeader('x-csrftoken', window._sharedData.config.csrf_token);
        http.setRequestHeader('x-instagram-ajax', '1');
        http.setRequestHeader('x-requested-with', 'XMLHttpRequest');
        http.setRequestHeader("pragma", "no-cache");
        http.setRequestHeader("cache-control", "no-cache");
        http.setRequestHeader("accept-language", "en-US,en;q=0.8,it;q=0.6");
        http.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        http.onreadystatechange = function () {
            if (http.readyState == 4 && http.status !== 200) {
                console.log('error'+ http.status);
                reject('error: ' + http.status);
            };
            if (http.readyState == 4 && http.status == 200) {
                var json = JSON.parse(http.response);
                if (!json.authenticated) {
                    console.log('wrong pwd');
                    reject('bad password');
                } else if (json.authenticated && json.user && json.status === 'ok') {
                    console.log('success cookie');
                    resolve('success:', document.cookie);
                };
            };
        };
        http.send(params);
    }).catch(function (error) {
        console.log(error);
    });
}
...