Skip to content

Commit 87c814f

Browse files
committed
支持cookie,解决401的问题
1 parent ed43a9a commit 87c814f

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

base/request.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ export class Request {
55
constructor(o = {}) {
66
this.proxy = o.proxy;
77
this.cookie = o.cookie;
8-
this.timeout = o.timeout || 30000; // 默认30秒超时
8+
this.timeout = o.timeout || 30000;
99
this.headers = {
1010
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
1111
'accept': 'text/html,application/json,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
12-
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
12+
'accept-language': 'en-US,en;q=0.9',
1313
'cache-control': 'no-cache',
1414
'pragma': 'no-cache',
1515
'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
@@ -20,18 +20,22 @@ export class Request {
2020
'sec-fetch-site': 'none',
2121
'sec-fetch-user': '?1',
2222
'upgrade-insecure-requests': '1',
23-
'x-requested-with': 'XMLHttpRequest',
24-
'x-instagram-ajax': '1',
25-
'referer': 'https://www.instagram.com/'
23+
'x-ig-app-id': '936619743392459',
24+
'x-requested-with': 'XMLHttpRequest'
2625
};
26+
27+
if (!this.cookie) {
28+
this.cookie = 'ig_did=...; csrftoken=...; mid=...; ds_user_id=...; sessionid=...';
29+
}
2730
}
2831

2932
async fetch(url, options = {}) {
3033
const reqOptions = {
3134
...options,
3235
headers: {
3336
...this.headers,
34-
...options.headers
37+
...options.headers,
38+
cookie: this.cookie
3539
},
3640
timeout: this.timeout
3741
};
@@ -41,13 +45,12 @@ export class Request {
4145
reqOptions.agent = new HttpsProxyAgent(this.proxy);
4246
}
4347

44-
if (this.cookie) {
45-
reqOptions.headers.cookie = this.cookie;
46-
}
47-
4848
try {
4949
const response = await fetch(url, reqOptions);
5050
if (!response.ok) {
51+
if (response.status === 401) {
52+
throw new Error('Instagram认证失败,请检查cookie是否有效');
53+
}
5154
throw new Error(`HTTP error! status: ${response.status}`);
5255
}
5356
return response;

bin/cli.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ program
3434
proxy: options.proxy,
3535
timeout: parseInt(options.timeout),
3636
maxItems: parseInt(options.maxItems),
37-
mediaTypes: options.types.split(',')
37+
mediaTypes: options.types.split(','),
38+
cookie: config.cookie
3839
});
3940

4041
await inst.init();
@@ -60,7 +61,8 @@ program
6061
proxy: options.proxy,
6162
maxItems: account.maxItems,
6263
timeout: parseInt(options.timeout),
63-
mediaTypes: account.mediaTypes
64+
mediaTypes: account.mediaTypes,
65+
cookie: config.cookie
6466
});
6567

6668
await inst.init();

config.json.sample

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
}
99
],
1010
"fetchInterval": "1d",
11-
"timeout": 30000
11+
"timeout": 30000,
12+
"cookie": "ig_did=...; csrftoken=...; mid=...; ds_user_id=...; sessionid=..."
1213
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ins-grabber",
3-
"version": "0.0.3",
3+
"version": "0.0.5",
44
"type": "module",
55
"bin": {
66
"ins-grab": "./bin/cli.js"

0 commit comments

Comments
 (0)