• .net程序调用新浪微博接口发微博


    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);
    
            }
        }
    }
    
    
    
     
  • 相关阅读:
    [论文笔记] On Construction of Cloud IaaS for VM Live Migration Using KVM and OpenNebula (ICA3PP, 2012)
    [论文笔记] Energyaware resource allocation heuristics for efficient management of data centers for Cloud computing (FGCS, 2012)
    [概念学习] hypervisor
    [论文泛读] Dynamic placement of virtual machines for managing sla violations (IM, 2007)
    [论文笔记] Costs of virtual machine live migration A survey (SERVICES, 2012)
    [论文笔记] Live Migration of Multiple Virtual Machines with Resource Reservation in Cloud Computing Environments (CLOUD, 2011)
    vscode如何搭建vue项目
    jenkins部署
    yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
    Spring 笔记——核心数据规则篇
  • 原文地址:https://www.cnblogs.com/vaejp/p/weibo.html
Copyright © 2020-2023  润新知