dilame / instagram-private-api

NodeJS Instagram private API SDK. Written in TypeScript.
MIT License
5.93k stars 1.14k forks source link

CookieNotValidError #462

Closed DenisKrsk closed 6 years ago

DenisKrsk commented 6 years ago

Hi! Everything worked for me. And now suddenly ceased with here such here error.

{"name":"CookieNotValidError","message":"Cookie sessionid you are searching found was either not found or not valid!"}

Nothing changed in the code. Cookies cleaned. What could be the reason?

alexandr-bbm commented 6 years ago

It seems that cookies host changed from i.instagram.com to instagram.com However, simple change of HOSTNAME from constants.js does not help

DenisKrsk commented 6 years ago

I understand the problem is not only mine?

DenisKrsk commented 6 years ago

I have a strange soft it works with the version of Instagram 22.0.0.15.68. It works fine. Requests go all also to i.instagram.com

bnopne commented 6 years ago

Just got this error when i tried to log in. Are there any workarounds?

mellodev commented 6 years ago

Workaround if you are building against a local copy of instagram-private-api, change /v1/cookie-storage.js line 21 from

self.storage.findCookie(CONSTANTS.HOSTNAME, '/', name, function(err, cookie) {

to

self.storage.findCookie('instagram.com', '/', name, function(err, cookie) {

I refactored this to CONSTANTS.COOKIE_DOMAIN.

Seems to work, not sure of the ramifications.. Changing CONSTANTS.HOSTNAME breaks other things, leave it alone. I'm sure there's a better fix, but I don't know enough about the cookie implementation to offer a real PR.

mellodev commented 6 years ago

I think IG has rolled back the change they made that broke us. I'm getting the CookieNotValid/sessionid exception now when using my workaround above. Reverting that hack (or using i.instagram.com for COOKIE_DOMAIN) resolves the issue... So we're back to normal I guess?

bnopne commented 6 years ago

Yeah, it seems that i can login again, thanks for you response @mellodev

mvhirsch commented 6 years ago

I currently do face the same issue. I'll try the workaround.

IvanMMM commented 6 years ago

IG now uses both domains. i.instagram.com and instagram.com. There should be a way to use wildcards, to avoid checking for two hosts or we can just force JAR to write cookies at specific hostname, regardless of host IG tells us. But for now, it works.

function CookieStorage(cookieStorage) {
    this.storage = Promise.promisifyAll(cookieStorage);
}

CookieStorage.prototype.getCookieValue = function (name,host=CONSTANTS.COOKIEHOSTNAME) {
    return this.storage.findCookieAsync(host, '/', name)
    .then(cookie=>{
        if(!_.isObject(cookie) && host === CONSTANTS.COOKIEHOSTNAME){
            return this.getCookieValue(name,CONSTANTS.HOSTNAME)
        }else if(!_.isObject(cookie)){
            throw new Exceptions.CookieNotValidError(name);
        }else{
            return cookie;
        }
    });
};

CookieStorage.prototype.getCookies = function (host=CONSTANTS.COOKIEHOSTNAME) {
    return this.storage.findCookiesAsync(host, '/')
    .then(cookies=>{
        if(!cookies && host === CONSTANTS.COOKIEHOSTNAME){
          return this.getCookies(CONSTANTS.HOSTNAME)
        }else {
          return cookies || [];
        }
    });
};
IvanMMM commented 6 years ago

Another way to solve this error: Add this to \client\v1\jar.js

uri = uri.replace(`://${CONSTANTS.COOKIEHOSTNAME}/`,`://${CONSTANTS.HOSTNAME}/`);
mvhirsch commented 6 years ago

Sorry, but hot-patching third-party code shouldn't be the preferred way to fix problems. @IvanMMM is there any chance you could do a PR so this can be fixed and merged and packaged right away?

ifedapoolarewaju commented 6 years ago

btw, here's a working patch I added to my fork https://github.com/ifedapoolarewaju/instagram-private-api/commit/54613a55a0e74b2263b5fed31223691ae2fde899

Not the cleanest, but it works.

mathdroid commented 6 years ago

@ifedapoolarewaju Would you do a PR based on your patch on your fork?

IvanMMM commented 6 years ago

@ifedapoolarewaju That's not the best fix. I'l explain why. Let's say you want to check if session contains valid cookies. Easiest way to do it is to check for session._cookiesStore.storage.idx['i.instagram.com']['/']['sessionid'], but in your case you will have to check for two places. I recommend you to write all cookie files to the same store, same as searching for them at the same place. Don't care about IG domain.

IvanMMM commented 6 years ago

\client\v1\jar.js

RequestJar.prototype.setCookie = function(cookieOrStr, uri, options) {
    cookieOrStr = cookieOrStr.replace(/Domain=\.?instagram\.com;\s?/,'');
    uri = this.rewriteUri(uri);
    return this._jar.setCookieSync(cookieOrStr, uri, options || {});
};
drawrowfly commented 6 years ago

Maybe that's non-related issue. But I just noticed that sometimes(randomly) cookies are saved in wrong JSON format, so basically, tough-cookie adds extra 1 or 2 curly braces at the end of JSON file where cookies are stored and it makes that file invalid JSON

checho221 commented 6 years ago

@glazkoman I suspect the same, now i have 7 accounts and just one with this issue, even when implemented @IvanMMM fix on jar.js

drawrowfly commented 6 years ago

Its an issue with Async writing to file in tough-cookie-filestore package

IvanMMM commented 6 years ago

@glazkoman @checho221 Mine are fine, since I use redis database as cookie storage

drawrowfly commented 6 years ago

@IvanMMM yes it is completely different. Anyway fixed already

Thanks

checho221 commented 6 years ago

Well i just said that not working for me @IvanMMM & @glazkoman , so how can i prevent cookie rewrite on CookieNotValidError exeption? if i backup cookie works fine for a while

fostergn commented 6 years ago

Hey guys,

I'm still getting a

"CookieNotValidError: Cookie `sessionid` you are searching found was either not found or not valid!"

after installing the most recent version. "instagram-private-api": "^0.6.7",

checho221 commented 6 years ago

@fostergn this is related to async writting like @glazkoman said, so you have to workaround in order to do not call CookieFileStorage twice or more times, so you have to call it sync for now.

fostergn commented 6 years ago

does this mean creating the session sync instead of async @checho221 ? I'm currently:

  const device = new Client.Device(account.instagram.username);

  const storage = new Client.CookieMemoryStorage();

  const session = await Client.Session.create(device, storage, account.instagram.username, account.instagram.password)

does this mean instead calling const session = Client.Session.create(device, storage, account.instagram.username, account.instagram.password)

Thanks in advance for responding 👍

checho221 commented 6 years ago

@fostergn No, i mean new Client.CookieFileStorage method, just call it sync and dont call it twice at time

fostergn commented 6 years ago

@checho221 I'm running this as a serverless function, so I'm using CookieMemoryStorage() instead of CookieFileStorage(). I'm not invoking new Client.CookieMemoryStorage() twice and I assume that the code I've posted above is calling it synchronously unless you mean calling Client.Session.create() should be done sync (but i'm not sure how that'd be possible)?

fostergn commented 6 years ago

I'm still blocked on this. Wondering if I need to setup like a public webtask in order for someone to reproduce the issue

RiaanJacobs76 commented 6 years ago

@IvanMMM could you please show a code example of how to use redis-cookie-monster as your storage when doing the login? I'm already using the new files from your branch, I just need to know how to use it now. Thanks in advance.

yuceltoluyag commented 5 years ago

Hey, To everyone that tackles this in the future I managed to get it working. This problem was fixed in version 0.7, but you can't get it from NPM yet. If you will install this library with npm you will get 0.6.8(At this moment of writing this). to download the specific updated working version try to add this to package.json:

"instagram-private-api": "git://github.com/huttarichard/instagram-private-api#0.7.0"

and run npm install afterwards. This way the module will be installed directly from github and not from NPM.

https://github.com/dilame/instagram-private-api/issues/596#issuecomment-453927349

aleksanyanV commented 5 years ago

Hello, Thank you. I want to say that I can't login, but don't understand why. I see this error. When I try to login each account,I see this error. When accept,again I see this error.Please if you can,help me. Thanks. CheckpointError: Instagram call checkpoint for this action!

ср, 16 янв. 2019 г. в 04:15, yücel notifications@github.com:

Hey, To everyone that tackles this in the future I managed to get it working. This problem was fixed in version 0.7, but you can't get it from NPM yet. If you will install this library with npm you will get 0.6.8(At this moment of writing this). to download the specific updated working version try to add this to package.json:

"instagram-private-api": "git:// github.com/huttarichard/instagram-private-api#0.7.0"

and run npm install afterwards. This way the module will be installed directly from github and not from NPM.

596 (comment)

https://github.com/dilame/instagram-private-api/issues/596#issuecomment-453927349

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dilame/instagram-private-api/issues/462#issuecomment-454601582, or mute the thread https://github.com/notifications/unsubscribe-auth/Ae38JCk6IRur5NUAwIdbo06XRTEDhXsGks5vDm8fgaJpZM4TbYfb .

yuceltoluyag commented 5 years ago

Delete igdm-cli/node_modules/instagram-private-api folder and change ur package.json

{
  "_from": "igdm-cli",
  "_id": "igdm-cli@0.5.3",
  "_inBundle": false,
  "_integrity": "sha1-YUMioIUsUdH85o3KrYaqPqky4Dc=",
  "_location": "/igdm-cli",
  "_phantomChildren": {},
  "_requested": {
    "type": "tag",
    "registry": true,
    "raw": "igdm-cli",
    "name": "igdm-cli",
    "escapedName": "igdm-cli",
    "rawSpec": "",
    "saveSpec": null,
    "fetchSpec": "latest"
  },
  "_requiredBy": [
    "#USER"
  ],
  "_resolved": "https://registry.npmjs.org/igdm-cli/-/igdm-cli-0.5.3.tgz",
  "_shasum": "614322a0852c51d1fce68dcaad86aa3ea932e037",
  "_spec": "igdm-cli",
  "_where": "/home/friday13",
  "bin": {
    "igdm": "bin/index.js"
  },
  "bugs": {
    "url": "https://github.com/mathdroid/igdm-cli/issues"
  },
  "bundleDependencies": false,
  "dependencies": {
    "babel-runtime": "^6.26.0",
    "bignumber.js": "^5.0.0",
    "chalk": "^2.3.0",
    "has-ansi": "^3.0.0",
    "inquirer": "^4.0.2",
    "instagram-private-api": "git://github.com/huttarichard/instagram-private-api#0.7.0",
    "keypress": "^0.2.1",
    "log-update": "^2.3.0",
    "moment": "^2.20.1",
    "mri": "^1.1.0",
    "ms": "^2.1.1",
    "ora": "^1.3.0",
    "update-notifier": "^2.3.0"
  },
  "deprecated": false,
  "description": "> Instagram's direct message, right in your terminal.",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.6.1",
    "np": "^2.20.1",
    "release": "^3.0.3"
  },
  "files": [
    "package.json",
    "README.md",
    "bin"
  ],
  "homepage": "https://github.com/mathdroid/igdm-cli#readme",
  "license": "MIT",
  "name": "igdm-cli",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/mathdroid/igdm-cli.git"
  },
  "scripts": {
    "build": "babel src -d bin",
    "release": "np",
    "release:minor": "release minor"
  },
  "version": "0.5.3"
}

and open terminal npm install or npm update. Easy

checho221 commented 5 years ago

@yuceltoluyag >= 0.7 Version still having issues with cookies, look https://github.com/dilame/instagram-private-api/issues/632

Using 0.7 Version and old cookies work while changing i.instagram.com to instagram.com, but i found that is not always working, i am getting CookieNotValidError sometimes but is is disastrous because it is writing user.json files empty with 0kb, and it is breaking all functions

aleksanyanV commented 4 years ago

Hello. I want to user instagram-private-api that created from you. I want to say that I used it 2 years ago, and everything is OK. But I see now that you are doing changes. Global change TypeScript. I have a question. Can I create session after login, and that session token using any request to isntagram? For example if I want to get user (123789) last image (568974) all liked accounts, I must do login, get user, get user image, and get image liked account. I want to use token to do this steps. Can you help me? Thank you. Regards from Vahagn.