------------------ Windows Phone 7手机开发 Net培训、期待与您交流! ----------------------
这是 本人学习 windowsphone 与silverlight 一段的时间 后 一个练笔的小demo 腾讯微博客户端
有三大收获
一大收获是oauth认证 oauth认证基本是开发各大开放平台 所必要的 认证机制 腾讯认证的了解
恰好为开放各大应用的奠定了坚实的基础 其中源代码如下
/// <summary>
/// Oauth授权对象
/// </summary>
public class OAuth
{
/// <summary>
/// 根据app_key与app_secret实例化
/// </summary>
public OAuth(string appKey, string appSecret)
{
this.AppKey = appKey;
this.AppSecret = appSecret;
this.Charset = Encoding.UTF8;
}
/// <summary>
/// app_key
/// </summary>
public string AppKey { get; set; }
/// <summary>
/// app_secret
/// </summary>
public string AppSecret { get; set; }
/// <summary>
/// oauth_token, 根据不同的场合使用不同的值,如request_token或access_token
/// </summary>
public string Token { get; set; }
/// <summary>
/// oauth_token_secret, 根据不同的场合使用不同的值,如request_secret或access_secret
/// </summary>
public string TokenSecret { get; set; }
/// <summary>
/// 编码
/// </summary>
public Encoding Charset { get; set; }
/// <summary>
/// 授权过程中最后一次发生的错误
/// </summary>
public Exception LastError { get; set; }
/// <summary>
/// 使用默认的API地址同步获取未授权的Request Token.
/// 如果获取成功, 对象实例的Token与TokenSecret属性值将改为request_token与request_secret
/// </summary>
/// <param name="callback">返回地址</param>
/// <returns>是否获取request token成功</returns>
public void GetRequestToken(Action<string> callback)
{
this.GetRequestToken("https://open.t.qq.com/cgi-bin/request_token",callback);
}
/// <summary>
/// 获取未授权的Request Token。
/// 如果未去成功, 对象实例的Token与TokenSecret属性值将改为request_token与request_secret
/// </summary>
/// <param name="requestUrl">API请求地址</param>
/// <param name="callback">返回函数</param>
/// <returns>是否获取request token成功</returns>
public void GetRequestToken(string requestUrl, Action<string> callback)
{
this.Token = string.Empty;
this.TokenSecret = string.Empty;
var parameters = new Parameters();
parameters.Add("oauth_callback", "null");
this.GetOAuthToken(requestUrl, parameters, EndRequestToken, callback);
}
/// <summary>
/// 获取未授权的Request Token结束
/// </summary>
/// <param name="response">响应后的字符串</param>
/// <param name="callback">响应的回调的函数</param>
private void EndRequestToken(string response, Action<string> callback)
{
if (!string.IsNullOrEmpty(response))
{
var responseData = Util.GetQueryParameters(response);
this.Token = responseData["oauth_token"];
this.TokenSecret = responseData["oauth_token_secret"];
callback(this.Token);
}
else
{
if (callback != null)
{
callback("");
}
}
}
/// <summary>
/