Created
April 7, 2014 19:38
-
-
Save mslot/10037543 to your computer and use it in GitHub Desktop.
This gist creates a application-only request, retrieving the newest tweet from the https://twitter.com/brianbondy/lists/stack-overflow-15 list. Can be used if you want to do application only things, described here: https://dev.twitter.com/docs/auth/application-only-auth simply by suppling another url to line 50.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class GetStatuses | |
{ | |
public static void Get() | |
{ | |
// You need to set your own keys and screen name | |
var oAuthConsumerKey = ""; | |
var oAuthConsumerSecret = ""; | |
var oAuthUrl = "https://api.twitter.com/oauth2/token"; | |
var screenname = ""; | |
// Do the Authenticate | |
var authHeaderFormat = "Basic {0}"; | |
var authHeader = string.Format(authHeaderFormat, | |
Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":" + | |
Uri.EscapeDataString((oAuthConsumerSecret))) | |
)); | |
var postBody = "grant_type=client_credentials"; | |
HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl); | |
authRequest.Headers.Add("Authorization", authHeader); | |
authRequest.Method = "POST"; | |
authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; | |
authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; | |
using (Stream stream = authRequest.GetRequestStream()) | |
{ | |
byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody); | |
stream.Write(content, 0, content.Length); | |
} | |
authRequest.Headers.Add("Accept-Encoding", "gzip"); | |
WebResponse authResponse = authRequest.GetResponse(); | |
// deserialize into an object | |
TwitAuthenticateResponse twitAuthResponse; | |
using (authResponse) | |
{ | |
using (var reader = new StreamReader(authResponse.GetResponseStream())) | |
{ | |
JavaScriptSerializer js = new JavaScriptSerializer(); | |
var objectText = reader.ReadToEnd(); | |
twitAuthResponse = JsonConvert.DeserializeObject<TwitAuthenticateResponse>(objectText); | |
} | |
} | |
// Do the timeline | |
//var timelineFormat = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}&include_rts=1&exclude_replies=1&count=5"; | |
var timelineFormat = "https://api.twitter.com/1.1/lists/statuses.json?list_id={0}&count=1"; | |
var timelineUrl = string.Format(timelineFormat, "32809390"); | |
HttpWebRequest timeLineRequest = (HttpWebRequest)WebRequest.Create(timelineUrl); | |
var timelineHeaderFormat = "{0} {1}"; | |
timeLineRequest.Headers.Add("Authorization", string.Format(timelineHeaderFormat, twitAuthResponse.token_type, twitAuthResponse.access_token)); | |
timeLineRequest.Method = "Get"; | |
WebResponse timeLineResponse = timeLineRequest.GetResponse(); | |
var timeLineJson = string.Empty; | |
using (timeLineResponse) | |
{ | |
using (var reader = new StreamReader(timeLineResponse.GetResponseStream())) | |
{ | |
timeLineJson = reader.ReadToEnd(); | |
} | |
} | |
Console.WriteLine(timeLineJson); | |
Console.ReadKey(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment