jlobos / instagram-web-api

🤳 Instagram Private Web API client for Node
https://npmjs.com/instagram-web-api
MIT License
1.12k stars 185 forks source link

Asked to re-login when liking a picture #157

Open eoboite opened 4 years ago

eoboite commented 4 years ago

Hello,

When liking an image it throws an error inside the try/catch block but when I examine the error object, the actual error key is undefined but the message is "302 - undefined".

I noticed this URL in the raw headers "https://www.instagram.com/accounts/login/?next=/web/likes/2348376614742294239/like/"

Looks like it's asking me to login in again then redirect me to actually like the photo.

How do I deal with something like this?

codexJoin commented 4 years ago

Show your code to us. Remove username and password, and paste here your code

eoboite commented 4 years ago

I pasted the code below. I have two accounts that I run the code against. It works for one but the other account it errors out:

`const _ = require("lodash"); const Client = require('instagram-web-api'); const FileCookieStore = require('tough-cookie-filestore2');

class Instagram { constructor(config) { this._validateConfigOrThrow(config); const username = config.username; const password = config.password; this._cookiesPath = path.join(__dirname, ../../cookies/${config.username}-web.json); const cookieStore = new FileCookieStore(this._cookiesPath); this._client = new Client({username, password, cookieStore}) }

async loginIfNeeded() {
    await this._client.login();
}

async fetchMediaByHashTag(hashtag, first = 10) {
    const response = await this._client.getPhotosByHashtag({hashtag, first});
    const medias = response["hashtag"]["edge_hashtag_to_media"]["edges"].map((media) => media["node"]);
    return _.uniqBy(medias, 'owner.id');
}

async likeMediaByIds(...mediaIds) {
    let liked = [];
    for (const mediaId of mediaIds) {
        try {
            console.log(`Liking media with ID: ${mediaId}`);
            await edh.sleep(edh.randomTime({min: 7, max: 10}));
            await this._client.like({mediaId});
            liked.push(mediaId);
        } catch (error) {
            console.log(error);
        }
    }
    return liked;
}

_validateConfigOrThrow(config) {
    if (typeof config !== 'object') {
        throw new TypeError(`config must be an object, got ${typeof config}`);
    }

    ["username", "password"].forEach((key) => {
        if (!config[key]) throw new Error(`InstagramBot config must include ${key} property`);
    });
}

}

(async function(){ try{ const instagram = new Instagram("johnny", "appleseed"); await instagram.loginIfNeeded();

    const mediaIds = await instagram.fetchMediaByHashTag("sneakers");

    //Method below errors out
    const liked = await instagram.likeMediaByIds(...mediaIds.slice(0, 10));
}catch(error){
    console.log(error);
}

})();`

eoboite commented 4 years ago

it's pasting ugly for some reason

codexJoin commented 4 years ago

To this, don't save cookies in a file, just make code without cookie storage.

chrisyeung1121 commented 4 years ago

Wonder if you still have this issue since I am having this issue now.

eoboite commented 4 years ago

Hey, yes I am. It's only happening for one of my accounts. But I tried not storing in cookies and still the same thing. I moved it to different servers to see if it had something to do with it IP Address but no luck.

It only happens when I try to like a photo. Fetching media etc no issue.

On Wed, Jul 22, 2020 at 8:18 AM chrisyeung1121 notifications@github.com wrote:

Wonder if you still have this issue since I am having this issue now.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jlobos/instagram-web-api/issues/157#issuecomment-662420165, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4C2JZ4QIQS6IWVUWKA6XDR43KJTANCNFSM4OT62RHQ .

chrisyeung1121 commented 4 years ago

I was trying this on Like, Follow, Unfollow - All prompting me with 302. I wonder if replaying challenge will help ?

eoboite commented 4 years ago

Interesting. I'm wondering if something changed with IG web API.

I'm not sure if the replaying challenge will work. When I inspect the request headers that are being returned, it's trying to redirect us to a login and then upon successful login it will complete whatever action(like/follow/unfollow) you're trying to do.

On Wed, Jul 22, 2020 at 10:41 AM chrisyeung1121 notifications@github.com wrote:

I was trying this on Like, Follow, Unfollow - All prompting me with 302. I wonder if replaying challenge will help ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jlobos/instagram-web-api/issues/157#issuecomment-662493579, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4C2J2TZYQRM4FOE23AR23R433DHANCNFSM4OT62RHQ .

SethLily commented 4 years ago

I'm a newbie but what did you guys use to execute the codes? :) this is embarassing

eoboite commented 4 years ago

You need to install this dependency via npm into your project. Create a file where you want to execute your code from there start coding lol

On Wed, Jul 22, 2020 at 2:29 PM SethLily notifications@github.com wrote:

I'm a newbie but what did you guys use to execute the codes? :) this is embarassing

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jlobos/instagram-web-api/issues/157#issuecomment-662614596, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4C2J5K6VBSOLQ6WGZ2L33R44VZ5ANCNFSM4OT62RHQ .

joebengos commented 3 years ago

Hello, I'm having this same problem to follow a profile.

He return 302 - undefined

Does anyone suggest something?

const Instagram = require("instagram-web-api");
const FileCookieStore = require("tough-cookie-filestore2");

const username = "*********";
const password = "**********";

(async () => {
    const cookieStore = new FileCookieStore("./cookies.json");
    const client = new Instagram({ username, password, cookieStore });

    try {
        const user = await client.getUserByUsername({username: anyUser }); //example 
        await client.follow({ userId: user.id });
        console.log('Seguiu com sucesso!', user.id);
    } catch (error) {
        console.log(error.message);
    }

})();