C# 新浪微博群发器
时间:2011-05-30 来源:FMN
通过新浪微博api群发微博,使用了sina提供的sdk,并对其进行小改,跳过了oauth页面认证。这个sdk用起来挺方便的。下面介绍实现方法,给有这方面需要的一个参考。
犹豫跳过了oauth页面认证的方法需要发送一次用户密码,大家应该酌情使用。我小改的sdk下载地址,将下载下来的文件夹复制到项目中:http://files.cnblogs.com/fmnisme/sinaApi.rar
sina官方文档中心:http://open.weibo.com/wiki/index.php/%E9%A6%96%E9%A1%B5
绕过oauth页面认证的方法使用了博客园akita 的方法,在此感谢,关于该方法的博客:http://www.cnblogs.com/btxakita/archive/2011/05/24/2055767.html
skd使用方法:
首先修改SDK中GlobalInfo类的appkey,appSecret为你在sina申请到的appKey,appSecret;
var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET) as HttpGet;
//因为群发博客,所以用数组存放了用户信息,x代表数组下标。
httpRequest.GetRequestToken();
string url = httpRequest.GetAuthorizationUrl();
GlobalInfo.requestTokens[x] = httpRequest.Token;
GlobalInfo.requesTokenSecrets[x] = httpRequest.TokenSecret;
httpRequest.GetVerifier(“用户名[x]”,“密码[x]”x);
httpRequest.GetAccessToken();
GlobalInfo.requestTokens[x] = httpRequest.Token;
GlobalInfo.requesTokenSecrets[x] = httpRequest.TokenSecret;
var sendUrl = "http://api.t.sina.com.cn/statuses/update.xml?";
httpRequest2.Request(sendUrl, "status=" + HttpUtility.UrlEncode(“微博内容”));
下面贴出代码:
using:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using LeoShi.Soft.OpenSinaAPI;
using System.Web;
using System.Threading;
读取配置文件并获取相应的accessToken,配置文件的格式为:username&password,每个这样的占一行,使用记事本写就行了。
private void btn_readIni_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txt_filePath.Text = openFileDialog1.FileName;
}
if (txt_filePath.Text != "")
{
FileStream fs = new FileStream(txt_filePath.Text, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
while(!sr.EndOfStream)
{
string str = sr.ReadLine();;
if (str == "")
continue;
string[] strArr = str.Split('&');
userInfoList.Add(new string[2] {strArr[0].Trim(),strArr[1].Trim()});
}
sr.Close();
fs.Close();
}
btn_send.Enabled = false;
initSina();
btn_send.Enabled = true;
lb_status.Text = "成功连接微博,初始化完成";
}
private void initSina()
{
for (int x = 0; x < userInfoList.Count; x++)
{
if (userInfoList[x][0]=="")
break;
var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET) as HttpGet;
httpRequest.GetRequestToken();
string url = httpRequest.GetAuthorizationUrl();
GlobalInfo.requestTokens[x] = httpRequest.Token;
GlobalInfo.requesTokenSecrets[x] = httpRequest.TokenSecret;
httpRequest.GetVerifier(userInfoList[x][ 0], userInfoList[x][ 1],x);
httpRequest.GetAccessToken();
GlobalInfo.requestTokens[x] = httpRequest.Token;
GlobalInfo.requesTokenSecrets[x] = httpRequest.TokenSecret;
}
}
发送微博:
private void btn_send_Click(object sender, EventArgs e)
{
Thread sendWeiboThread = new Thread(new ThreadStart(sendWeibo));
sendWeiboThread.Start();
}
private void sendWeibo()
{
Thread.CurrentThread.IsBackground = true;
Control.CheckForIllegalCrossThreadCalls = false;
btn_send.Enabled = false;
btn_readIni.Enabled = false;
for (int m = 0; m < int.Parse(txt_sendTimes.Text); m++)
{
times++;
for (int x = 0; x < userInfoList.Count; x++)
{
lb_status.Text = userInfoList[x][0] + " " + x.ToString() + "/" + userInfoList.Count;
var httpRequest2 = HttpRequestFactory.CreateHttpRequest(Method.POST);
httpRequest2.Token = GlobalInfo.requestTokens[x];
httpRequest2.TokenSecret = GlobalInfo.requesTokenSecrets[x];
var sendUrl = "http://api.t.sina.com.cn/statuses/update.xml?";
httpRequest2.Request(sendUrl, "status=" + HttpUtility.UrlEncode(txt_weibo.Text + " " + times.ToString()));
Thread.Sleep(150);
}
}
btn_send.Enabled = true;
btn_readIni.Enabled = true;
lb_status.Text = "发送完成。";
}
相关阅读 更多 +