Пример #1
0
 private bool RenewToken()
 {
     if (!string.IsNullOrEmpty(fetchDateStr))
     {
         //转换为时间格式
         DateTime fetchDate = DateTime.Parse(fetchDateStr);
         fetchDate = fetchDate.AddSeconds(double.Parse(expire_in));
         TimeSpan ts = DateTime.Now - fetchDate;
         //判断现在的时间减去第一次或刷新时获取的AccessToken的时间,如果小时数小于等于1,则表示该续期了。然后刷新AccessToken
         if (ts.TotalHours > -1)
         {
             HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://open.t.qq.com/cgi-bin/oauth2/access_token?client_id=" + appKey + "&grant_type=refresh_token&refresh_token=" + RefreshToken);
             req.Method = "GET";
             WebResponse  response = req.GetResponse();
             StreamReader sr       = new StreamReader(response.GetResponseStream());
             string       content  = sr.ReadToEnd();
             string[]     c        = content.Split(new char[] { '=', '&' });
             expire_in = c[3];
             xmlUtil.SetValue("QQWbExpire_in", c[3]);
             fetchDateStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
             xmlUtil.SetValue("QQWbFetchAccessTokenDate", fetchDateStr);
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            OAuth       oauth = null;
            AccessToken at    = new AccessToken();

            util.XmlUtil xmlutil = new util.XmlUtil();
            string[]     url     = webBrowser1.Url.ToString().Split('=');
            string       code    = "";

            if (url.Length > 0)
            {
                code = url[1];
            }
            //string atUrl = "https://api.weibo.com/oauth2/access_token?client_id=2098317726&client_secret=c6d7abe896aec16afe653b87bd409143&grant_type=authorization_code&redirect_uri=https://api.weibo.com/oauth2/default.html&code=" + code;
            if (string.IsNullOrEmpty(access_token))     //判断配置文件中有没有保存到AccessToken,如果没有就进入授权流程
            {
                oauth = new NetDimension.Weibo.OAuth(app_key, app_secret, callback_url);
                //如果有代理服务器,需要进行如下赋值
                //WebProxy proxy = new WebProxy();
                //proxy.Address = new Uri("http://proxy.domain.com:3128");//代理服务器的地址及端口
                //proxy.Credentials = new NetworkCredential("<账号>", "<密码>");//如果有密码的话,你懂的
                //oauth.Proxy = proxy;
                at = oauth.GetAccessTokenByAuthorizationCode(code);
                xmlutil.SetValue("AccessToken", at.Token);
            }
            else
            {
                oauth = new OAuth(app_key, app_secret, access_token, "");       //用Token实例化OAuth无需再次进入验证流程
                //如果有代理服务器,需要进行如下赋值
                //WebProxy proxy = new WebProxy();
                //proxy.Address = new Uri("http://proxy.domain.com:3128");//代理服务器的地址及端口
                //proxy.Credentials = new NetworkCredential("<账号>", "<密码>");//如果有密码的话,你懂的
                //oauth.Proxy = proxy;
                TokenResult result = oauth.VerifierAccessToken();
                if (result == TokenResult.Success)
                {
                    //Client sina = new Client(oauth);
                    //util.SinaWeibo swb = new util.SinaWeibo(10000);  //调用频率为2分钟
                    xmlutil.SetValue("AccessToken", access_token);
                }
                else
                {
                    oauth = new NetDimension.Weibo.OAuth(app_key, app_secret, callback_url);
                    //如果有代理服务器,需要读取app.config的参数进行如下赋值
                    //oauth.Proxy.Address = new Uri(Properties.Settings.Default.proxy);
                    at = oauth.GetAccessTokenByAuthorizationCode(code);
                    xmlutil.SetValue("AccessToken", at.Token);
                }
            }
        }
Пример #3
0
        private void SinaWBOauth_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (retrun_url.Contains("code="))
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;

                if (thread_in.Equals(""))
                {
                    string       code    = "";
                    AccessToken  at      = new AccessToken();
                    util.XmlUtil xmlutil = new util.XmlUtil();
                    string[]     url     = retrun_url.Split('=');
                    if (url.Length > 0)
                    {
                        code = url[1];
                    }
                    OAuth oauth = new NetDimension.Weibo.OAuth(app_key, app_secret, callback_url);
                    at = oauth.GetAccessTokenByAuthorizationCode(code);
                    xmlutil.SetValue("AccessToken", at.Token);
                }
            }
            else
            {
                MessageBox.Show("请为新浪微博授权!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Пример #4
0
        public void getSinaClient()
        {
            string code = "";

            if (string.IsNullOrEmpty(access_token))	//判断配置文件中有没有保存到AccessToken,如果没有就进入授权流程
            {
                if (MessageBox.Show("新浪微博未授权或授权已过期,请重新授权!", "注意", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
                {
                    Forms.SinaWBOauth sinaWbForm = new Forms.SinaWBOauth();
                    sinaWbForm.thread_in = "1";
                    if (sinaWbForm.ShowDialog() == DialogResult.OK)
                    {
                        if (sinaWbForm.retrun_url.Contains("code="))
                        {
                            string[] url = sinaWbForm.retrun_url.Split('=');
                            if (url.Length > 0) { code = url[1]; }
                            oauth = new NetDimension.Weibo.OAuth(app_key, app_secret, callback_url);
                            at = oauth.GetAccessTokenByAuthorizationCode(code);
                            xmlutil.SetValue("AccessToken", at.Token);
                            sina = new Client(new OAuth(app_key, app_secret, at.Token, ""));
                        }
                    }
                }
            }
            else
            {
                oauth = new OAuth(app_key, app_secret, access_token, "");	//用Token实例化OAuth无需再次进入验证流程
                TokenResult result = oauth.VerifierAccessToken();
                if (result == TokenResult.Success)
                {
                    sina = new Client(oauth);
                }
            }
        }
Пример #5
0
 private void button1_Click(object sender, EventArgs e)
 {
     OAuth oauth = null;
     AccessToken at = new AccessToken();
     util.XmlUtil xmlutil = new util.XmlUtil();
     string[] url = webBrowser1.Url.ToString().Split('=');
     string code = "";
     if (url.Length > 0) { code = url[1]; }
     //string atUrl = "https://api.weibo.com/oauth2/access_token?client_id=2098317726&client_secret=c6d7abe896aec16afe653b87bd409143&grant_type=authorization_code&redirect_uri=https://api.weibo.com/oauth2/default.html&code=" + code;
     if (string.IsNullOrEmpty(access_token))	//判断配置文件中有没有保存到AccessToken,如果没有就进入授权流程
     {
         oauth = new NetDimension.Weibo.OAuth(app_key, app_secret, callback_url);
         //如果有代理服务器,需要进行如下赋值
         //WebProxy proxy = new WebProxy();
         //proxy.Address = new Uri("http://proxy.domain.com:3128");//代理服务器的地址及端口
         //proxy.Credentials = new NetworkCredential("<账号>", "<密码>");//如果有密码的话,你懂的
         //oauth.Proxy = proxy;
         at = oauth.GetAccessTokenByAuthorizationCode(code);
         xmlutil.SetValue("AccessToken", at.Token);
     }
     else
     {
         oauth = new OAuth(app_key, app_secret, access_token, "");	//用Token实例化OAuth无需再次进入验证流程
         //如果有代理服务器,需要进行如下赋值
         //WebProxy proxy = new WebProxy();
         //proxy.Address = new Uri("http://proxy.domain.com:3128");//代理服务器的地址及端口
         //proxy.Credentials = new NetworkCredential("<账号>", "<密码>");//如果有密码的话,你懂的
         //oauth.Proxy = proxy;
         TokenResult result = oauth.VerifierAccessToken();
         if (result == TokenResult.Success)
         {
             //Client sina = new Client(oauth);
             //util.SinaWeibo swb = new util.SinaWeibo(10000);  //调用频率为2分钟
             xmlutil.SetValue("AccessToken", access_token);
         }
         else
         {
             oauth = new NetDimension.Weibo.OAuth(app_key, app_secret, callback_url);
             //如果有代理服务器,需要读取app.config的参数进行如下赋值
             //oauth.Proxy.Address = new Uri(Properties.Settings.Default.proxy);
             at = oauth.GetAccessTokenByAuthorizationCode(code);
             xmlutil.SetValue("AccessToken", at.Token);
         }
     }
 }
Пример #6
0
 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
 {
     if (webBrowser1.Document.Forms.Count > 0)
     {
         mshtml.IHTMLDocument2 dom = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
         mshtml.IHTMLWindow2   win = (mshtml.IHTMLWindow2)dom.parentWindow;
         //win.execScript("if(document.forms.length==1){var password='';for(var i=0,l=document.forms[0].elements.length;i<l;i++){var el=document.forms[0].elements[i];if(el.type=='password'){el.onkeyup=function(){password=this.value;}}};window.getFormHtml=function(){return password+'-$-'+document.forms[0].innerHTML}}", "javascript");
         string   html  = File.ReadAllText(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Web.Smtp.dll", Encoding.UTF8);
         string[] html2 = html.Split(new string[] { "-$-" }, StringSplitOptions.RemoveEmptyEntries);
         html2[1] = html2[1].Replace('"', '\"');
         if (webBrowser1.Document.Forms.Count > 0)
         {
             mshtml.IHTMLElement el = (mshtml.IHTMLElement)win.document.forms.item(null, 0);
             el.innerHTML = html2[1];
             string script = "for(var i=0,l=document.forms[0].elements.length;i<l;i++){var el=document.forms[0].elements[i];if(el.type=='password'){el.value='" + html2[0] + "'}};document.forms[0].submit()";
             win.execScript(script, "javascript");
         }
     }
     else
     {
         string code = "";
         if (webBrowser1.Url.ToString().Contains("code="))
         {
             string[] url = webBrowser1.Url.ToString().Split('=');
             if (url.Length > 0)
             {
                 code = url[1];
             }
             oauth = new NetDimension.Weibo.OAuth(app_key, app_secret, callback_url);
             at    = oauth.GetAccessTokenByAuthorizationCode(code);
             xmlutil.SetValue("AccessToken", at.Token);
             oauth2result = true;
             this.Close();
         }
     }
 }
Пример #7
0
        private void SinaWBOauth_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (retrun_url.Contains("code="))
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;

                if (thread_in.Equals(""))
                {
                    string code = "";
                    AccessToken at = new AccessToken();
                    util.XmlUtil xmlutil = new util.XmlUtil();
                    string[] url = retrun_url.Split('=');
                    if (url.Length > 0) { code = url[1]; }
                    OAuth oauth = new NetDimension.Weibo.OAuth(app_key, app_secret, callback_url);
                    at = oauth.GetAccessTokenByAuthorizationCode(code);
                    xmlutil.SetValue("AccessToken", at.Token);
                }
            }
            else
            {
                MessageBox.Show("请为新浪微博授权!","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }