public T GetLinkedInFeeds(string linkedInGetPostsUrl, OauthAuthentication oauthAuthentication) { //Using Linked Oauth: http://www.codeproject.com/Articles/247336/Twitter-OAuth-authentication-using-Net if (HttpContext.Current.Session["OAuthAuthentication"] == null) { GetOauthLinkedInToken(oauthAuthentication); } else { var queries = HttpContext.Current.Request.QueryString; var code = queries.GetValues("code").FirstOrDefault(); if (!string.IsNullOrEmpty(code)) { var oauthUnit = HttpContext.Current.Session["OAuthAuthentication"] as OauthAuthentication; oauthUnit.TokenKey = code; var accessToken = GetLinkedInAccessToken(oauthUnit); var oauthHeader = GetOauthHeader(OauthHeaderType.Bearer, accessToken, OAuthSocialNetwork.LinkedIn); Func <XDocument, IEnumerable <TweetObject> > resolveXml = GetLinkedInFeedsFromXdoc; return(MakeGetHttpRequest(linkedInGetPostsUrl, resolveXml, oauthHeader)); } } return(new T()); }
public WidgetGroupItemList GetProfileTwitterFeeds(string twitterProfileUrl, OauthAuthentication oauthAuthentication) { //Using Twitter Oauth: http://www.codeproject.com/Articles/247336/Twitter-OAuth-authentication-using-Net _widgetGroupItems = _cache[_cacheKey] == null ? null : _cache[_cacheKey] as WidgetGroupItemList; if (_widgetGroupItems == null) { _widgetGroupItems = new WidgetGroupItemList(); var oauthToken = GetOauthToken(oauthAuthentication); var oauthHeader = GetOauthHeader(OauthHeaderType.Bearer, oauthToken); StreamReader reader = null; ServicePointManager.Expect100Continue = false; try { HttpWebRequest request = null; request = WebRequest.Create(twitterProfileUrl) as HttpWebRequest; if (request != null) { request.Headers.Add("Authorization", oauthHeader); request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; using (var response = request.GetResponse() as HttpWebResponse) { using (Stream respStream = response.GetResponseStream()) { if (respStream != null) { reader = new StreamReader(respStream); string xmlString = reader.ReadToEnd(); reader.Close(); return(ConvertRawTweetsToXmlConsumables(xmlString)); } } } } return(new WidgetGroupItemList()); } catch (Exception e) { if (reader != null) { reader.Close(); } throw e; } } else { return(_cache[_cacheKey] as WidgetGroupItemList); } }
public void GetOauthLinkedInToken(OauthAuthentication oauthAuthentication) { StreamReader reader = null; HttpWebRequest request = null; //var oauthHeader = GetOauthHeader(oauthAuthentication, OauthHeaderType.Basic, OAuthSocialNetwork.LinkedIn); try { request = WebRequest.Create(string.Format("https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id={0}&redirect_uri={1}&scope=r_fullprofile&state=MezZanillionsxein2859", oauthAuthentication.ConsumerKey, "http%3A%2F%2Fmartinlayooinc.test.uk/auth%2Flinkedin%2Fcallback")) as HttpWebRequest; //request.Headers.Add("Authorization", oauthHeader); request.Method = "GET"; if (request != null) { using (var response = request.GetResponse() as HttpWebResponse) { using (Stream respStream = response.GetResponseStream()) { if (respStream != null) { reader = new StreamReader(respStream); string xmlString = reader.ReadToEnd(); reader.Close(); var tokenResult = xmlString.Split(new char[] { '&' }); if (tokenResult.Contains("code")) { var resultCode = tokenResult.Where(p => p.StartsWith("code")).SingleOrDefault(); if (!string.IsNullOrEmpty(resultCode)) { oauthAuthentication.TokenKey = resultCode.Split('=')[1]; } /* * var resultState = tokenResult.Where(p => p.StartsWith("state")).SingleOrDefault(); * if (!string.IsNullOrEmpty(resultCode)) * { * oauthAuthentication.TokenSecret = resultTokenSecret.Split('=')[1]; * }*/ } } } } } } catch (Exception e) { if (reader != null) { reader.Close(); } throw e; } HttpContext.Current.Session["OAuthAuthentication"] = oauthAuthentication; }
public T GetProfileTwitterFeeds(string twitterProfileUrl, OauthAuthentication oauthAuthentication) { //Using Twitter Oauth: http://www.codeproject.com/Articles/247336/Twitter-OAuth-authentication-using-Net var oauthToken = GetOauthTwitterToken(oauthAuthentication); var oauthHeader = GetOauthHeader(OauthHeaderType.Bearer, oauthToken, OAuthSocialNetwork.Twitter); Func <XDocument, IEnumerable <TweetObject> > resolveXml = GetTweetFeedsFromXdoc; return(MakeGetHttpRequest(twitterProfileUrl, resolveXml, oauthHeader)); }
public string GetOauthToken(OauthAuthentication oauthAuthentication) { StreamReader reader = null; HttpWebRequest request = null; var oauthHeader = GetOauthHeader(oauthAuthentication, OauthHeaderType.Basic); try { request = WebRequest.Create("https://api.twitter.com/oauth2/token") as HttpWebRequest; request.Headers.Add("Authorization", oauthHeader); request.Method = "POST"; if (request != null) { request.ContentType = "application/x-www-form-urlencoded"; var bytes = UTF8Encoding.ASCII.GetBytes("grant_type=client_credentials"); request.ContentLength = bytes.Length; request.GetRequestStream().Write(bytes, 0, bytes.Length); using (var response = request.GetResponse() as HttpWebResponse) { using (Stream respStream = response.GetResponseStream()) { if (respStream != null) { reader = new StreamReader(respStream); string xmlString = reader.ReadToEnd(); reader.Close(); var tokenResult = xmlString.Split(new char[] { ',' }); if (tokenResult[0].Contains("bearer")) { return(tokenResult[1].Split(new char[] { ':' })[1].Split(new char[] { '}', '"' }, StringSplitOptions.RemoveEmptyEntries)[0].Trim()); } else { return(tokenResult[0].Split(new char[] { ':' })[1].Split(new char[] { '}', '"' }, StringSplitOptions.RemoveEmptyEntries)[0].Trim()); } } } } } return(null); } catch (Exception e) { if (reader != null) { reader.Close(); } throw e; } }
private string GetOauthHeader(OauthAuthentication oauthentication, string resource_url) { var oauth_version = "1.0"; var oauth_signature_method = "HMAC-SHA1"; var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); var timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString(); var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&oauth_verifier={6}"; var oauth_token = GetOauthToken(oauthentication); var baseString = string.Format(baseFormat, oauthentication.ConsumerKey, oauth_nonce, oauth_signature_method, oauth_timestamp, oauthentication.TokenKey, oauth_version ); baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString)); var compositeKey = string.Concat(Uri.EscapeDataString(oauthentication.ConsumerSecret), "&", Uri.EscapeDataString(oauthentication.TokenSecret)); string oauth_signature; using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))) { oauth_signature = Convert.ToBase64String( hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString))); } var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " + "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " + "oauth_token=\"{4}\", oauth_signature=\"{5}\", " + "oauth_version=\"{6}\",oauth_callback=oob"; var authHeader = string.Format(headerFormat, Uri.EscapeDataString(oauth_nonce), Uri.EscapeDataString(oauth_signature_method), Uri.EscapeDataString(oauth_timestamp), Uri.EscapeDataString(oauthentication.ConsumerKey), Uri.EscapeDataString(oauthentication.TokenKey), Uri.EscapeDataString(oauth_signature), Uri.EscapeDataString(oauth_version) ); return(authHeader); }
private string GetLinkedInAccessToken(OauthAuthentication oauthAuthentication) { StreamReader reader = null; HttpWebRequest request = null; try { request = WebRequest.Create("https://api.linkedin.com/uas/oauth/accessToken") as HttpWebRequest; request.Method = "POST"; if (request != null) { request.ContentType = "application/x-www-form-urlencoded"; var bytes = UTF8Encoding.ASCII.GetBytes(string.Format("grant_type=authorization_code&redirect_uri={0}&client_id={1}&client_secret={2}&code={3}", "http%3A%2F%2Flocalhost%2Fmartinchaos%2Home%AboutUs", oauthAuthentication.ConsumerKey, oauthAuthentication.TokenSecret, oauthAuthentication.TokenKey)); request.ContentLength = bytes.Length; request.GetRequestStream().Write(bytes, 0, bytes.Length); using (var response = request.GetResponse() as HttpWebResponse) { using (Stream respStream = response.GetResponseStream()) { if (respStream != null) { reader = new StreamReader(respStream); string jsonString = reader.ReadToEnd(); //jsonString ==> {"access_token":xxx, "expires_in":xxx} reader.Close(); var tokenResult = jsonString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0]; var accessToken = tokenResult.Split(new char[] { '{', '}', ':' }, StringSplitOptions.RemoveEmptyEntries)[1]; return(accessToken); } } } } } catch (Exception e) { if (reader != null) { reader.Close(); } throw e; } return(string.Empty); }
private string GetOauthHeader(OauthAuthentication oauthentication, OauthHeaderType oauthHeaderType) { var oauth_version = "1.0"; var oauth_signature_method = "HMAC-SHA1"; var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); var timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString(); var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}"; var baseString = string.Format(baseFormat, oauthentication.ConsumerKey, oauth_nonce, oauth_signature_method, oauth_timestamp, oauthentication.TokenKey, oauth_version ); var compositeKey = string.Concat(Uri.EscapeDataString(oauthentication.ConsumerSecret), "&", Uri.EscapeDataString(oauthentication.TokenSecret)); string oauth_signature; using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))) { oauth_signature = Convert.ToBase64String( hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString))); } var headerFormat = oauthentication.ConsumerKey + ":" + oauthentication.ConsumerSecret; headerFormat = Convert.ToBase64String(ASCIIEncoding.UTF8.GetBytes(headerFormat)); return(oauthHeaderType.ToString() + " " + headerFormat); }
private string GetOauthHeader(OauthAuthentication oauthentication, OauthHeaderType oauthHeaderType, OAuthSocialNetwork network) { var oauth_version = "1.0"; var oauth_signature_method = "HMAC-SHA1"; var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); var timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString(); var baseFormat = string.Empty; var baseString = string.Empty; if (network.Equals(OAuthSocialNetwork.Twitter)) { baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}"; baseString = string.Format(baseFormat, oauthentication.ConsumerKey, oauth_nonce, oauth_signature_method, oauth_timestamp, oauthentication.TokenKey, oauth_version ); } else if (network.Equals(OAuthSocialNetwork.LinkedIn)) { baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_callback={4}&oauth_version={5}&client_id={6}&redirect_uri={7}&scope=r_fullprofile%20w_share&state=MezZanillionsxein2859"; baseString = string.Format(baseFormat, oauthentication.ConsumerKey, oauth_nonce, oauth_signature_method, oauth_timestamp, HttpContext.Current.Request.Url.AbsoluteUri, oauth_version, oauthentication.ClientAppId, HttpContext.Current.Request.Url.AbsoluteUri ); } var compositeKey = string.Concat(Uri.EscapeDataString(oauthentication.ConsumerSecret), "&", Uri.EscapeDataString(oauthentication.TokenSecret)); string oauth_signature; using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))) { oauth_signature = Convert.ToBase64String( hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString))); } var headerFormat = oauthentication.ConsumerKey + ":" + oauthentication.ConsumerSecret; headerFormat = Convert.ToBase64String(ASCIIEncoding.UTF8.GetBytes(headerFormat)); return(oauthHeaderType.ToString() + " " + headerFormat); }