Last active
December 19, 2015 05:39
-
-
Save IntuitDeveloperRelations/5905520 to your computer and use it in GitHub Desktop.
DevDefined / IPP .NET DevKit v2 - QBD - PurchaseOrder Query #DotNetDevKitV2 #QBD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public string GetQbdPurchaseOrders(DataServices dataServices, int startPage, int chunkSize) | |
{ | |
HttpWebRequest httpWebRequest = WebRequest.Create(dataServices.ServiceContext.BaseUrl + "purchaseorder/v2/" + dataServices.ServiceContext.RealmId) as HttpWebRequest; | |
httpWebRequest.Method = "POST"; | |
httpWebRequest.ContentType = "text/xml"; | |
string requestBody = String.Format("<PurchaseOrderQuery xmlns=\"http://www.intuit.com/sb/cdm/v2\"><StartPage>{0}</StartPage><ChunkSize>{1}</ChunkSize></PurchaseOrderQuery>", startPage, chunkSize); | |
httpWebRequest.Headers.Add("Authorization", GetDevDefinedOAuthHeader(dataServices, httpWebRequest, requestBody.ToString())); | |
UTF8Encoding encoding = new UTF8Encoding(); | |
byte[] content = encoding.GetBytes(requestBody.ToString()); | |
using (var stream = httpWebRequest.GetRequestStream()) | |
{ | |
stream.Write(content, 0, content.Length); | |
} | |
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; | |
using (Stream data = httpWebResponse.GetResponseStream()) | |
{ | |
return new StreamReader(data).ReadToEnd(); | |
} | |
} | |
private string GetDevDefinedOAuthHeader(DataServices dataServices, HttpWebRequest webRequest, string requestBody) | |
{ | |
OAuthRequestValidator oauthValidator = (OAuthRequestValidator)dataServices.ServiceContext.RequestValidator; | |
OAuthConsumerContext consumerContext = new OAuthConsumerContext | |
{ | |
ConsumerKey = oauthValidator.ConsumerKey, | |
SignatureMethod = SignatureMethod.HmacSha1, | |
ConsumerSecret = oauthValidator.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 = oauthValidator.AccessToken, | |
ConsumerKey = oauthValidator.ConsumerKey, | |
TokenSecret = oauthValidator.AccessTokenSecret | |
}; | |
IConsumerRequest consumerRequest = oSession.Request(); | |
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment