public async static Task <OAuthTokens> GetAccessToken(string accessToken, string accessTokenSecret, string oauthVerifier) { return(await RequestHandler(async() => { const string requestTokenUrl = "https://api.twitter.com/oauth/access_token"; var nonce = OAuth.Nonce(); var timestamp = OAuth.TimeStamp(); var parameters = new[] { new[] { "oauth_verifier", oauthVerifier } }; var signature = OAuth.Signature("POST", requestTokenUrl, nonce, timestamp, accessToken, accessTokenSecret, parameters); var authorizationHeader = OAuth.AuthorizationHeader(nonce, timestamp, accessToken, signature, parameters); var request = System.Net.WebRequest.Create(new Uri(requestTokenUrl)); request.Method = "POST"; request.Headers.Add("Authorization", authorizationHeader); using (var response = await request.GetResponseAsync()) { using (var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { var tokens = stream.ReadToEnd().Split('&'); var oauthTokens = new OAuthTokens { OAuthToken = Token(tokens[0]), OAuthSecret = Token(tokens[1]), UserId = Token(tokens[2]), ScreenName = Token(tokens[3]) }; return oauthTokens; } } })); }
public async static Task <OAuthTokens> GetRequestToken() { return(await RequestHandler(async() => { const string requestTokenUrl = "https://api.twitter.com/oauth/request_token"; var nonce = OAuth.Nonce(); var timestamp = OAuth.TimeStamp(); var parameters = new[] { new[] { "oauth_callback", "oob" } }; var signature = OAuth.Signature("POST", requestTokenUrl, nonce, timestamp, "", "", parameters); var authorizationHeader = OAuth.AuthorizationHeader(nonce, timestamp, null, signature, parameters); var request = System.Net.WebRequest.Create(new Uri(requestTokenUrl)); request.Method = "POST"; request.Headers.Add("Authorization", authorizationHeader); using (var response = await request.GetResponseAsync()) { using (var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { var body = stream.ReadToEnd(); var tokens = body.Split('&'); var oauthToken = Token(tokens[0]); var oauthSecret = Token(tokens[1]); var callbackConfirmed = Token(tokens[2]); if (callbackConfirmed != "true") { throw new InvalidProgramException("callback token not confirmed"); } return new OAuthTokens { OAuthToken = oauthToken, OAuthSecret = oauthSecret }; } } })); }
public async static Task <string> UpdateStatusWithMedia(string message, string filename) { return(await RequestHandler(async() => { var media = File.ReadAllBytes(filename); var mediaName = Path.GetFileName(filename); const string url = "https://api.twitter.com/1.1/statuses/update_with_media.json"; var oauth = new OAuth(); var nonce = OAuth.Nonce(); var timestamp = OAuth.TimeStamp(); var signature = OAuth.Signature("POST", url, nonce, timestamp, oauth.AccessToken, oauth.AccessTokenSecret, null); var authorizeHeader = OAuth.AuthorizationHeader(nonce, timestamp, oauth.AccessToken, signature); var request = System.Net.WebRequest.Create(new Uri(url)); request.Headers.Add("Authorization", authorizeHeader); request.Method = "POST"; var formDataBoundary = String.Format("{0:N}", Guid.NewGuid()); var contentType = "multipart/form-data; boundary=" + formDataBoundary; request.ContentType = contentType; using (var requestStream = request.GetRequestStream()) { var header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n", formDataBoundary, "status"); var footer = string.Format("\r\n--{0}--\r\n", formDataBoundary); WriteStream(requestStream, header); WriteStream(requestStream, message); header = string.Format( "\r\n--{0}\r\nContent-Type: application/octet-stream\r\n" + "Content-Disposition: form-data; name=\"media[]\"; filename=\"{1}\"\r\n\r\n", formDataBoundary, mediaName); WriteStream(requestStream, header); requestStream.Write(media, 0, media.Length); WriteStream(requestStream, footer); } using (var response = await request.GetResponseAsync()) { using (var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { var result = stream.ReadToEnd(); return result; } } }, () => string.Empty)); }
private async static Task <string> WebRequest(string url, IEnumerable <string[]> parameters, bool post = false) { var oauth = new OAuth(); var nonce = OAuth.Nonce(); var timestamp = OAuth.TimeStamp(); var signature = OAuth.Signature(post ? "POST" : "GET", url, nonce, timestamp, oauth.AccessToken, oauth.AccessTokenSecret, parameters); var authorizeHeader = OAuth.AuthorizationHeader(nonce, timestamp, oauth.AccessToken, signature); var parameterStrings = parameters.Select(p => string.Format("{0}={1}", OAuth.UrlEncode(p[0]), OAuth.UrlEncode(p[1]))).ToList(); if (!post) { url += "?" + string.Join("&", parameterStrings); } var request = WebRequestWrapper.Create(new Uri(url)); request.Headers.Add("Authorization", authorizeHeader); request.Method = post ? "POST" : "GET"; if (post) { Trace.TraceInformation(string.Join("&", parameterStrings)); request.ContentType = "application/x-www-form-urlencoded"; if (parameters != null) { using (var requestStream = request.GetRequestStream()) { WriteStream(requestStream, string.Join("&", parameterStrings)); } } } using (var response = await request.GetResponseAsync()) { using (var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { var result = stream.ReadToEnd(); return(result); } } }
public static void User(CancellationToken cancelationToken) { Task.Run(() => { while (cancelationToken.IsCancellationRequested == false) { var delay = Task.Delay(30 * 1000, cancelationToken); delay.Wait(cancelationToken); if (delay.IsCanceled || delay.IsFaulted) { break; } Trace.TraceInformation("{ Start Twitter User Stream }"); const string url = "https://userstream.twitter.com/1.1/user.json"; var oauth = new OAuth(); var nonce = OAuth.Nonce(); var timestamp = OAuth.TimeStamp(); var signature = OAuth.Signature("GET", url, nonce, timestamp, oauth.AccessToken, oauth.AccessTokenSecret, null); var authorizeHeader = OAuth.AuthorizationHeader(nonce, timestamp, oauth.AccessToken, signature); var request = WebRequestWrapper.Create(new Uri(url)); request.Headers.Add("Authorization", authorizeHeader); try { using (var response = request.GetResponse()) using (var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { stream.BaseStream.ReadTimeout = 60 * 1000; while (true) { var json = stream.ReadLine(); if (json == null) { Trace.TraceInformation("{ null }"); break; } if (cancelationToken.IsCancellationRequested) { break; } Trace.TraceInformation(string.IsNullOrWhiteSpace(json) ? "{ Blankline }" : json); var serializer = new JavaScriptSerializer(); var reply = serializer.Deserialize <Dictionary <string, object> >(json); if (reply != null && reply.ContainsKey("user")) { Trace.TraceInformation("{ tweet identified }"); var statuses = Status.ParseJson("[" + json + "]"); Application.Current.Dispatcher.InvokeAsync (() => UpdateStatusHomeTimelineCommand.Command.Execute(statuses, Application.Current.MainWindow)); } } } } catch (WebException ex) { Trace.TraceError(ex.ToString()); } catch (ArgumentNullException ex) { Trace.TraceError(ex.ToString()); } catch (ArgumentException ex) { Trace.TraceError(ex.ToString()); } catch (InvalidOperationException ex) { Trace.TraceError(ex.ToString()); } catch (IOException ex) { Trace.TraceError(ex.ToString()); } } Trace.TraceInformation("{ Stream task ends }"); }, cancelationToken); }