andrevalentin / Tradeshift-C-REST-API-Sample

0 stars 0 forks source link

POST requests #1

Open zeezment opened 6 years ago

zeezment commented 6 years ago

Hi,

I know it's been a while since you've worked on integration with Tradeshift but do you by any chance have the sample for dispatching documents to TS or any POST requests? All my GET requests work with your sample code but I get a 401 unauthorized trying to use the same format when dispatching documents from my .net application. The POST requests with the same OAuth 1.0a standard work from Postman. See Code below:

`var url = "https://api.tradeshift.com/tradeshift/rest/external/documents/dispatcher?documentId=" + Guid.NewGuid()+"&documentProfileId=tradeshift.invoice.1.0";

        OAuthRequest OAuthclient = new OAuthRequest
        {
            Method = "POST",
            Type = OAuthRequestType.AccessToken,
            SignatureMethod = OAuthSignatureMethod.HmacSha1,
            ConsumerKey = "MyCKey",
            ConsumerSecret = "MyCSecret",
            Token = "MyToken",
            TokenSecret = "MySecret",
            RequestUrl = url,
            Version = "1.0"
        };
        var auth = OAuthclient.GetAuthorizationHeader();
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("X-Tradeshift-TenantId", "ts-tennantid");;
            client.DefaultRequestHeaders.Add("Authorization", auth);
            client.DefaultRequestHeaders.Add("User-Agent", "TestApp/0.1");

            HttpContent _Body = new StringContent("<?xml version='1.0' encoding='utf-8'?><Invoice xmlns:cac='urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' xmlns:cbc='urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' xmlns='urn:oasis:names:specification:ubl:schema:xsd:Invoice-2'><cbc:UBLVersionID>2.0</cbc:UBLVersionID><cbc:CustomizationID>urn:tradeshift.com:ubl-2.0-customizations:2010-06</cbc:CustomizationID><cbc:ProfileID schemeID='CWA 16073:2010' schemeAgencyID='CEN/ISSS WS/BII' schemeVersionID='1'>urn:www.cenbii.eu:profile:bii04:ver1.0</cbc:ProfileID><cbc:ID>1100110011</cbc:ID><cbc:IssueDate>2012-04-18</cbc:IssueDate><cbc:DocumentCurrencyCode>USD</cbc:DocumentCurrencyCode><cac:AccountingSupplierParty><cac:Party><cac:PartyIdentification><cbc:ID schemeID='GB:VAT'>GB123456789</cbc:ID></cac:PartyIdentification><cac:PartyName><cbc:Name>AATest3DE Company</cbc:Name></cac:PartyName><cac:PostalAddress><cbc:AddressFormatCode listAgencyID='6' listID='UN/ECE 3477' listVersionID='D08B'>5</cbc:AddressFormatCode><cbc:StreetName>DubStreet</cbc:StreetName><cbc:BuildingNumber>243</cbc:BuildingNumber><cbc:CityName>Dublin</cbc:CityName><cbc:PostalZone>32352</cbc:PostalZone><cac:Country><cbc:IdentificationCode>za</cbc:IdentificationCode></cac:Country></cac:PostalAddress><cac:Contact><cbc:ID>123</cbc:ID><cbc:Name>Gert Sylvest</cbc:Name><cbc:ElectronicMail>gert138@mailinator.com</cbc:ElectronicMail></cac:Contact></cac:Party></cac:AccountingSupplierParty><cac:AccountingCustomerParty><cac:Party><cac:PartyIdentification><cbc:ID schemeID='GB:VAT'>GB123456789</cbc:ID></cac:PartyIdentification><cac:PartyName><cbc:Name>gert136spain</cbc:Name></cac:PartyName><cac:PostalAddress><cbc:AddressFormatCode listAgencyID='6' listID='UN/ECE 3477' listVersionID='D08B'>5</cbc:AddressFormatCode><cbc:StreetName>asdg</cbc:StreetName><cbc:BuildingNumber>24</cbc:BuildingNumber><cbc:CityName>asdgha</cbc:CityName><cbc:PostalZone>23452</cbc:PostalZone><cac:Country><cbc:IdentificationCode>ZA</cbc:IdentificationCode></cac:Country></cac:PostalAddress><cac:Contact><cbc:ID>111</cbc:ID><cbc:ElectronicMail>gert136@mailinator.com</cbc:ElectronicMail></cac:Contact></cac:Party></cac:AccountingCustomerParty><cac:LegalMonetaryTotal><cbc:LineExtensionAmount currencyID='RUB'>0</cbc:LineExtensionAmount><cbc:PayableAmount currencyID='RUB'>0</cbc:PayableAmount></cac:LegalMonetaryTotal><cac:InvoiceLine><cbc:ID>264</cbc:ID><cbc:InvoicedQuantity unitCode='HUR'>3</cbc:InvoicedQuantity><cbc:LineExtensionAmount currencyID='RUB'>75</cbc:LineExtensionAmount><cac:TaxTotal><cbc:TaxAmount currencyID='USD'>0</cbc:TaxAmount><cac:TaxSubtotal><cbc:TaxableAmount currencyID='USD'>75</cbc:TaxableAmount><cbc:TaxAmount currencyID='USD'>0</cbc:TaxAmount><cac:TaxCategory><cbc:ID schemeID='UN/ECE 5305' schemeAgencyID='6' schemeVersionID='D08B'>Z</cbc:ID><cbc:Percent>0</cbc:Percent><cac:TaxScheme><cbc:ID schemeID='UN/ECE 5153 Subset' schemeAgencyID='6' schemeVersionID='D08B'>OTH</cbc:ID><cbc:Name>VAT</cbc:Name></cac:TaxScheme></cac:TaxCategory></cac:TaxSubtotal></cac:TaxTotal><cac:Item><cbc:Description>item 2</cbc:Description><cbc:Name>item 2</cbc:Name></cac:Item><cac:Price><cbc:PriceAmount currencyID='USD'>75</cbc:PriceAmount><cbc:BaseQuantity>1</cbc:BaseQuantity><cbc:OrderableUnitFactorRate>1</cbc:OrderableUnitFactorRate></cac:Price></cac:InvoiceLine><cac:InvoiceLine><cbc:ID>234</cbc:ID><cbc:InvoicedQuantity unitCode='EA'>1</cbc:InvoicedQuantity><cbc:LineExtensionAmount currencyID='EUR'>200.00</cbc:LineExtensionAmount><cac:TaxTotal><cbc:TaxAmount currencyID='EUR'>42.00</cbc:TaxAmount><cac:TaxSubtotal><cbc:TaxableAmount currencyID='EUR'>200.00</cbc:TaxableAmount><cbc:TaxAmount currencyID='EUR'>42.00</cbc:TaxAmount><cac:TaxCategory><cbc:ID schemeAgencyID='6' schemeID='UN/ECE 5305' schemeVersionID='D08B'>S</cbc:ID><cbc:Percent>21</cbc:Percent><cac:TaxScheme><cbc:ID schemeAgencyID='6' schemeID='UN/ECE 5153 Subset' schemeVersionID='D08B'>VAT</cbc:ID><cbc:Name>VAT</cbc:Name></cac:TaxScheme></cac:TaxCategory></cac:TaxSubtotal></cac:TaxTotal><cac:Item><cbc:Description>Test</cbc:Description><cbc:Name>Test</cbc:Name><cac:SellersItemIdentification><cbc:ID schemeAgencyID='9' schemeID='GTIN'>2</cbc:ID></cac:SellersItemIdentification></cac:Item><cac:Price><cbc:PriceAmount currencyID='EUR'>200.00</cbc:PriceAmount><cbc:BaseQuantity unitCode='EA'>1</cbc:BaseQuantity><cbc:OrderableUnitFactorRate>1</cbc:OrderableUnitFactorRate></cac:Price></cac:InvoiceLine></Invoice>", Encoding.UTF8, "application/xml");
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 |
                                   SecurityProtocolType.Tls11 |
                                   SecurityProtocolType.Tls |
                                   SecurityProtocolType.Ssl3;

            return await client.PostAsync("", _Body);`

Any assistance would be much appreciated. Thanks

andrevalentin commented 6 years ago

Hi @zeezment, I will try to help you even though it's been a while 🙂 The first thing that comes to mind is the missing Content-Type header on this request, you should add the following line to your client. client.DefaultRequestHeaders.Add("Content-Type", "application/xml"); The rest actually looks good, and this would not cause an error with a GET call. Let me know if this works.

zeezment commented 6 years ago

Hey @Lozzano! Thank you so much for responding. I had added the Body Content-Type to the body HttpContent variable i.e. HttpContent _Body = new StringContent(xmlString, Encoding.UTF8, "application/xml"); but I'll try adding it to the header explicitly and confirm this.

zeezment commented 6 years ago

@Lozzano when I added the content-type to the headers I got a "Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects." and hence as mentioned above I added it to the HttpContent object instead

andrevalentin commented 6 years ago

@zeezment could you let me know what your TenantID on Tradeshift is, and which environment, prod or sandbox you are using to test this one? I would love to have a quick look at your account. Do note that the TenantId isn't secret information so sharing it here isn't considered a security issue.

ssted1098 commented 3 years ago

@andrevalentin I have the same problem. But testing with Postman is OK