• C# WebForm实现发表腾讯微博


      开发环境:VS2010 .Net framework 4.0

       根据官方的API开发的一个发表腾讯微博的WebForm程序,可以根据这个Demo给网站开发类似的功能。

       开发前需要先引用官方API:QWeiboSDK。下载地址:http://wiki.open.t.qq.com/index.php/SDK%E4%B8%8B%E8%BD%BD#C.23_SDK

       若有什么问题,可以跟帖或者私信。


    需要在此处定义好在腾讯微博开发平台的AppKey以及AppSecret。

            /// <summary>
    /// 定义常量
    /// </summary>
    private const string strAppKey = "******";   //应用KEY
    private const string strAppSecret = "******"; //应用密钥


    定义需要用到的变量。

            //授权确认码
    private string oauthVerify = null;

    public string OauthVerify
    {
    get { return oauthVerify; }
    set { oauthVerify = value; }
    }

    //返回的request_token
    private string tokenKey = null;

    public string TokenKey
    {
    get { return tokenKey; }
    }

    //返回的request_secret
    private string tokenSecret = null;

    public string TokenSecret
    {
    get { return tokenSecret; }
    }

    //返回的accessToken
    private string accessToken = null;

    public string AccessToken
    {
    get { return accessToken; }
    set { accessToken = value; }
    }

    //返回的accessSecret
    private string accessSecret = null;

    public string AccessSecret
    {
    get { return accessSecret; }
    set { accessSecret = value; }
    }


    页面加载事件

            /// <summary>
    /// 页面加载
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
    //如果当前页面不是为了响应回发而加载
    if (!Page.IsPostBack)
    {
    //如果Request指定对象存在值
    if (Request["oauth_verifier"] != null)
    {
    //取得Oauth标识码
    OauthVerify = Request["oauth_verifier"].ToString();

    //将OautuVerify存入Session
    Session["oauthVerify"] = OauthVerify;

    //lblInfo.Text = "成功";
    }
    else
    {
    //如果成功取得返回的request_token
    if (GetRequestToken(strAppKey, strAppSecret))
    {
    //保存变量至Session
    Session["tokenKey"] = tokenKey;
    Session["tokenSecret"] = tokenSecret;

    //跳转至授权页面
    Response.Redirect("http://open.t.qq.com/cgi-bin/authorize?oauth_token=" + tokenKey);
    }
    }

    //如果尚未获取AccessToken以及AccessSecret
    if (Session["accessToken"] == null && Session["accessSecret"] == null)
    {
    //当取得Oauth标识码
    if (OauthVerify != null && Session["tokenKey"] != null && Session["tokenSecret"] != null)
    {
    //取得AccessToken
    bool answer = GetAccessToken(strAppKey, strAppSecret, Session["tokenKey"].ToString(), Session["tokenSecret"].ToString(), OauthVerify);

    //如果成功获取
    if (answer)
    {
    lblInfo.Text = "成功";

    //将AccessToken保存至Session.
    Session["accessToken"] = tokenKey;

    //将AccessSecret保存至Session.
    Session["accessSecret"] = tokenSecret;

    //发送微博按钮显示
    btnUpdate.Visible = true;
    }
    }
    }
    }
    }

    在callbackUrl需要设置好你网站的URL或者调试地址。

            /// <summary>
    /// 得到返回的request_token
    /// </summary>
    /// <param name="customKey">应用Key</param>
    /// <param name="customSecret">应用密钥</param>
    /// <returns>true 得到返回值 false 失败</returns>
    private bool GetRequestToken(string customKey, string customSecret)
    {
    //需要取得Token的地址
    string url = "https://open.t.qq.com/cgi-bin/request_token";
    //创建泛型集合
    List<QWeiboSDK.Parameter> parameters = new List<QWeiboSDK.Parameter>();
    //创建Oauth协议私密对象
    OauthKey oauthKey = new OauthKey();
    //存放应用Key
    oauthKey.customKey = customKey;
    //存在应用密钥
    oauthKey.customSecret = customSecret;
    //存放应用返回URL
    oauthKey.callbackUrl = "http://localhost:20595";
    //创建request对象
    QWeiboRequest request = new QWeiboRequest();

    //转换Token
    return ParseToken(request.SyncRequest(url, "GET", oauthKey, parameters, null));
    }
            /// <summary>
    /// 得到访问标识码
    /// </summary>
    /// <param name="customKey">应用Key</param>
    /// <param name="customSecret">应用密钥</param>
    /// <param name="requestToken">request_token</param>
    /// <param name="requestTokenSecrect">request_tokenSecrect</param>
    /// <param name="verify">确认码</param>
    /// <returns>true 成功 false 失败</returns>
    private bool GetAccessToken(string customKey, string customSecret, string requestToken, string requestTokenSecrect, string verify)
    {
    //需要取得AccessToken的地址
    string url = "https://open.t.qq.com/cgi-bin/access_token";
    //创建泛型数组
    List<QWeiboSDK.Parameter> parameters = new List<QWeiboSDK.Parameter>();
    //创建Oauth协议私密对象
    OauthKey oauthKey = new OauthKey();
    //存放应用Key
    oauthKey.customKey = customKey;
    //存在应用密钥
    oauthKey.customSecret = customSecret;
    //存放TokenKey
    oauthKey.tokenKey = requestToken;
    //存放TokenSecret
    oauthKey.tokenSecret = requestTokenSecrect;
    //存放确认码
    oauthKey.verify = verify;
    //创建request对象
    QWeiboRequest request = new QWeiboRequest();

    //转换Token
    return ParseToken(request.SyncRequest(url, "GET", oauthKey, parameters, null));
    }
            /// <summary>
    /// 发送微博
    /// </summary>
    private void Send()
    {
    //返回信息
    string strRet = "";
    //创建UTF8对象
    //UTF8Encoding utf8 = new UTF8Encoding();
    //创建泛型数组
    List<QWeiboSDK.Parameter> parameters = new List<QWeiboSDK.Parameter>();
    //创建Oauth协议私密对象
    OauthKey oauthKey = new OauthKey();
    //存放应用Key
    oauthKey.customKey = strAppKey;
    //存在应用密钥
    oauthKey.customSecret = strAppSecret;
    //存放AccessToken
    oauthKey.tokenKey = Session["accessToken"].ToString();
    //存放AccessSecret
    oauthKey.tokenSecret = Session["accessSecret"].ToString();

    //发送微博
    t twit = new t(oauthKey, "json");

    //取得返回值
    strRet = twit.add(txtMessage.Text, "127.0.0.1", "", "");

    lblInfo.Text = strRet;

    }
            /// <summary>
    /// 转换Token
    /// </summary>
    /// <param name="response">Token字符串</param>
    /// <returns>true 成功 false 失败</returns>
    private bool ParseToken(string response)
    {
    //判断参数是否为空
    if (string.IsNullOrEmpty(response))
    {
    //返回错误
    return false;
    }

    //根据指定符号进行分割
    string[] tokenArray = response.Split('&');

    //如果数组长度小于2
    if (tokenArray.Length < 2)
    {
    //返回错误
    return false;
    }

    //取得TokenKey
    string strTokenKey = tokenArray[0];
    //取得Token密钥
    string strTokenSecrect = tokenArray[1];

    //根据指定符号进行分割
    string[] token1 = strTokenKey.Split('=');

    //如果数组长度小于2
    if (token1.Length < 2)
    {
    //返回错误
    return false;
    }

    //取得TokenKey
    tokenKey = token1[1];

    //根据指定符号进行分割
    string[] token2 = strTokenSecrect.Split('=');

    //如果数组长度小于2
    if (token2.Length < 2)
    {
    //返回错误
    return false;
    }

    //取得Token密钥
    tokenSecret = token2[1];

    //返回正确
    return true;
    }





      




  • 相关阅读:
    查看真机的系统版本sdk
    查看真机的系统中sdk的版本
    用Appium去操作移动设备上的chrome
    adb devices 显示error
    创建一个web Test Plan
    Maven2的配置文件settings.xml(转)
    解决HierarchyViewer不能连接真机的问题
    利用Hudson持续集成来执行Android自动化测试(转)
    eclipse中修改maven仓储
    Appium的理念
  • 原文地址:https://www.cnblogs.com/chenkite/p/2356392.html
Copyright © 2020-2023  润新知