public Tweet[] GetTweets(string userId) { string address = $"https://api.twitter.com/2/timeline/profile/{userId}.json?tweet_mode=extended&simple_quoted_tweet=true"; string string3; try { lock (_webClient) { string3 = Encoding.UTF8.GetString(_webClient.DownloadData(address)); } } catch (WebException e) { LogHelp.WriteLine(e.ToString()); if (int.TryParse(e.Response.Headers["status"], out int status)) { LogHelp.WriteLine(e.ToString()); LogHelp.WriteLine("状态码", status.ToString()); } GetToken(); return(null); } var json = JObject.Parse(string3); List <Tweet> tweets = new List <Tweet>(); foreach (var item in json["timeline"]["instructions"][0]["addEntries"]["entries"]) { var entryId = item["entryId"].ToString(); if (entryId.Contains("tweet")) { var id = item["sortIndex"].ToString(); ParseTweet(json, id, out Tweet tweet); tweets.Add(tweet); } } return(tweets.ToArray()); }
protected void GetAuthorization() { string @string; try { lock (_webClient) { _webClient.Headers.Add(_header); @string = Encoding.UTF8.GetString(_webClient.DownloadData("https://twitter.com/")); } } catch (WebException e) { LogHelp.WriteLine(e.ToString()); if (e.Status == WebExceptionStatus.ProtocolError) { LogHelp.WriteLine("进入等待"); Task.Delay(5 * 60 * 1000).Wait(); } return; } Regex regex = new Regex("https([^\"]*?)/main\\.([^\"]*?)\\.js"); string text = regex.Match(@string).Value; string string2; lock (_webClient) { string2 = Encoding.UTF8.GetString(_webClient.DownloadData(text)); } Regex regex2 = new Regex("\"(A{5,}.*?)\""); string value = "Bearer " + regex2.Match(string2).Groups[1].Value; _webClient.Headers["authorization"] = value; Path = new Regex("queryId:\"([a-zA-Z0-9-_]*?)\",operationName:\"UserByScreenName\"").Match(string2) .Groups[1].Value; }