Skip to content

Instantly share code, notes, and snippets.

Daniel Kipkurui Kiptoon

Personal info

  • Date of birth:06/12/1992

  • Nationality:Kenyan

  • Postal Code:7070-30100, Nairobi 30100, Kenya

class Program
{
static void Main(string[] args)
{
var client = new Client();
var token =client.GetBearerToken().GetAwaiter().GetResult();
var tweets = client.GetUserTimelineJson(token, "@twitterapi", 200, true, false).GetAwaiter().GetResult();
}
/// <summary>
/// Gets a user timeline.
/// * API endpoint user_timeline: https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
/// </summary>
/// <param name="bearerToken">Token obtained from authentication.</param>
/// <param name="screenName">username of user whose timeline will be returned.</param>
/// <param name="count">Number of tweets to return</param>
/// <param name="excludeReplies">Whether to exclude replies. False is reccomended because API removed replies after obtaining requested number of tweets, leading to unpredictable result counts.</param>
/// <param name="includeRTs">Whether to include retweets. True is reccomended because API removes retweets after obtaining requested number of tweets, leading to unpredictable result counts.(</param>
/// <returns>Raw JSON response from API.</returns>
@KiptoonKipkurui
KiptoonKipkurui / helpermethods.cs
Created March 21, 2020 14:14
http client helper methods
private static HttpRequestMessage CreateRequest(string url,HttpMethod method)
{
var uri = new Uri(ApiBaseUrl + url);
var webrequest = new HttpRequestMessage(method,uri);
return webrequest;
}
@KiptoonKipkurui
KiptoonKipkurui / AuthenticationSnippet.cs
Last active March 21, 2020 14:15
code to get the oauth token from twitter api
/// <summary>
/// Gets bearer token for application-only authentication from Twitter API 1.1, obtaining key and secret from web.config/app.config.
/// * NOTE: This token should be cached by the application -- for up to 15 mins.
/// * Dependant on web.config appSettings params twitterConsumerKey and twitterConsumerSecret.
/// * Twitter API client oAuth settings: https://dev.twitter.com/app
/// * Application-only authentication: https://dev.twitter.com/docs/auth/application-only-auth
/// * API endpoint oauth2/token: https://dev.twitter.com/docs/api/1.1/post/oauth2/token
/// </summary>
/// <returns>oAuth bearer token for Twitter API authentication.</returns>
public async Task<string> GetBearerToken()
/// <summary>
/// twitter authentication options
/// </summary>
public class TwitterOptions
{
/// <summary>
/// twitter app consumer key
/// </summary>
public string ConsumerKey { get; set; }