using System; using System.Collections.Generic; using System.Data; using System.Configuration; using System.Web; using System.Web.Script; using System.Net; using System.IO; using System.Collections.Specialized; using System.Runtime.Remoting.Messaging; using System.Linq; using System.Text; using System.Xml; using System.Windows.Forms; using TradeIdeas.XML; using System.Web.Script.Serialization; namespace TradeIdeas.TIProGUI { //NOTICE!!! Currently for now, the LinkedIn option is "on hold", and menu items hidden. However, //LinkedIn has deprecated oAuth 1 and moved to oAuth 2. This means that the authorization and //getting access codes are a bit different. Thus this file has some major changes *in progress*. These changes //shall be saved to CVS. If we do decide to re-implement the LinkedIn option, we can pick up where we left off with //this class... public class oAuthLinkedIn : OAuthBase { private List _phrases; private string _consumerKey = ""; private string _consumerSecret = ""; public enum Method { GET, POST, PUT, DELETE }; public const string USER_AGENT = "YourAgent"; public const string AUTHORIZE_BASE = "https://www.linkedin.com/uas/oauth2/authorization"; public const string ACCESS_TOKEN = "https://www.linkedin.com/uas/oauth2/accessToken"; public const string CALLBACK = "http://trade-ideas.com/"; public static string STATE = "aWDF52XSDcKUpZC9"; private string _token = ""; //private string _tokenSecret = ""; SocialSettings _socialSettings = new SocialSettings(); #region PublicPropertiies public string ConsumerKey { get { _consumerKey = "e7sz09i0mofu"; //_keys.Node("API_KEY").PropertyForCulture("TEXT", "***"); return _consumerKey; } set { _consumerKey = value; } } public string ConsumerSecret { get { _consumerSecret = "YDJ4qmjDk5wRCfIL";// _keys.Node("SECRET_KEY").PropertyForCulture("TEXT", "***"); return _consumerSecret; } set { _consumerSecret = value; } } public string Token { get { return _token; } set { _token = value; } } #endregion /// /// Get the authorization token by showing the dialog /// /// The authorization token. public String authorizeToken() { LinkedInForm lw = new LinkedInForm(this,_socialSettings); lw.ShowDialog(); Token = lw.Token; return Token; } /// /// Get the access token /// /// The access token. public String getAccessToken() { _phrases = GuiEnvironment.XmlConfig.Node("COLLABORATE_WINDOW").Node("PHRASES"); if (string.IsNullOrEmpty(Token)) { MessageBox.Show(_phrases.Node("TOKEN_NOT_SET").PropertyForCulture("TEXT", "***")); return null; } //create the post payload string string tokenUrlPattern = @"grant_type=authorization_code&code={0}&redirect_uri={1}&client_id={2}&client_secret={3}"; string tokenUrl = string.Format(tokenUrlPattern, Token, CALLBACK, ConsumerKey, ConsumerSecret); string response = WebRequest(Method.POST, ACCESS_TOKEN, tokenUrl); if (response.Length > 0) { var test = new JavaScriptSerializer().Deserialize>(response); if (test.ContainsKey("access_token") && test["access_token"] != null) { this.Token = (test["access_token"]).ToString(); } else { this.Token = ""; } _socialSettings.oAuthTokenLinkedIn = this.Token; _socialSettings.Save(); } return Token; } /// /// Get the link to Linked In's authorization page for this application. /// public string AuthorizationLink { get { string tokenUrlPattern = @"{0}response_type=code&client_id={1}&redirect_uri={2}&state={3}"; string tokenUrl = string.Format(tokenUrlPattern,AUTHORIZE_BASE + "?", ConsumerKey, CALLBACK, STATE); return tokenUrl; } } /// /// WebRequestWithPut /// /// WebRequestWithPut /// /// /// public string APIWebRequest(string method, string url, object postData) { //THIS IS STILL A WORK IN PROGRESS. HERE WE'RE TRYING TO POST TO A //LINKED-IN USERS WALL VIA JSON. IT NEEDS FIXING AS THERE'S EXECEPTION THROWN // inspired from: http://stackoverflow.com/questions/11464330/post-to-linkedin-using-share-api var message = postData; var requestJson = new JavaScriptSerializer().Serialize(message); var client = new WebClient(); var requestHeaders = new NameValueCollection { { "Content-Type", "application/json" }, { "x-li-format", "json" } }; client.Headers.Add(requestHeaders); var responseJson = client.UploadString(url, "POST", requestJson); var response = new JavaScriptSerializer().Deserialize>(responseJson); return "work in progress"; } /// /// Web Request /// /// Http Method /// Full url to the web resource /// Data to post /// The web server response. public string WebRequest(Method method, string url, string postData) { HttpWebRequest webRequest = null; StreamWriter requestWriter = null; string responseData = ""; webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest; webRequest.Method = method.ToString(); webRequest.ServicePoint.Expect100Continue = false; webRequest.UserAgent = USER_AGENT; webRequest.Timeout = 20000; if (method == Method.POST) { webRequest.ContentType = "application/x-www-form-urlencoded"; requestWriter = new StreamWriter(webRequest.GetRequestStream()); try { requestWriter.Write(postData); } catch { throw; } finally { requestWriter.Close(); requestWriter = null; } } responseData = WebResponseGet(webRequest); webRequest = null; return responseData; } /// /// Process the web response. /// /// The request object. /// The response data. public string WebResponseGet(HttpWebRequest webRequest) { StreamReader responseReader = null; string responseData = ""; try { responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()); responseData = responseReader.ReadToEnd(); } catch (Exception e) { throw e; } finally { webRequest.GetResponse().GetResponseStream().Close(); responseReader.Close(); responseReader = null; } return responseData; } } }