dilame / instagram-private-api

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

IgNetworkError [RequestError]: Error: tunneling socket could not be established, statusCode=407 #1465

Open Vaasu-Dhand opened 3 years ago

Vaasu-Dhand commented 3 years ago

Bug Report

Read the Notes and fill out the form.

Notes

Your issue will be closed if you violate any rule below.

Form

Put an [x] if you meet the condition, else leave [ ].

Requirements

Description

I have deployed a scheduled Instagram poster to heroku using the Instagram-private-api. It was worked fine for a few days, but in the last two or three days, it cannot upload any posts when deployed on heroku. It works locally though. This error might be related to proxies. For proxies, I am using FIXIE addon on Heroku. However, I checked that and it is proxying my requests correctly.

Code

My Instagram Posting Code -

import { get } from 'request-promise';
import { IgApiClient, MediaRepositoryConfigureResponseRootObject } from 'instagram-private-api';
import getCaption from './caption';
import { getEmailBody, sendEmail } from '../email';
import { FAILED }  from '../utils'

export default async function post(imageId: string, imageURL: string) {
  try {
    const ig = new IgApiClient();

    ig.state.generateDevice(process.env.IG_USERNAME);
    // Optionally you can setup proxy url
    if (process.env.NODE_ENV === "production") {  // * Might have to test it
      console.log(process.env.FIXIE_URL);
      ig.state.proxyUrl = process.env.FIXIE_URL
    }
    await ig.simulate.preLoginFlow();
    const loggedInUser = await ig.account.login(
      process.env.IG_USERNAME,
      process.env.IG_PASSWORD
    );
    // The same as preLoginFlow()
    // Optionally wrap it to process.nextTick so we dont need to wait ending of this bunch of requests
    process.nextTick(async () => await ig.simulate.postLoginFlow());

    // * Post Image
    const imageBuffer = await get({
      url: imageURL, // random picture with 800x800 size
      encoding: null, // this is required, only this way a Buffer is returned
    });

    const publishResult: MediaRepositoryConfigureResponseRootObject = await ig.publish.photo({
      file: imageBuffer, // image buffer, you also can specify image from your disk using fs
      caption: getCaption(), // nice caption (optional)
    });
    return publishResult;
  } catch (error) {
    console.log(error);
    // * Send Post Failed Email
    const [text, html] = getEmailBody(FAILED, imageId, error)
    sendEmail(FAILED, imageId, text, html);
  }
}

Error and Output

If you've got an error put it here. You can get the stacktrace in the most environments using Error.stack (MDN Docs).

2021-07-10T18:21:39.266368+00:00 heroku[web.1]: State changed from starting to up
2021-07-10T18:21:39.307182+00:00 app[web.1]: IgNetworkError [RequestError]: Error: tunneling socket could not be established, statusCode=407
2021-07-10T18:21:39.307211+00:00 app[web.1]: at Request.faultTolerantRequest (/app/node_modules/instagram-private-api/dist/core/request.js:132:19)
2021-07-10T18:21:39.307212+00:00 app[web.1]: at async Request.send (/app/node_modules/instagram-private-api/dist/core/request.js:47:26)
2021-07-10T18:21:39.307216+00:00 app[web.1]: at async AccountRepository.msisdnHeaderBootstrap (/app/node_modules/instagram-private-api/dist/repositories/account.repository.js:250:26) {
2021-07-10T18:21:39.307217+00:00 app[web.1]: cause: Error: tunneling socket could not be established, statusCode=407
2021-07-10T18:21:39.307217+00:00 app[web.1]: at ClientRequest.onConnect (/app/node_modules/tunnel-agent/index.js:166:19)
2021-07-10T18:21:39.307218+00:00 app[web.1]: at Object.onceWrapper (events.js:482:26)
2021-07-10T18:21:39.307218+00:00 app[web.1]: at ClientRequest.emit (events.js:375:28)
2021-07-10T18:21:39.307219+00:00 app[web.1]: at Socket.socketOnData (_http_client.js:553:11)
2021-07-10T18:21:39.307219+00:00 app[web.1]: at Socket.emit (events.js:375:28)
2021-07-10T18:21:39.307219+00:00 app[web.1]: at addChunk (internal/streams/readable.js:290:12)
2021-07-10T18:21:39.307220+00:00 app[web.1]: at readableAddChunk (internal/streams/readable.js:265:9)
2021-07-10T18:21:39.307220+00:00 app[web.1]: at Socket.Readable.push (internal/streams/readable.js:204:10)
2021-07-10T18:21:39.307221+00:00 app[web.1]: at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
2021-07-10T18:21:39.307222+00:00 app[web.1]: code: 'ECONNRESET'
2021-07-10T18:21:39.307222+00:00 app[web.1]: },
2021-07-10T18:21:39.307223+00:00 app[web.1]: error: Error: tunneling socket could not be established, statusCode=407
2021-07-10T18:21:39.307223+00:00 app[web.1]: at ClientRequest.onConnect (/app/node_modules/tunnel-agent/index.js:166:19)
2021-07-10T18:21:39.307223+00:00 app[web.1]: at Object.onceWrapper (events.js:482:26)
2021-07-10T18:21:39.307224+00:00 app[web.1]: at ClientRequest.emit (events.js:375:28)
2021-07-10T18:21:39.307224+00:00 app[web.1]: at Socket.socketOnData (_http_client.js:553:11)
2021-07-10T18:21:39.307224+00:00 app[web.1]: at Socket.emit (events.js:375:28)
2021-07-10T18:21:39.307225+00:00 app[web.1]: at addChunk (internal/streams/readable.js:290:12)
2021-07-10T18:21:39.307225+00:00 app[web.1]: at readableAddChunk (internal/streams/readable.js:265:9)
2021-07-10T18:21:39.307225+00:00 app[web.1]: at Socket.Readable.push (internal/streams/readable.js:204:10)
2021-07-10T18:21:39.307226+00:00 app[web.1]: at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
2021-07-10T18:21:39.307226+00:00 app[web.1]: code: 'ECONNRESET'
2021-07-10T18:21:39.307226+00:00 app[web.1]: },
2021-07-10T18:21:39.307227+00:00 app[web.1]: options: {
2021-07-10T18:21:39.307227+00:00 app[web.1]: method: 'POST',
2021-07-10T18:21:39.307227+00:00 app[web.1]: url: '/api/v1/accounts/msisdn_header_bootstrap/',
2021-07-10T18:21:39.307228+00:00 app[web.1]: form: {
2021-07-10T18:21:39.307228+00:00 app[web.1]: ig_sig_key_version: '4',
2021-07-10T18:21:39.307230+00:00 app[web.1]: signed_body: '6ab187f149487a785bf75c8e8a7911cad107c71dcd80577583e0e369e72db4d2.{"mobile_subno_usage":"ig_select_app","device_id":"7b4b2abb-ac29-5dd5-ba7f-0a0f999b2b5f"}'
2021-07-10T18:21:39.307230+00:00 app[web.1]: },
2021-07-10T18:21:39.307231+00:00 app[web.1]: baseUrl: 'https://i.instagram.com/',
2021-07-10T18:21:39.307231+00:00 app[web.1]: resolveWithFullResponse: true,
2021-07-10T18:21:39.307231+00:00 app[web.1]: proxy: 'http://fixie:c7KwVWl6fVUXVh6@velodrome.usefixie.com:80',
2021-07-10T18:21:39.307232+00:00 app[web.1]: simple: false,
2021-07-10T18:21:39.307232+00:00 app[web.1]: transform: [Function: requestTransform],
2021-07-10T18:21:39.307232+00:00 app[web.1]: jar: RequestJar { _jar: [CookieJar] },
2021-07-10T18:21:39.307233+00:00 app[web.1]: strictSSL: false,
2021-07-10T18:21:39.307233+00:00 app[web.1]: gzip: true,
2021-07-10T18:21:39.307233+00:00 app[web.1]: headers: {
2021-07-10T18:21:39.307234+00:00 app[web.1]: 'User-Agent': 'Instagram 121.0.0.29.119 Android (27/8.1.0; 480dpi; 1080x2150; HUAWEI/HONOR; COL-L29; HWCOL; kirin970; en_US; 185203708)',
2021-07-10T18:21:39.307234+00:00 app[web.1]: 'X-Ads-Opt-Out': '0',
2021-07-10T18:21:39.307234+00:00 app[web.1]: 'X-CM-Bandwidth-KBPS': '-1.000',
2021-07-10T18:21:39.307234+00:00 app[web.1]: 'X-CM-Latency': '-1.000',
2021-07-10T18:21:39.307235+00:00 app[web.1]: 'X-IG-App-Locale': 'en_US',
2021-07-10T18:21:39.307235+00:00 app[web.1]: 'X-IG-Device-Locale': 'en_US',
2021-07-10T18:21:39.307235+00:00 app[web.1]: 'X-Pigeon-Session-Id': '508ef0b8-a496-5ac8-a914-4b9a0aedbcb5',
2021-07-10T18:21:39.307236+00:00 app[web.1]: 'X-Pigeon-Rawclienttime': '1625941299.151',
2021-07-10T18:21:39.307236+00:00 app[web.1]: 'X-IG-Connection-Speed': '3312kbps',
2021-07-10T18:21:39.307237+00:00 app[web.1]: 'X-IG-Bandwidth-Speed-KBPS': '-1.000',
2021-07-10T18:21:39.307237+00:00 app[web.1]: 'X-IG-Bandwidth-TotalBytes-B': '0',
2021-07-10T18:21:39.307237+00:00 app[web.1]: 'X-IG-Bandwidth-TotalTime-MS': '0',
2021-07-10T18:21:39.307238+00:00 app[web.1]: 'X-IG-EU-DC-ENABLED': undefined,
2021-07-10T18:21:39.307238+00:00 app[web.1]: 'X-IG-Extended-CDN-Thumbnail-Cache-Busting-Value': '1000',
2021-07-10T18:21:39.307238+00:00 app[web.1]: 'X-Bloks-Version-Id': '1b030ce63a06c25f3e4de6aaaf6802fe1e76401bc5ab6e5fb85ed6c2d333e0c7',
2021-07-10T18:21:39.307239+00:00 app[web.1]: 'X-MID': undefined,
2021-07-10T18:21:39.307239+00:00 app[web.1]: 'X-IG-WWW-Claim': '0',
2021-07-10T18:21:39.307239+00:00 app[web.1]: 'X-Bloks-Is-Layout-RTL': 'false',
2021-07-10T18:21:39.307239+00:00 app[web.1]: 'X-IG-Connection-Type': 'WIFI',
2021-07-10T18:21:39.307240+00:00 app[web.1]: 'X-IG-Capabilities': '3brTvwE=',
2021-07-10T18:21:39.307240+00:00 app[web.1]: 'X-IG-App-ID': '567067343352427',
2021-07-10T18:21:39.307240+00:00 app[web.1]: 'X-IG-Device-ID': '7b4b2abb-ac29-5dd5-ba7f-0a0f999b2b5f',
2021-07-10T18:21:39.307241+00:00 app[web.1]: 'X-IG-Android-ID': 'android-82ffd668279a0697',
2021-07-10T18:21:39.307241+00:00 app[web.1]: 'Accept-Language': 'en-US',
2021-07-10T18:21:39.307241+00:00 app[web.1]: 'X-FB-HTTP-Engine': 'Liger',
2021-07-10T18:21:39.307241+00:00 app[web.1]: Authorization: undefined,
2021-07-10T18:21:39.307242+00:00 app[web.1]: Host: 'i.instagram.com',
2021-07-10T18:21:39.307242+00:00 app[web.1]: 'Accept-Encoding': 'gzip',
2021-07-10T18:21:39.307242+00:00 app[web.1]: Connection: 'close'
2021-07-10T18:21:39.307243+00:00 app[web.1]: },
2021-07-10T18:21:39.307243+00:00 app[web.1]: callback: [Function: RP$callback],
2021-07-10T18:21:39.307243+00:00 app[web.1]: transform2xxOnly: false
2021-07-10T18:21:39.307243+00:00 app[web.1]: },
2021-07-10T18:21:39.307244+00:00 app[web.1]: response: undefined
2021-07-10T18:21:39.307244+00:00 app[web.1]: }
NotII commented 3 years ago

Does your proxy require any authentication? if so that may be the issue

Vaasu-Dhand commented 3 years ago

No, it doesn't. I am using FIXIE ad-on on heroku

NotII commented 3 years ago

Most likely to do with the IP's

pavelefimenko95 commented 1 year ago

I had the same issue, set "pool: false" in the request options of the lib (instagram-private-api/dist/core/request.js). Not sure what it does but it fixes the issue for me. Maybe it is to do with the nodejs version, i'm using current lts.