public Form1() { InitializeComponent(); appKey = Properties.Settings.Default.AppKey; appSecret = Properties.Settings.Default.AppSecret; accessToken = Properties.Settings.Default.AcessToken; serialReadFlag = false; OAuth oauth = null; sina = null; if (string.IsNullOrEmpty(accessToken))//如果本地未保存accessToken,这里没有做过期判断2012-10-5 { oauth = Authorize(); if (!string.IsNullOrEmpty(oauth.AccessToken)) { Properties.Settings.Default.AcessToken = oauth.AccessToken; Properties.Settings.Default.Save(); statTxt.Text = "授权成功,保存accesstoken"; sina = new Client(oauth); } else { statTxt.Text = "授权失败,未获取accesstoken"; } } else { oauth = new OAuth(appKey, appSecret, accessToken, "");//最后一个参数是refreshtoken这个是面向高级用户对的 TokenResult tokenResult = oauth.VerifierAccessToken(); if (tokenResult == TokenResult.Success) { statTxt.Text = "验证本地accesstoken有效"; sina = new Client(oauth); } else { Properties.Settings.Default.AcessToken = string.Empty; Properties.Settings.Default.Save(); statTxt.Text = "验证本地accesstoken无效,尝试重新授权"; oauth = Authorize(); if (!string.IsNullOrEmpty(oauth.AccessToken)) { Properties.Settings.Default.AcessToken = oauth.AccessToken; Properties.Settings.Default.Save(); statTxt.Text = "授权成功,重新保存accesstoken"; sina = new Client(oauth); } else { statTxt.Text = "授权失败,未获取accesstoken"; } } } }
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); } } }
public static Client getOAuth() { OAuth oauth = null; Properties.WeiboOAuth.Default.AccessToken = string.Empty; Properties.WeiboOAuth.Default.Save(); string accessToken = Properties.WeiboOAuth.Default.AccessToken; if (string.IsNullOrEmpty(accessToken)) //判断配置文件中有没有保存到AccessToken,如果没有就进入授权流程 { oauth = Authorize(); if (!string.IsNullOrEmpty(oauth.AccessToken)) { Console.WriteLine("Get AccessToken{{{0}}} success!", oauth.AccessToken); Console.Write("Save Token to local in order to launch automaticaly next time? [Y/N]: Y"); /* if (Console.ReadKey(true).Key == ConsoleKey.Y) * */ { Properties.WeiboOAuth.Default.AccessToken = oauth.AccessToken; Properties.WeiboOAuth.Default.Save(); //配置文件已保存。如果要撤销AccessToken请删除ConsoleApp.exe.config中AcceessToken节点中的Token值。 Console.WriteLine(); Console.WriteLine("Token Saved."); } /* else { Console.WriteLine(); Console.WriteLine("AccessToken unsaved."); } */ } } else //如果配置文件中保存了AccesssToken { Console.WriteLine("Get saved AccessToken{{{0}}}!", accessToken); oauth = new OAuth(AppKey, AppSecret, accessToken, ""); //用Token实例化OAuth无需再次进入验证流程 Console.Write("Checking the saved Token ... "); TokenResult result = oauth.VerifierAccessToken(); //测试保存的AccessToken的有效性 if (result == TokenResult.Success) { Console.WriteLine("Done!"); /* Console.Write("Delete the saved token in order to OAuth next time?[Y/N]:"); if (Console.ReadKey(true).Key == ConsoleKey.Y) { //如果想演示下登录授权,那就得把配置文件中的Token删除,所以做那么一个判断。 Properties.WeiboOAuth.Default.AccessToken = string.Empty; Properties.WeiboOAuth.Default.Save(); //已从配置文件移除AccessToken值,重新运行程序来演示授权过程。 Console.WriteLine(); Console.WriteLine("Access Token Removed"); } // */ } else { Console.WriteLine("Error! Message:{0}", result); Properties.WeiboOAuth.Default.AccessToken = string.Empty; Properties.WeiboOAuth.Default.Save(); //已从配置文件移除AccessToken值,重新运行程序获取有效的AccessToken Console.WriteLine("Access Token Deleted, Please run again."); Console.ReadKey(); return null; //AccessToken无效,继续执行没有任何意义,反正也无法调用API } } //好吧,授权至此应该成功了。下一步调用接口吧。 return new Client(oauth); }
/// <summary> /// 初始化Weibo /// </summary> /// <returns></returns> private static void InitWeiboOAuth(string APPKEY, string APPSECRET, string ACCESSTOKEN) { OAUTH = new OAuth(APPKEY, APPSECRET, accessToken: ACCESSTOKEN); if (OAUTH.VerifierAccessToken() == TokenResult.Success)//验证AccessToken是否有效 { SINA = new NetDimension.Weibo.Client(OAUTH); Console.WriteLine(OAUTH.AccessToken); //还是来打印下AccessToken看看与前面方式获取的是不是一样的 //streamWriter.WriteLine(OAUTH.AccessToken); } }
public void getSinaClientAuto() { access_token = xmlutil.GetValue("AccessToken"); oauth = new OAuth(app_key, app_secret, access_token, ""); //用Token实例化OAuth无需再次进入验证流程 TokenResult result = oauth.VerifierAccessToken(); if (result == TokenResult.Success) { sina = new Client(oauth); } }
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); } } }