private static OAuth1Helper getOAuthHelper(string url, string httpMethod, string token, string secret) { OAuth1Token oAuth1Token = new OAuth1Token(SettingsTwitter.TwitterAppId, SettingsTwitter.TwitterAppSecret, token, secret); OAuth1Helper oAuth1Helper = new OAuth1Helper(url, oAuth1Token, httpMethod); return(oAuth1Helper); }
/// <summary> /// Fetches Data wrt given Tweet ID /// </summary> /// <param name="taskInfo">contains the TweetID for which data is to be fetched</param> public override async Task <List <ItemMetadata> > FetchData(ConnectorTask taskInfo, string sourceInfo) { Trace.TraceInformation("Data fetch Started"); List <ItemMetadata> itemMetaData = new List <ItemMetadata>(); SourceInfoTwitter twitterSourceInfo = JsonConvert.DeserializeObject <SourceInfoTwitter>(sourceInfo); OAuth1Token token = new OAuth1Token(SettingsTwitter.TwitterApiKey, SettingsTwitter.TwitterApiSecretKey, twitterSourceInfo.ClientToken, twitterSourceInfo.ClientSecret); var filterTime = taskInfo.EndTime; OAuth1Helper oAuth1Helper = new OAuth1Helper(url, token, HttpMethod.Get.ToString().ToUpperInvariant()); while (true) { Dictionary <string, string> param = getParams(taskInfo, twitterSourceInfo); string queryString = oAuth1Helper.GetQueryString(param); string authHeader = oAuth1Helper.GenerateAuthorizationHeader(); AuthenticationHeaderValue header = new AuthenticationHeaderValue("OAuth", authHeader); List <Tweet> tweets = await downloader.GetWebContent <List <Tweet>, ErrorsTwitter>(queryString, header); bool isScheduleCompleted = false; if (tweets != null && tweets.Any()) { var minId = tweets.Select(t => long.Parse(t.Tweetid)).ToList <long>().Min().ToString() ?? twitterSourceInfo.SinceId; isScheduleCompleted = DateTime.Compare(DateTime.ParseExact(tweets.Where(t => t.Tweetid.Equals(minId)).First().CreatedAt, Const_TwitterDateTemplate, new System.Globalization.CultureInfo("en-US")), taskInfo.EndTime) > 0; } if (tweets == null || tweets.Count == 0 || isScheduleCompleted) { break; // When no new data to get since sinceID(last fetched tweet) } twitterSourceInfo.SinceId = tweets.Select(t => long.Parse(t.Tweetid)).ToList <long>().Max().ToString(); tweets.RemoveAll(t => DateTime.Compare(DateTime.ParseExact(t.CreatedAt, Const_TwitterDateTemplate, new System.Globalization.CultureInfo("en-US")), taskInfo.StartTime) < 0); tweets.RemoveAll(t => DateTime.Compare(DateTime.ParseExact(t.CreatedAt, Const_TwitterDateTemplate, new System.Globalization.CultureInfo("en-US")), taskInfo.EndTime) > 0); Trace.TraceInformation($"Tweets Fetched {tweets.Count}"); if (tweets.Any()) { foreach (var tweet in tweets) { var enrichedTweet = await EnrichTweetWithAttachments(tweet); itemMetaData.Add(await UploadTweet(twitterItemMapper, enrichedTweet, taskInfo)); } twitterSourceInfo.SinceId = tweets.Select(t => long.Parse(t.Tweetid)).ToList <long>().Max().ToString(); } } return(itemMetaData); }
public async Task <string> GetAccessToken(string accessCode, string redirectUrl, Dictionary <string, string> requestTokens) { string url = string.Format($"{SettingsTwitter.TwitterEndPoint}/oauth/access_token"); var queryParams = new Dictionary <string, string>(); queryParams.Add("oauth_verifier", accessCode); queryParams.Add("oauth_callback", redirectUrl); OAuth1Token oAuth1Token = new OAuth1Token(SettingsTwitter.TwitterClientToken, SettingsTwitter.TwitterClientSecret, requestTokens.Where(k => k.Key == "ClientToken").FirstOrDefault().Value, requestTokens.Where(k => k.Key == "ClientSecret").FirstOrDefault().Value); OAuth1Helper oAuth1Helper = new OAuth1Helper(url, oAuth1Token, HttpMethod.Post.ToString().ToUpperInvariant()); var qstr = oAuth1Helper.GetQueryString(queryParams); string tempToken = oAuth1Helper.GenerateAuthorizationHeader(); var requestHeaders = new Dictionary <string, string>(); requestHeaders.Add("Authorization", "OAuth " + tempToken); return(await this.Client.PostRequestAsync <Dictionary <string, string>, string>(url, requestHeaders, queryParams, CancellationToken.None)); }
static void AuthorizeTwitterAccess_UsingOutOfBandPincode() { // Get Twitter API keys from file (don't want the secret parts hardcoded in public repository TwitterKeys keys = ReadKeys(); // Configure OAuth1 with the stuff it needs for it's magic OAuth1Settings settings = new OAuth1Settings { ConsumerKey = keys.consumer_key, ConsumerSecrect = keys.consumer_secret, RequestTokenUrl = new Uri(Session.BaseUri, TwitterApi.OAuthRequestTokenPath), AuthorizeUrl = new Uri(Session.BaseUri, TwitterApi.OAuthAuthorizePath), AccessTokenUrl = new Uri(Session.BaseUri, TwitterApi.OAuthAccessTokenPath), CallbackUrl = "oob" // oob = Out Of Band }; Session.OAuth1Configure(settings); // Attach a logger after settings has been assigned (use debugger for breakpoints inside it) Session.OAuth1Logger(new OAuth1Logger()); // Get temporary credentials from Twitter (request token) and remember it internally OAuth1Token requestToken = Session.OAuth1GetRequestToken(); // Ask user to authorize use of the request token Console.WriteLine("Now opening a browser with autorization info. Please follow instructions there."); Request authorizationRequest = Session.Bind(TwitterApi.OAuthAuthorizePath, requestToken); Process.Start(authorizationRequest.Url.AbsoluteUri); Console.WriteLine("Please enter Twitter pincode: "); string pincode = Console.ReadLine(); if (!string.IsNullOrWhiteSpace(pincode)) { // Get access credentials from Twitter Session.OAuth1GetAccessTokenFromRequestToken(pincode); } }
public void SaveRequestToken(string key, OAuth1Token credential) { cache.Set(BuildCacheKey(key), credential, TimeSpan.FromMinutes(15)); }
/// <summary> /// Gets the Twitter access token. /// </summary> /// <param name="requestToken">The request token.</param> /// <param name="verifier">The verifier.</param> /// <returns><see cref="OAuth1TokenCredential"/> object.</returns> /// <author>Anwar</author> /// <datetime>3/26/2011 3:31 PM</datetime> public OAuth1TokenCredential GetAccessToken(OAuth1Token requestToken, string verifier) { return(this.GetAccessToken(requestToken, TwitterConstants.AccessToken, verifier)); }