通過新浪微博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 的方法,在此感謝,關於該方法的博客:html">http://www.cnblogs.com/btxakita/archive/2011/05/24/2055767.html
新浪對api訪問次數有限制:
也就是說:每個賬號每小時最多發30條微博。。。。
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; &nbs