Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I recommend using dotnetopenauth. </p> <p>Unfortunately I wasn't able to use it due to project constraints, so here is a snippet from my working code. I removed some proprietary code to try and make it easier to read and use. If anyone has questions please let me know, and I'll see if I can help.</p> <pre><code> /// &lt;summary&gt; /// Summary description for GoToWebinarProvider /// &lt;/summary&gt; public class GoToWebinarProvider { #region properties private GoToWebinarAccessToken _accessToken; private readonly string _apiKey = "apikey"; /// &lt;summary&gt; /// Use for debugging purposes /// &lt;/summary&gt; private const bool EnableCaching = true; private const string G2WApiDateTimeFormat = "yyyy-MM-ddTHH:mm:ssZ"; #endregion properties #region token management private void OAuthenticate() { GoToWebinarAccessToken localToken = null; //try to get access token from cache if (EnableCaching) { localToken = GetTokenFromDb(); //cache it here } else { _accessToken = GetTokenFromDb(); } //if the token hasn't expired yet, we have an access token, so instantiation is complete if (_accessToken != null &amp;&amp; _accessToken.ExpirationDate &gt; DateTime.Now) { return; } //else get new token var responseKey = GetResponseKey(); if (responseKey != null) { _accessToken = GetAccessToken(responseKey); if (_accessToken != null) { StoreTokenInDb(_accessToken); } else { throw new Exception("Could not acquire access token from GoToWebinar"); } } else { throw new Exception("Could not acquire response key from GoToWebinar"); } } private GoToWebinarAccessToken GetAccessToken(string responseKey) { var url = String.Format( "https://api.citrixonline.com/oauth/access_token?grant_type=authorization_code&amp;code={0}&amp;client_id={1}", responseKey, _apiKey); const string applicationJSON = "application/json; charset=UTF-8"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.ProtocolVersion = HttpVersion.Version11; httpWebRequest.Method = "GET"; httpWebRequest.Accept = applicationJSON; try { var response = httpWebRequest.GetResponse(); //will throw error if not 200 response using (var dataStream = response.GetResponseStream()) { var responseText = dataStream != null ? new StreamReader(dataStream).ReadToEnd() : String.Empty; var jsSerializer = new JavaScriptSerializer(); var accessToken = jsSerializer.Deserialize(responseText, typeof(GoToWebinarAccessToken)) as GoToWebinarAccessToken; //pad it by a minute. took time to calculate it, and each request will have latency in getting to gotowebinar accessToken.ExpirationDate = DateTime.Now.AddSeconds(Convert.ToDouble(accessToken.Expires_In)).AddMinutes(-1); return accessToken; } } catch (Exception ex) { return null; } } private string GetResponseKey() { var redirectURL = HttpContext.Current.Server.UrlEncode("http://mysite.net/Welcome.aspx"); var requestURL = String.Format("https://api.citrixonline.com/oauth/authorize?client_id={0}&amp;amp;redirect_uri={1}", _apiKey, redirectURL); var request = CreateWebRequest(requestURL, "GET"); try { //first request var response = request.GetResponse(); //will throw error if not 200 response using (var dataStream = response.GetResponseStream()) { var responseText = dataStream != null ? new StreamReader(dataStream).ReadToEnd() : String.Empty; return Login(redirectURL); } } catch (Exception ex) { return null; } } /// &lt;summary&gt; /// Should only be called by GetResponseKey /// &lt;/summary&gt; /// &lt;param name="urlEncodedRedirect"&gt;&lt;/param&gt; private string Login(string urlEncodedRedirect) { var elp = new EventLogProvider(); var userName = HttpContext.Current.Server.UrlEncode(SettingsKeyProvider.GetSettingsKeyInfo("GoToWebinarUserEmail").KeyValue); var password = HttpContext.Current.Server.UrlEncode(SettingsKeyProvider.GetSettingsKeyInfo("GoToWebinarUserPassword").KeyValue); var postData = String.Format( "emailAddress={0}&amp;password={1}&amp;client_id={2}&amp;access_type=G2W&amp;app_name=MyApp&amp;redirect_url={3}&amp;submitted=form_submitted&amp;submit=Log+In", userName, password, _apiKey, urlEncodedRedirect); var request = CreateWebRequest("https://developer.citrixonline.com/oauth/g2w/authorize.php", "POST", postData, "application/x-www-form-urlencoded"); request.AllowAutoRedirect = false; var response = request.GetResponse(); using (var dataStream = response.GetResponseStream()) { var responseText = dataStream != null ? new StreamReader(dataStream).ReadToEnd() : String.Empty; const string locationHeader = "location"; if (!String.IsNullOrWhiteSpace(response.Headers.Get(locationHeader))) { //get redirect to login page var locationRedirect = response.Headers.Get(locationHeader); elp.LogEvent("I", DateTime.Now, GetType().Name, "GoToWebinar", HttpContext.Current.Request.Url.ToString(), locationRedirect); const string comparator = "code="; var index = locationRedirect.IndexOf(comparator, StringComparison.OrdinalIgnoreCase); if (index != -1) { return locationRedirect.Substring(index + comparator.Length); } throw new Exception("Could not parse response code: " + locationRedirect); } throw new Exception("Did not receive redirect: ", new Exception(responseText)); } } private static GoToWebinarAccessToken GetTokenFromDb() { //return token } private static void StoreTokenInDb(GoToWebinarAccessToken accessToken) { //store token } #endregion token management #region api calls public IList&lt;GoToWebinarWebinar&gt; GetWebinars() { if (_accessToken == null || _accessToken.ExpirationDate &lt; DateTime.Now) { OAuthenticate(); } var serviceEndPoint = String.Format("https://api.citrixonline.com/G2W/rest/organizers/{0}/webinars", _accessToken.Organizer_Key); try { var httpWebRequest = CreateJSONRequest(serviceEndPoint, "GET", null); var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var httpResponseStream = httpResponse.GetResponseStream()) { if (httpResponseStream != null) { using (var streamReader = new StreamReader(httpResponseStream)) { var responseText = streamReader.ReadToEnd(); var jsSerializer = new JavaScriptSerializer(); var gotoWebinarList = jsSerializer.Deserialize&lt;IList&lt;GoToWebinarWebinar&gt;&gt;(responseText); return gotoWebinarList; } } } } catch (Exception ex) { //log it } return null; } #endregion api calls #region web requests private HttpWebRequest CreateJSONRequest(string serviceEndPoint, string httpMethod, string postData) { const string applicationJSON = "application/json"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(serviceEndPoint); httpWebRequest.ProtocolVersion = HttpVersion.Version11; httpWebRequest.Method = httpMethod; httpWebRequest.Accept = String.Format("{0}, application/vnd.citrix.g2wapi-v1.1+json", applicationJSON); httpWebRequest.ContentType = applicationJSON; httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, _accessToken.Access_Token); if (httpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase)) { var encoding = new ASCIIEncoding(); var body = encoding.GetBytes(postData); httpWebRequest.ContentLength = body.Length; using (var newStream = httpWebRequest.GetRequestStream()) { newStream.Write(body, 0, body.Length); newStream.Close(); } } return httpWebRequest; } private static HttpWebRequest CreateWebRequest(string serviceEndPoint, string httpMethod) { //const string applicationJSON = "application/json"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(serviceEndPoint); httpWebRequest.ProtocolVersion = HttpVersion.Version11; httpWebRequest.Method = httpMethod; //httpWebRequest.Accept = String.Format("{0}, application/vnd.citrix.g2wapi-v1.1+json", applicationJSON); //httpWebRequest.ContentType = applicationJSON; //httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, _accessToken); return httpWebRequest; } private static HttpWebRequest CreateWebRequest(string serviceEndPoint, string httpMethod, string postData, string contentType) { var httpWebRequest = (HttpWebRequest)WebRequest.Create(serviceEndPoint); httpWebRequest.ProtocolVersion = HttpVersion.Version11; httpWebRequest.Method = httpMethod; if (contentType != null) { httpWebRequest.ContentType = contentType; } if (httpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase)) { var encoding = new ASCIIEncoding(); var body = encoding.GetBytes(postData); httpWebRequest.ContentLength = body.Length; using (var newStream = httpWebRequest.GetRequestStream()) { newStream.Write(body, 0, body.Length); newStream.Close(); } } return httpWebRequest; } #endregion web requests private void LogRequest(string methodName, string dataToLog) { //log it } /// &lt;summary&gt; /// Syntax matches JSON response /// &lt;/summary&gt; private class GoToWebinarAccessToken { public string Access_Token; public string Refresh_Token; public string Organizer_Key; public string Account_Key; public double? Expires_In; public DateTime ExpirationDate; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload