helper类里的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NetDimension;
using NetDimension.Weibo.Entities;
using NetDimension.Json;
using NetDimension.Weibo;
using System.Text;
using System.Net;
using System.Configuration;
/// <summary>
///WeiboHelper 的摘要说明
/// </summary>
public class WeiboHelper
{
public WeiboHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
private static string _appkey;
public static string Appkey
{
get
{
return ConfigurationManager.AppSettings["Appkey"];
}
}
private static string _Secret;
public static string Secret
{
get
{
return ConfigurationManager.AppSettings["Secret"];
}
}
private static string _CallBackUrl;
public static string CallBackUrl
{
get
{
return ConfigurationManager.AppSettings["CallBackUrl"];
}
set { WeiboHelper._CallBackUrl = value; }
}
static HttpContext context;
/// <summary>
/// 发送微博方法
/// </summary>
/// <param name="Msg">要发送的消息字符窜 最多不能超过140字 </param>
/// <param name="Code">用户授权之后的授权码</param>
/// <returns></returns>
public static void Send(string Msg, string Code)
{
var token = HttpContext.Current.Session["token"] as NetDimension.Weibo.OAuth;
NetDimension.Weibo.Client c = new NetDimension.Weibo.Client(token);
c.API.Entity.Statuses.Update(Msg, 90, 90, "");
}
/// <summary>
/// 发送一条带照片的微博
/// </summary>
/// <param name="Msg">要发送的消息字符窜 最多不能超过140字 </param>
/// <param name="Code">用户授权之后的授权码</param>
/// <returns></returns>
public static void SendandPic(string Msg, string Code ,byte[] TT )
{
var token = HttpContext.Current.Session["token"] as NetDimension.Weibo.OAuth;
NetDimension.Weibo.Client c = new NetDimension.Weibo.Client(token);
c.API.Entity.Statuses.Upload(Msg, TT, 140, 140, "");
}
/// <summary>
/// 设置Token
/// </summary>
/// <param name="code"></param>
public static void SetToken(string code)
{
if (HttpContext.Current.Session["token"] == null)
{
NetDimension.Weibo.OAuth a = new NetDimension.Weibo.OAuth(Appkey, Secret, CallBackUrl);
a.GetAccessTokenByAuthorizationCode(code);
HttpContext.Current.Session["token"] = a;
}
}
}
调用: 前题是导入新浪微博的SDK
///// <summary>
///// 常量 微博授权服务地址
///// </summary>
public const string callback = "https://api.weibo.com/oauth2/authorize?client_id={0}&response_type=code&redirect_uri={1}";
string code = "";
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString.Count > 0)
{
code = Request.QueryString["code"];
Session["code"] = code;
WeiboHelper.SetToken(code);
}
GetCode();
}/// <summary>
/// 打开微博授权页面获取code
/// </summary>
private void GetCode()
{
//判断是否存在code 授权码
if (Session["code"] == null)
{
//参数见配置文件
string callurl = string.Format(callback, ConfigurationManager.AppSettings["AppKey"], ConfigurationManager.AppSettings["CallBackUrl"]);
string test = string.Format("<script>window.location = '{0}'</script>", callurl);
Response.Write(test);
}
}
}