博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QQ微博登陆封装
阅读量:4353 次
发布时间:2019-06-07

本文共 6001 字,大约阅读时间需要 20 分钟。

最近想搞写一个基于QQ微博的桌面应用,按照官方的介绍,我使用C#的SDK进行开发,但是发现这个SDK关于登陆的地方很粗糙,另外发现通过OAuth的方式登陆的话,需要用户去填写获取到的verifer字符串。 研究了一下,发现C#里面可以通过一个Webbrowser来解决这个问题。基于这个机制,我写了一个登陆的辅助类。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading;namespace QQWeiboApi{    public partial class FrmTencentAuth : Form    {        private volatile bool is_run;        public FrmTencentAuth()        {            InitializeComponent();        }        public string GetOauthVerifier(IWin32Window win, string tokenKey, string callbackurl)        {            string url = "http://open.t.qq.com/cgi-bin/authorize?oauth_token=" + tokenKey;            is_run = true;            webBrowser1.Navigate(url);            this.Show(win);            while (is_run == true && (webBrowser1.Url == null || ! webBrowser1.Url.ToString().StartsWith(callbackurl)))            {                Application.DoEvents();            }            if (is_run == false)            {                return null;            }            url = webBrowser1.Url.ToString();            this.Close();            string sl = "oauth_verifier=";            return url.Substring(url.IndexOf(sl) + sl.Length);        }        private void FrmTencentAuth_FormClosing(object sender, FormClosingEventArgs e)        {            is_run = false;        }    }}
  上面是验证form的一部分,下面的是处理登陆的代码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using QWeiboSDK;using System.Collections.Specialized;using System.Reflection;namespace QQWeiboApi{    public class QWeiboLoginHelper    {        private string appKey = null;        private string appSecret = null;        private string accessKey = null;        private string accessSecret = null;        private string requestKey = null;        private string requestSecret = null;        private string verifier = null;        private string callbackurl = "http://www.smallbridge.co.cc";        public QWeiboLoginHelper(string appKey, string appSecret)        {            this.appKey = appKey;            this.appSecret = appSecret;        }        ///         /// 登陆        ///         /// 应用的key        /// 应用secret        public void Login()        {            GetRequestToken(appKey, appSecret);            GetUserAuth();            GetAccessToken();        }        public OauthKey AuthKey        {            get            {                if (appKey == null || appSecret == null || accessKey == null || accessSecret == null)                {                    return null;                }                return new OauthKey() { customKey = appKey, customSecret = appSecret, tokenKey = accessKey, tokenSecret = accessKey };            }        }        private void GetAccessToken()        {            string url = "https://open.t.qq.com/cgi-bin/access_token";            List
parameters = new List
(); OauthKey oauthKey = new OauthKey(); oauthKey.customKey = appKey; oauthKey.customSecret = appSecret; oauthKey.tokenKey = requestKey; oauthKey.tokenSecret = requestSecret; oauthKey.verify = verifier; try { QWeiboRequest request = new QWeiboRequest(); string result = request.SyncRequest(url, "GET", oauthKey, parameters, null); NameValueCollection nvs = ParseKVPairs(result); accessKey = nvs["oauth_token"]; accessSecret = nvs["oauth_token_secret"]; if (accessKey == null || accessSecret == null) { throw new Exception("get null accessKey or accessSecret"); } } catch (Exception ex) { throw new Exception("get accessKey and accessSecret error", ex); } } private void GetUserAuth() { FrmTencentAuth frmau = new FrmTencentAuth(); try { verifier = frmau.GetOauthVerifier(null, requestKey, callbackurl); if (verifier == null) { throw new Exception("get null user oauth verifier"); } } catch (Exception ex) { throw new Exception("get user oauth verifier error", ex); } } private void GetRequestToken(string appKey, string appSecret) { string url = "https://open.t.qq.com/cgi-bin/request_token"; List
parameters = new List
(); OauthKey oauthKey = new OauthKey(); oauthKey.customKey = appKey; oauthKey.customSecret = appSecret; oauthKey.callbackUrl = callbackurl; try { QWeiboRequest request = new QWeiboRequest(); string result = request.SyncRequest(url, "GET", oauthKey, parameters, null); NameValueCollection nvs = ParseKVPairs(result); requestKey = nvs["oauth_token"]; requestSecret = nvs["oauth_token_secret"]; if (requestKey == null || requestSecret == null) { throw new Exception("get null request key or secret"); } } catch (Exception ex) { throw new Exception("get request key and secret error", ex); } } private NameValueCollection ParseKVPairs(string str) { NameValueCollection nvs = new NameValueCollection(); foreach (string kv in str.Split('&')) { string[] kvs = kv.Split('='); if (kvs.Length == 2) { nvs.Add(kvs[0], kvs[1]); } } return nvs; } }}
  代码下载,请点 。  

转载于:https://www.cnblogs.com/youwang/archive/2011/12/22/2298619.html

你可能感兴趣的文章
14软件G2班
查看>>
bzoj 1977 [BeiJing2010组队]次小生成树 Tree
查看>>
bzoj 2119 股市的预测——枚举长度的关键点+后缀数组
查看>>
maven:新建的maven工程需要添加一下插件
查看>>
改变和恢复view的方向
查看>>
C#调用金数据API
查看>>
Convert Sorted List to Binary Search Tree
查看>>
Leetcode:Unique Binary Search Trees
查看>>
D3.js 绘制散点图
查看>>
HTML—链接
查看>>
将进程设置为守护进程
查看>>
用连接池提高Servlet访问数据库的效率
查看>>
luogu P1494 [国家集训队]小Z的袜子 ( 普 通 )
查看>>
树的数据结构
查看>>
MyEclipse导入Color Theme
查看>>
Vue开发微信H5 微信分享签名失败问题解决方案
查看>>
Linux - 配置SSH免密通信 - “ssh-keygen”的基本用法
查看>>
Python(2.7.6) glob - 匹配指定模式的文件
查看>>
HTTP - 持久连接
查看>>
添加路由时啥时候是dev啥时候是gw
查看>>