C # Sina Weibo chatbot

Source: Internet
Author: User
Tags oauth

Through the sina Weibo api Group microblog, I used the sdk provided by sina and made minor changes to it, skipping the oauth page authentication. This sdk is easy to use. The following describes the implementation method and provides a reference for this purpose.

Because oauth page authentication is skipped, a user password needs to be sent once, which is not safe enough and should be used as appropriate.

I changed the sdk, copy the downloaded folder to the project: http://files.cnblogs.com/fmnisme/sinaApi.rar

Sina documentation center: http://open.weibo.com/wiki/index.php/%E9%A6%96%E9%A1%B5

The method of bypassing oauth page authentication using the blog garden akita method, thanks here, blog on this method: html "> http://www.cnblogs.com/btxakita/archive/2011/05/24/2055767.html

Sina has a limit on the number of api accesses:

 

That is to say, each account can send up to 30 Weibo posts per hour ....

Skd usage:

First, modify the appkey of the GlobalInfo class in the SDK. appSecret indicates the appKey and appSecret you applied for in sina;

Var httpRequest = HttpRequestFactory. CreateHttpRequest (Method. GET) as HttpGet;

// Because a group of blogs are sent, user information is stored in arrays. x represents the array subscript.

HttpRequest. GetRequestToken ();

String url = httpRequest. GetAuthorizationUrl ();

GlobalInfo. requestTokens [x] = httpRequest. Token;

GlobalInfo. requesTokenSecrets [x] = httpRequest. TokenSecret;

HttpRequest. GetVerifier ("user name [x]", "password [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 ("Weibo content"); the following code is provided:

Using:

Using System;

Using System. Collections. Generic;

Using System. Windows. Forms;

Using System. IO;

Using LeoShi. Soft. OpenSinaAPI;

Using System. Web;

Using System. Threading;

Read the configuration file and obtain the corresponding accessToken. The format of the configuration file is username & password. You can use NotePad to write each such line.

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 = "successfully connected to Weibo, initialization completed ";

}

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.