/// <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); } }
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(""); }
/// <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; } } }
static void Main(string[] args) { var consumerContext = new OAuthConsumerContext { SignatureMethod = SignatureMethod.HmacSha1, ConsumerKey = "dai4siwkt27bo3r", // this is just a sample app setup for demo purposes called "DevDefinedOAuthTest", it's a sandbox app in "Development" mode. ConsumerSecret = "emsponeqnebimbm", UseHeaderForOAuthParameters = true // this is required for the PUT request with raw body to succeed. }; var session = new OAuthSession(consumerContext, "https://api.dropbox.com/1/oauth/request_token", "https://www.dropbox.com/1/oauth/authorize", "https://api.dropbox.com/1/oauth/access_token"); IToken requestToken = session.GetRequestToken(); string authorisationUrl = session.GetUserAuthorizationUrlForToken(requestToken); Console.WriteLine("Authorization Url: {0}", authorisationUrl); Process.Start(authorisationUrl); Console.WriteLine(); Console.WriteLine("Press enter once authorization complete"); Console.ReadLine(); session.ExchangeRequestTokenForAccessToken(requestToken); string accountInfo = session.Request().Get().ForUrl("https://api.dropbox.com/1/account/info").ReadBody(); Console.WriteLine("Account info: {0}", accountInfo); Console.WriteLine(); Console.WriteLine("Press enter to continue (will put file into sandbox)"); Console.ReadLine(); Console.WriteLine("Uploading..."); const string root = "sandbox"; // can also be "dropbox" (if app has all folders access) string fileName = "img_" + Guid.NewGuid() + ".png"; string putUrl = string.Format("https://api-content.dropbox.com/1/files_put/{0}/{1}", root, fileName); byte[] contents = File.ReadAllBytes("DevDefinedOAuthTitle.png"); IConsumerRequest putRequest = session.Request().Put().ForUrl(putUrl).WithRawContent(contents); string putInfo = putRequest.ReadBody(); Console.WriteLine("Put response: {0}", putInfo); Console.WriteLine(); Console.WriteLine("Press enter to exit"); Console.ReadLine(); }