static string GetDevDefinedOAuthHeader(HttpWebRequest webRequest, string consumerKey, string consumerSecret, string accessToken, string accessTokenSecret) { OAuthConsumerContext consumerContext = new OAuthConsumerContext { ConsumerKey = consumerKey, ConsumerSecret = consumerSecret, SignatureMethod = SignatureMethod.HmacSha1, UseHeaderForOAuthParameters = true }; consumerContext.UseHeaderForOAuthParameters = true; //URIs not used - we already have Oauth tokens OAuthSession oSession = new OAuthSession(consumerContext, "https://www.example.com", "https://www.example.com", "https://www.example.com"); oSession.AccessToken = new TokenBase { Token = accessToken, ConsumerKey = consumerKey, TokenSecret = accessTokenSecret }; IConsumerRequest consumerRequest = oSession.Request(); consumerRequest = ConsumerRequestExtensions.ForMethod(consumerRequest, webRequest.Method); consumerRequest = ConsumerRequestExtensions.ForUri(consumerRequest, webRequest.RequestUri); consumerRequest = consumerRequest.SignWithToken(); return(consumerRequest.Context.GenerateOAuthParametersForHeader()); }
static string GetDevDefinedOAuthHeader(HttpWebRequest webRequest, string consumerKey, string consumerSecret, string accessToken, string accessTokenSecret) { OAuthConsumerContext consumerContext = new OAuthConsumerContext { ConsumerKey = consumerKey, ConsumerSecret = consumerSecret, SignatureMethod = SignatureMethod.HmacSha1, UseHeaderForOAuthParameters = true }; consumerContext.UseHeaderForOAuthParameters = true; OAuthSession oSession = new OAuthSession(consumerContext, GET_REQUEST_TOKEN, AuthorizeUrl, GET_ACCESS_TOKEN); oSession.AccessToken = new TokenBase { Token = accessToken, ConsumerKey = consumerKey, TokenSecret = accessTokenSecret }; IConsumerRequest consumerRequest = oSession.Request(); consumerRequest = ConsumerRequestExtensions.ForMethod(consumerRequest, webRequest.Method); consumerRequest = ConsumerRequestExtensions.ForUri(consumerRequest, webRequest.RequestUri); consumerRequest = consumerRequest.SignWithToken(); return(consumerRequest.Context.GenerateOAuthParametersForHeader()); }
private static string GetDevDefinedOAuthHeader(string consumerKey, string consumerSecret, string accessToken, string accessTokenSecret, HttpWebRequest webRequest, string requestBody) { OAuthConsumerContext consumerContext = new OAuthConsumerContext { ConsumerKey = consumerKey, SignatureMethod = SignatureMethod.HmacSha1, ConsumerSecret = consumerSecret, UseHeaderForOAuthParameters = true }; //We already have OAuth tokens, so OAuth URIs below are not used - set to example.com OAuthSession oSession = new OAuthSession(consumerContext, "https://www.example.com", "https://www.example.com", "https://www.example.com"); oSession.AccessToken = new TokenBase { Token = accessToken, ConsumerKey = consumerKey, TokenSecret = accessTokenSecret }; IConsumerRequest consumerRequest = oSession.Request(); consumerRequest = ConsumerRequestExtensions.ForMethod(consumerRequest, webRequest.Method); if (requestBody != null) { consumerRequest = consumerRequest.Post().WithRawContentType(webRequest.ContentType).WithRawContent(System.Text.Encoding.ASCII.GetBytes(requestBody)); } consumerRequest = ConsumerRequestExtensions.ForUri(consumerRequest, webRequest.RequestUri); consumerRequest = consumerRequest.SignWithToken(); return(consumerRequest.Context.GenerateOAuthParametersForHeader()); }
public IConsumerResponse RunConsumerRequest(IConsumerRequest consumerRequest) { int retryCounter = 2; while (retryCounter-- > 0) { IConsumerResponse consumerResponse = ConsumerRequestRunner.Run(consumerRequest); LogMessage(consumerRequest, consumerResponse); if (consumerResponse.IsForbiddenResponse) { // Catch http 403 errors generated by IIS that are actually html pages warning about certificate issues.. throw new AuthenticationException(string.Format("The API server returned http {0} with content type {1}. See the inner exception for more details.", (int)consumerResponse.ResponseCode, consumerResponse.ContentType), consumerResponse.WebException); } if (consumerResponse.IsTokenExpiredResponse && !string.IsNullOrEmpty(consumerRequest.Context.SessionHandle)) { // Refresh the access token and try again.. AccessToken newAccessToken = RenewAccessToken(); consumerRequest.SignWithToken(newAccessToken, false); continue; } if (consumerResponse.IsOAuthProblemResponse) { // A usable response wasn't returned.. throw new OAuthException(consumerResponse, consumerRequest.Context, consumerResponse.ToProblemReport()); } return(consumerResponse); } throw new ApplicationException("The consumer request could not be executed into a valid consumer response"); }
/// <summary> /// Creates a HttpRequest with oAuthSession (OAuth Token) and gets the response with invalidating user /// from QuickBooks for this app /// For Authorization: The request header must include the OAuth parameters defined by OAuth Core 1.0 Revision A. /// /// If the disconnect is successful, then the HTTP status code is 200 and /// the XML response includes the <ErrorCode> element with a 0 value. /// If an HTTP error is detected, then the HTTP status code is not 200. /// If an HTTP error is not detected but the disconnect is unsuccessful, /// then the HTTP status code is 200 and the response XML includes the <ErrorCode> element with a non-zero value. /// For example, if the OAuth access token expires or is invalid for some other reason, then the value of <ErrorCode> is 270. /// </summary> /// <param name="sender">Sender of the event.</param> /// <param name="e">Event args.</param> protected void Page_Load(object sender, EventArgs e) { OAuthConsumerContext consumerContext = new OAuthConsumerContext { ConsumerKey = ConfigurationManager.AppSettings["consumerKey"].ToString(), SignatureMethod = SignatureMethod.HmacSha1, ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"].ToString() }; OAuthSession oSession = new OAuthSession(consumerContext, Constants.OauthEndPoints.IdFedOAuthBaseUrl + Constants.OauthEndPoints.UrlRequestToken, Constants.OauthEndPoints.AuthorizeUrl, Constants.OauthEndPoints.IdFedOAuthBaseUrl + Constants.OauthEndPoints.UrlAccessToken); oSession.ConsumerContext.UseHeaderForOAuthParameters = true; if ((Session["accessToken"] + "").Length > 0) { oSession.AccessToken = new TokenBase { Token = HttpContext.Current.Session["accessToken"].ToString(), ConsumerKey = ConfigurationManager.AppSettings["consumerKey"].ToString(), TokenSecret = HttpContext.Current.Session["accessTokenSecret"].ToString() }; IConsumerRequest conReq = oSession.Request(); conReq = conReq.Get(); conReq = conReq.ForUrl(Constants.IaEndPoints.DisconnectUrl); try { conReq = conReq.SignWithToken(); } catch (Exception ex) { throw ex; } //Used just see the what header contains string header = conReq.Context.GenerateOAuthParametersForHeader(); //This method will clean up the OAuth Token txtServiceResponse = conReq.ReadBody(); //Reset All the Session Variables HttpContext.Current.Session.Remove("oauthToken"); // Add the invalid access token into session for the display of the Disconnect btn HttpContext.Current.Session["InvalidAccessToken"] = HttpContext.Current.Session["accessToken"]; // Dont remove the access token since this is required for Reconnect btn in the Blue dot menu // HttpContext.Current.Session.Remove("accessToken"); // Dont Remove flag since we need to display the blue dot menu for Reconnect btn in the Blue dot menu // HttpContext.Current.Session.Remove("Flag"); DisconnectFlg = "User is Disconnected from QuickBooks!"; //Remove the Oauth access token from the OauthAccessTokenStorage.xml OauthAccessTokenStorageHelper.RemoveInvalidOauthAccessToken(Session["FriendlyEmail"].ToString(), Page); } }
/// <summary> /// Gets the OAuth header for request. /// </summary> /// <param name="oauthSession">The OAuth session.</param> /// <param name="webRequest">The web request.</param> /// <returns> /// Returns the OAuth header /// </returns> private string GetOAuthHeaderForRequest(IOAuthSession oauthSession, WebRequest webRequest) { IConsumerRequest consumerRequest = oauthSession.Request(); consumerRequest = ConsumerRequestExtensions.ForMethod(consumerRequest, webRequest.Method); consumerRequest = ConsumerRequestExtensions.ForUri(consumerRequest, webRequest.RequestUri); if (webRequest.Headers.Count > 0) { ConsumerRequestExtensions.AlterContext(consumerRequest, context => context.Headers = webRequest.Headers); } if (this.AdditionalParameters != null) { consumerRequest.Context.AuthorizationHeaderParameters.Add(this.AdditionalParameters); } consumerRequest = consumerRequest.SignWithToken(); return(consumerRequest.Context.GenerateOAuthParametersForHeader()); }
public static string callPlatform(RestProfile profile, string url) { OAuthConsumerContext consumerContext = new OAuthConsumerContext { ConsumerKey = ConfigurationManager.AppSettings["consumerKey"].ToString(), SignatureMethod = SignatureMethod.HmacSha1, ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"].ToString() }; OAuthSession oSession = new OAuthSession(consumerContext, Constants.OauthEndPoints.IdFedOAuthBaseUrl + Constants.OauthEndPoints.UrlRequestToken, Constants.OauthEndPoints.AuthorizeUrl, Constants.OauthEndPoints.IdFedOAuthBaseUrl + Constants.OauthEndPoints.UrlAccessToken); oSession.ConsumerContext.UseHeaderForOAuthParameters = true; if (profile.OAuthAccessToken.Length > 0) { oSession.AccessToken = new TokenBase { Token = profile.OAuthAccessToken, ConsumerKey = ConfigurationManager.AppSettings["consumerKey"].ToString(), TokenSecret = profile.OAuthAccessTokenSecret }; IConsumerRequest conReq = oSession.Request(); conReq = conReq.Get(); conReq = conReq.ForUrl(url); try { conReq = conReq.SignWithToken(); return(conReq.ReadBody()); } catch (Exception ex) { throw ex; } } return(""); }
public bool doDisconnect() { OAuthSession oas = getOAuthSession(); oas.ConsumerContext.UseHeaderForOAuthParameters = true; oas.AccessToken = getAccessToken(); IConsumerRequest icr = oas.Request(); icr = icr.Get(); icr = icr.ForUrl(QB_DISCONNECT); icr = icr.SignWithToken(); var ret = icr.ToWebResponse(); if (ret.StatusCode.ToInt() == 200) { return(true); } else { return(false); } }
/// <summary> /// Core Logic for Blue Dot Menu /// Error Handling: If the OAuth access token has expired or is invalid for some other reason, /// then the HTTP status code is 200, and the HTML returned shows the Connect to QuickBooks button within the Intuit "blue dot" menu. /// If an internal error is detected, then the HTTP status code returned is not 2xx, and the HTML returned will display the following text in the menu: "We are sorry, but we cannot load the menu right now." /// </summary> protected void GetBlueDotMenu() { Session["serviceEndPoint"] = Constants.IaEndPoints.BlueDotAppMenuUrl; OAuthConsumerContext consumerContext = new OAuthConsumerContext { ConsumerKey = ConfigurationManager.AppSettings["consumerKey"].ToString(), SignatureMethod = SignatureMethod.HmacSha1, ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"].ToString() }; OAuthSession oSession = new OAuthSession(consumerContext, Constants.OauthEndPoints.IdFedOAuthBaseUrl + Constants.OauthEndPoints.UrlRequestToken, Constants.OauthEndPoints.AuthorizeUrl, Constants.OauthEndPoints.IdFedOAuthBaseUrl + Constants.OauthEndPoints.UrlAccessToken); oSession.ConsumerContext.UseHeaderForOAuthParameters = true; oSession.AccessToken = new TokenBase { Token = Session["accessToken"].ToString(), ConsumerKey = ConfigurationManager.AppSettings["consumerKey"].ToString(), TokenSecret = Session["accessTokenSecret"].ToString() }; IConsumerRequest conReq = oSession.Request(); conReq = conReq.Get(); conReq = conReq.ForUrl(Session["serviceEndPoint"].ToString()); try { conReq = conReq.SignWithToken(); } catch (Exception ex) { throw ex; } string header = conReq.Context.GenerateOAuthParametersForHeader(); try { txtServiceResponse = conReq.ReadBody(); Response.Write(txtServiceResponse); } catch (WebException we) { HttpWebResponse rsp = (HttpWebResponse)we.Response; if (rsp != null) { try { using (StreamReader reader = new StreamReader(rsp.GetResponseStream())) { txtServiceResponse = txtServiceResponse + rsp.StatusCode + " | " + reader.ReadToEnd(); } } catch (Exception) { txtServiceResponse = txtServiceResponse + "Status code: " + rsp.StatusCode; } } else { txtServiceResponse = txtServiceResponse + "Error Communicating with Intuit Anywhere" + we.Message; } } }
public IConsumerResponse RunConsumerRequest(IConsumerRequest consumerRequest) { int retryCounter = 2; while (retryCounter-- > 0) { IConsumerResponse consumerResponse = ConsumerRequestRunner.Run(consumerRequest); LogMessage(consumerRequest, consumerResponse); if (consumerResponse.IsForbiddenResponse) { // Catch http 403 errors generated by IIS that are actually html pages warning about certificate issues.. throw new AuthenticationException(string.Format("The API server returned http {0} with content type {1}. See the inner exception for more details.", (int)consumerResponse.ResponseCode, consumerResponse.ContentType), consumerResponse.WebException); } if (consumerResponse.IsTokenExpiredResponse && !string.IsNullOrEmpty(consumerRequest.Context.SessionHandle)) { // Refresh the access token and try again.. AccessToken newAccessToken = RenewAccessToken(); consumerRequest.SignWithToken(newAccessToken, false); continue; } if (consumerResponse.IsOAuthProblemResponse) { // A usable response wasn't returned.. throw new OAuthException(consumerResponse, consumerRequest.Context, consumerResponse.ToProblemReport()); } return consumerResponse; } throw new ApplicationException("The consumer request could not be executed into a valid consumer response"); }
/// <summary> /// Calls the Xero API. /// </summary> /// <param name="consumerRequest">The consumer request.</param> /// <param name="accessTokenRepository">The access token repository.</param> /// <returns></returns> private Response CallXeroApiInternal(IConsumerRequest consumerRequest, ITokenRepository <AccessToken> accessTokenRepository) { AccessToken accessToken = accessTokenRepository.GetToken(""); if (accessToken == null) { return(new Response { Status = "NotConnected" }); } if (accessToken.HasExpired()) { accessToken = RenewAccessToken(accessTokenRepository); } if (accessToken.HasExpired()) { return(new Response { Status = "AccessTokenExpired" }); } // At this point, we should have a valid a consumerRequest.SignWithToken(accessToken); HttpWebResponse webResponse; try { webResponse = consumerRequest.ToWebResponse(); } catch (WebException ex) { if (ex.Response is HttpWebResponse) { HttpWebResponse httpWebResponse = (HttpWebResponse)ex.Response; if (httpWebResponse.StatusCode == HttpStatusCode.NotFound) { return(new Response { Status = "NotFound" }); } } return(new Response { Status = "Web Exception: " + ex.Message }); } catch (OAuthException ex) { return(new Response { Status = "OAuth Exception: " + ex.Report }); } catch (Exception ex) { return(new Response { Status = "Exception: " + ex.Message }); } return(ModelSerializer.DeSerializer <Response>(webResponse.GetResponseStream().ReadToEnd())); }