上一篇:新浪微博PC客户端(DotNet WinForm版)—— 初探
说明一下:只是兴趣,并不是想发布为一个软件,说实在的,如果要作为一个软件发布,要做的工作还有很多。
新浪微博API地址:http://open.t.sina.com.cn/wiki/index.php/API%E6%96%87%E6%A1%A3?retcode=0。
目前提供的SDK:
其它的不清楚,C#的还不完善,而且不是官方的。
当前已实现的功能:
1、HTTP普通鉴权(Basic Authentication)的身份验证方式,说白了就是每次访问API都要发送帐号和密码,当然是不安全的,但是相比OAuth验证方式门槛要低的多。要使用OAuth验证方式可以去看下SDK。
===》account/verify_credentials 验证当前用户身份是否合法
OAuth是一种国际通用的授权方式,它的特点是不需要用户在第三方应用输入用户名及密码。OAuth的技术说明可参看官方网站 http://oauth.net。学习和研究OAuth的意义不仅仅在于新浪围脖,而是对以后开发类似的项目非常有意义和价值(如果你还不知道OAuth授权方式)。
验证通过则返回用户的个人信息,参照下面的API字段说明。
上一篇说了,API返回的结果格式有两种:XML和JSON,在调用API的时候可以指定。
这里我指定的是XML格式,实现代码如下:
登录窗体:FrmLogin.cs,可以看上一篇的截图。
(1)把申请AppKey和AppSecret写到app.config
<configuration>
<appSettings>
<add key="AppKey" value="3476523072"/>
<add key="AppSecret" value="3c909770efa6865fd5843a564545826d"/>
</appSettings>
</configuration>
要申请AppKey,需要登录新浪围脖,然后在这个地址创建应用并生成: http://open.t.sina.com.cn/apps/new。
(2)创建了一个静态类,保存用户信息
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace MBClient.Entities
{
/// <summary>
/// 微博帐号资料
/// </summary>
internal class UserInfo
{
internal UserInfo(){ }
internal static string UserName { get; set; }
internal static string Password { get; set; }
internal static string UserNamePassword { get; set; }
internal static int ID { get; set; }
internal static string ScreenName { get; set; }
internal static string Name { get; set; }
internal static int Province { get; set; }
internal static int City { get; set; }
internal static string Location { get; set; }
internal static string Description { get; set; }
internal static string Url { get; set; }
internal static string ProfileImageUrl { get; set; }
internal static string Domain { get; set; }
internal static string Gender { get; set; }
/// <summary>
/// 粉丝数
/// </summary>
internal static int FollowersCount { get; set; }
/// <summary>
/// 关注数
/// </summary>
internal static int FriendsCount { get; set; }
/// <summary>
/// 微博数
/// </summary>
internal static int StatusesCount { get; set; }
internal static int FavouritesCount { get; set; }
/// <summary>
/// 微博帐号日期
/// </summary>
internal static string CreatedAt { get; set; }
internal static bool Following { get; set; }
internal static bool Verified { get; set; }
internal static bool AllowAllActMsg { get; set; }
internal static bool GeoEnabled { get; set; }
}
}