/// <summary> /// 在已知道Access Token和Access Secret情况下调用API的示例 /// </summary> public static void Test() { //实例化OAuth对象 string appKey = "3ab2872742234704925c33dec507f9bb"; string appSecret = "f6cd03eb8734e8f64b98bef6ce8d546a"; OAuth oauth = new OAuth(appKey, appSecret); oauth.Token = "91c8a7555f694dd3bd7a55178dfa952d"; //Access Token oauth.TokenSecret = "2e01aa675faf5764d59564537f142f51"; //Access Secret //根据OAuth对象实例化API接口 Timeline api = new Timeline(oauth); var data = api.GetPublicTimeline(0, 10); Twitter twitter = new Twitter(oauth); //var data2 = twitter.Add("铁盒的钥匙我找不到!沉在盒子里的是你给我的快乐,我很想记得,可是我记不得", @"C:\Users\hooyes\Pictures\58.gif", "127.0.0.1"); var data2 = twitter.Add("让我们半兽人的眼神单纯。", "207.6.14.22"); if (data2.Ret == 0) { //删除刚发的微博 Console.WriteLine("发布成功"); } else { Console.WriteLine("发布失败"); } Console.ReadLine(); }
public static void Run() { try { //实例化OAuth对象 string appKey = Constant.app_key_QQ; string appSecret = Constant.app_secret_QQ; List<RelationEntity> lt = Relation.Get(); foreach (RelationEntity rt in lt) { #region 单个用户 try { Dictionary<string, string> Main = new Dictionary<string, string>(); Dictionary<string, string> Sub = new Dictionary<string, string>(); Main = Dict.Get(rt.App, rt.UserID); Sub = Dict.Get(rt.SubApp, rt.SubUserID); OAuth oauth = new OAuth(appKey, appSecret); oauth.Token = Main["Token"];// //"91c8a7555f694dd3bd7a55178dfa952d"; //Access Token oauth.TokenSecret = Main["TokenSecret"];// "2e01aa675faf5764d59564537f142f51"; //Access Secret Timeline api = new Timeline(oauth); //各取数据出来 long maxTimeline = Main.ContainsKey("MaxTimeline") ? (long)Convert.ToDecimal(Main["MaxTimeline"]) : 0; decimal MaxId = Sub.ContainsKey("MaxId") ? Convert.ToDecimal(Sub["MaxId"]) : 0; TimelineData data = null; try { data = api.GetBroadcast_timeline(PageFlag.First, 0, Constant.QQRequestNum); } catch (Exception ex) { log.Warn("{4},{0},{1},{2},{3}", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID); log.Warn("{0},{1}", ex.Message, ex.StackTrace); Console.WriteLine("-----"); Console.Write("{4},{0},{1},{2},{3},GetQQDataError", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID); } XmlDocument sinaT = null; try { sinaT = Task.GetSinaUserTimeline(Sub["Token"], Sub["TokenSecret"], MaxId.ToString()); } catch (Exception ex) { log.Warn("{4},{0},{1},{2},{3}", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID); log.Warn("{0},{1}", ex.Message, ex.StackTrace); Console.WriteLine("-----"); Console.Write("{4},{0},{1},{2},{3},GetSinaDataError", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID); } long maxTimelineTemp = maxTimeline; decimal MaxIdTemp = 0; if (data != null) { try { #region QQ to Sina if (data.Tweets.Length > 0) { //Master DictEntity dt = new DictEntity(); dt.App = rt.App; dt.Key = "MaxTimeline"; dt.UserID = rt.UserID; DictEntity dt2 = new DictEntity(); dt2.App = rt.SubApp; dt2.Key = "MaxId"; dt2.UserID = rt.SubUserID; for (int i = data.Tweets.Length; i > 0; i--) { try { OpenTSDK.Tencent.Objects.Tweet tw = data.Tweets[i - 1]; if (tw.Timestamp > maxTimeline) { string Token = Sub["Token"]; string TokenSecret = Sub["TokenSecret"]; decimal Tid = 0; string pic = string.Empty; //只发原创 if (tw.Type == 1) { if (string.IsNullOrEmpty(tw.Image)) { Tid = Sina(tw.Origtext, Token, TokenSecret); } else { pic = GetHttpFile(tw.Image + "/2000"); if (!string.IsNullOrEmpty(pic)) { Tid = Sina(tw.Origtext, Token, TokenSecret, pic); } else { Tid = Sina(tw.Origtext, Token, TokenSecret); } } } //dt.Value = tw.Timestamp.ToString(); if (maxTimelineTemp < tw.Timestamp) { maxTimelineTemp = tw.Timestamp; } if (MaxIdTemp < Tid) { MaxIdTemp = Tid; } } } catch (Exception ex) { log.Warn("{4},{0},{1},{2},{3}", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID); log.Warn("{0},{1}", ex.Message, ex.StackTrace); Console.Write(ex.Message); Console.Write("{4},{0},{1},{2},{3}", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID); } } if (maxTimeline < maxTimelineTemp) { dt.Value = maxTimelineTemp.ToString(); Dict.Save(dt); } if (MaxIdTemp > 0) { dt2.Value = MaxIdTemp.ToString(); Dict.Save(dt2); } } #endregion } catch (Exception ex) { log.Warn("{0},{1}", ex.Message, ex.StackTrace); } } #region Sina TO QQ try { if (sinaT != null) { long maxTimelineTemp2 = maxTimelineTemp; decimal MaxIdTemp2 = MaxIdTemp; XmlNodeList x = sinaT.SelectNodes("/statuses/status"); if (x.Count > 0) { DictEntity dt = new DictEntity(); dt.App = rt.App; dt.Key = "MaxTimeline"; dt.UserID = rt.UserID; DictEntity dt2 = new DictEntity(); dt2.App = rt.SubApp; dt2.Key = "MaxId"; dt2.UserID = rt.SubUserID; foreach (XmlNode xn in x) { decimal id = Util.GetXmlNodeValue<decimal>(xn.SelectSingleNode("id")); string text = Util.GetXmlNodeValue<string>(xn.SelectSingleNode("text")); string original_pic = string.Empty; try { original_pic = Util.GetXmlNodeValue<string>(xn.SelectSingleNode("original_pic")); if (!string.IsNullOrEmpty(original_pic)) { original_pic = GetHttpFile(original_pic); } } catch (Exception ex) { log.Warn("{0},{1}", ex.Message, ex.StackTrace); } XmlNode retweeted_status = xn.SelectSingleNode("//retweeted_status"); TweetOperateResult QQresult = null; //转播的不发啦 if (retweeted_status == null) { QQresult = Task.QQ(text, Main["Token"], Main["TokenSecret"], original_pic); if (maxTimelineTemp2 < QQresult.Timestamp) { maxTimelineTemp2 = QQresult.Timestamp; } } if (MaxIdTemp2 < (long)id) { MaxIdTemp2 = id; } } if (maxTimelineTemp2 > maxTimelineTemp) { dt.Value = maxTimelineTemp2.ToString(); Dict.Save(dt); } if (MaxIdTemp2 > MaxIdTemp) { dt2.Value = MaxIdTemp2.ToString(); Dict.Save(dt2); } } //----- } } catch (Exception ex) { log.Warn("{0},{1}", ex.Message, ex.StackTrace); } #endregion } catch (Exception ex) { Console.WriteLine(ex.Message); log.Warn("{4},{0},{1},{2},{3}", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID); log.Warn("{0},{1}", ex.Message, ex.StackTrace); } #endregion } } catch (Exception ex) { log.Warn("{0},{1}", ex.Message, ex.StackTrace); } }