public TwitterCredentials Authenticate() { if (twitterCredentials == null) { try { string url = "https://api.twitter.com/oauth2/token"; string consumerSecret = ConfigService.GetConfig(ConfigKeys.TWITTER_ACCESS_CONSUMER_SECRET, ""); string consumerKey = ConfigService.GetConfig(ConfigKeys.TWITTER_ACCESS_CONSUMER_KEY, ""); String bearerCredentials = HttpUtility.UrlEncode(consumerKey) + ":" + HttpUtility.UrlEncode(consumerSecret); bearerCredentials = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(bearerCredentials)); JSONConnector JSONConnector = new JSONConnector(); JObject searchResultsJSONObject = JSONConnector.PostJSONObjectWithHeader(url, "Authorization", "Basic " + bearerCredentials, "grant_type=client_credentials"); twitterCredentials = new TwitterAuthParser().Parse(searchResultsJSONObject); } catch (Exception exception) { ErrorService.Log("TwitterSearchService", "Auth", "Authentication Failed", exception); } } return twitterCredentials; }
public TwitterCredentials Parse(JObject searchResultsJSONObject) { TwitterCredentials credentials = null; try { string accessToken = searchResultsJSONObject.Value<string>("access_token"); credentials = new TwitterCredentials("Bearer", accessToken); } catch (Exception exception) { ErrorService.Log("TwitterAuthParser", "Auth", "Error Parsing", exception); } return credentials; }