如何使用nodejs代码在新浪微博上发布帖子

88 阅读1分钟

我写了一小段程序,代码如下:

var config = require("./mcConfig.js");
var request = require("request");

function sendWCMeaasge(content, callback){

    var postData = {
        location:"v6_content_home",
        text: content,
        style_type:"1",
        isReEdit:false,
        module:"stissue",
        pub_source:"main_",
        pub_type:"dialog",
        isPri:0,
        _t:0
    };

    var options = {
            url: "https://www.weibo.com/aj/mblog/add?ajwvr=6&__rnd=1564468131832",
            method: "POST",
            headers: {
                "cookie": config.cookie,
                "origin": "https://www.weibo.com",
                "referer": "https://www.weibo.com/abap/home?wvr=5",
                "content-type": "application/x-www-form-urlencoded"
            },
            form: postData
          };
      request(options,function(error,response,data){
          // console.log(data);
          if( !!data){
              var oResponse = JSON.parse(data);
              callback(oResponse.code);
          }
      });
  }

module.exports = sendWCMeaasge;

使用下面这段简单的代码来消费这个发送函数:

var tool = require("./SendWeiboModule.js");
var config = require("./mcConfig.js");

tool("i042416", function(code){
    console.log("response from Weibo: " + code);
});

使用命令行执行这个nodejs应用:

发送一条微博,内容为i042416: 马上在浏览器上就能看到这条帖子了:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":