发文流程基本解决,唯一就是cookie更新问题,大约2天cookie就会失效,可能是微博服务器那边的机制,解决之后会贴在多账户 模拟登录发文(3)
现在微博多账户图文发送,和微博头条文章发送都解决了
算了,本来想贴完整代码的,发现代码太多了,懒得弄了,只发主要流程的,如果有问题可以联系我qq:604360032
首先保存用户登录cookie:
session.defaultSession.cookies.get({url:arg2},(event,cookies)=>
{
insert_info =
{
img_src:arg1,
web_src:arg2,
type:'weibo',
cookie:cookies,
nick:arg3
}
result.push(insert_info);
//我这里是保存在本地的文件的,公司项目肯定要存在服务器安全
fs.writeFileSync(filename, JSON.stringify(result));
});
用户切换:
//读取cookie
var result=JSON.parse(fs.readFileSync(filename));
for(var i =0;i<result.length;i++)
{
//判断要切换的是哪个用户
if(result[i].img_src == arg1 && result[i].nick == arg3)
{
for(var j =0;j<result[i].cookie.length;j++)
{
session.defaultSession.cookies.set(
{
url:result[i].web_src,
name:result[i].cookie[j].name,
value:result[i].cookie[j].value,
domain:result[i].cookie[j].domain,
hostOnly:result[i].cookie[j].hostOnly,
path:result[i].cookie[j].path,
secure:result[i].cookie[j].secure,
httpOnly:result[i].cookie[j].httpOnly,
session:result[i].cookie[j].session,
expirationDate:result[i].cookie[j].expirationDate,
}
,(error)=>
{
if(error)
{
console.log('set cookie error');
}
});
}
}
}
微博简单单图文发送,想发多图文都是举一反三:
1.获取图片id
var data = fs.readFileSync(img_src); //打开图片
data = new Buffer(data).toString('base64');
var post_data = querystring.stringify({b64_data:data});
var post_options =
{
host: 'picupload.weibo.com',
port: '443',
path: '/interface/pic_upload.php?cb=https%3A%2F%2Fweibo.com%2Faj%2Fstatic%2Fupimgback.html%3F_wv%3D5%26callback%3DSTK_ijax_'+new Date().getTime()+'&mime=image%2Fjpeg&data=base64&url=weibo.com%2Fu%2F3317421975&markpos=1&logo=1&nick=%40%E5%B0%91%E5%B9%B4%E4%B8%B6%E9%99%88%E4%BA%8C%E7%8B%97&marks=0&app=miniblog&s=rdxt&pri=null&file_source=1',
method: 'POST',
headers:
{
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Cache-Control': 'max-age=0',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Connection': 'keep-alive',
'Content-Length': Buffer.byteLength(post_data),
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': cookies[i].cookie,
'Host': 'picupload.weibo.com',
'Origin': 'https://weibo.com',
'Referer': 'https://weibo.com',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36'
}
};
var pid = await get_pid(post_options,post_data);
2.通过cookie发送微博
var post_data = querystring.stringify({
'location': 'v6_content_home',
'text': str,//微博内容
'appkey': '',
'style_type': '1',
'pic_id': pid,//图片id
'tid': '',
'pdetail': '',
'mid': '',
'isReEdit': 'false',
'rank': '0',
'rankid': '',
'module': 'stissue',
'pub_source': 'main_',
'pub_type': 'dialog',
'isPri': '0',
'_t': '0'
});
var post_options = {
host: 'weibo.com',
port: '443',
//path: '/aj/mblog/add?ajwvr=6&__rnd=' + new Date().getTime(),
path: '/aj/mblog/add?ajwvr=6&__rnd=' + new Date().getTime(),
method: 'POST',
headers: {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Connection': 'keep-alive',
'Content-Length': Buffer.byteLength(post_data),
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': this.cookie,//这个是要发送的用户的cookie
'Host': 'weibo.com',
'Origin': 'https://weibo.com',
'Referer': 'https://weibo.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'
}
};
var post_req = https.request(post_options, res => {
res.setEncoding('utf8');
console.log('Status: ' + res.statusCode);
console.log('headers: ' + JSON.stringify(res.headers));
switch (res.statusCode) {
case 200:
console.log('\nSent!');
break;
default:
console.log('\nError!');
}
});
post_req.on('error', e => {
console.log(`Error: ${e.message}`);
});
// post the data
post_req.write(post_data);
post_req.end();
微博头条文章就难了一点,代码有点多,就不贴了,具体流程如下:
1.先create文章,通过response获取文章id
2.获取微博头条文章payinfo pid
get url = e.weibo.com/v1/public/paid/initpid?bid=1000207805&_t=1&ispay=0&callback=STK_'+new Date().getTime()
3.set payinfo pid
post ulr = card.weibo.com/article/v3/aj/editor/settings/setpayinfo
4.保存微博头条文章
5.发送微博头条文章