Upload File to Quickbooks online with IDS V3 - file-upload

I would like to upload a file to QuickBooks Online using IDS V3.
I followed the steps described here
https://developer.intuit.com/docs/0025_quickbooksapi/0050_data_services/020_key_concepts/attachments.
Here is my source code
URL url = new URL("https://quickbooks.api.intuit.com/v3/company/My_company_ID/upload");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.setDoOutput(true);
request.setRequestMethod("POST");
HttpParameters para = new HttpParameters();
//String status = URLEncoder.encode("中 文","utf-8").replaceAll("\\+", "%20");
//para.put("status", status);
String boundary = "---------------------------37531613912423";
//String content = "--"+boundary+"\r\nContent-Disposition: form-data; name=\"status\"\r\n\r\n";
String pic = "\r\n--"+boundary+"\r\nContent-Disposition: form-data; name=\"pic\"; filename=\"postpic.gif\"\r\nContent-Type: image/gif\r\n\r\n";
byte[] end_data = ("\r\n--" + boundary + "--\r\n").getBytes();
File f = new File("/Users/cnanfack/Documents/oml_map1.gif");
FileInputStream stream = new FileInputStream(f);
byte[] file = new byte[(int)f.length()];
stream.read(file);
String lineEnd = "\r\n";
String header = "Content-Disposition: form-data; name=\"file_metadata_0\""+lineEnd
+"Content-Type: application/xml; charset=UTF-8"+lineEnd
+"Content-Transfer-Encoding: 8bit"+lineEnd;
String attachable = header+"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Attachable xmlns=\"http://schema.intuit.com/finance/v3\" domain=\"QBO\" sparse=\"false\">"
+"<EntityRef type=\"Bill\">285</EntityRef>"
+"<Size>4099</Size>"
+"<ContentType>image/gif</ContentType>"
+"<FileName>postpic.gif</FileName>"
+"</Attachable>";
request.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
//request.setRequestProperty("Content-Length", String.valueOf(boundary.getBytes().length+"test".getBytes().length+pic.getBytes().length+f.length()+end_data.length));
//consumer.setAdditionalParameters(para);
consumer.sign(request);
OutputStream ot = request.getOutputStream();
ot.write(end_data);
ot.write(attachable.getBytes());
ot.write(end_data);
//ot.write(status.getBytes());
ot.write(pic.getBytes());
ot.write("Content-Transfer-Encoding: binary\r\n\r\n".getBytes());
ot.write(file);
ot.write(end_data);
ot.flush();
ot.close();
System.err.println("Sending request...");
request.connect();
System.err.println("Response: " + request.getResponseCode() + " "
+ request.getResponseMessage());
BufferedReader reader =new BufferedReader(new InputStreamReader(request.getInputStream()));
String b = null;
while((b = reader.readLine())!=null){
System.err.println(b);
}
The returned result:
Response: 200 OK
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2013-11-10T20:24:27.823-08:00"/>
But I don't see the file on Quickbooks Online.
Maybe I miss something. Can someone help me?
Thank you in advance

I had tried this Upload endpoint call using Java Devkit. It is working fine. PFB details.
- Request URI : https://quickbooks.api.intuit.com/v3/company/688779980/upload? Http
- Method : POST Content-Disposition: form-data; name="file_metadata_2bddf"
- Content-Type: application/xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Attachable xmlns="http://schema.intuit.com/finance/v3">
<FileName>images.jpg</FileName>
<ContentType>image/jpeg</ContentType>
<Lat>25.293112341223</Lat>
<Long>-21.3253249834</Long>
<PlaceName>Fake Place</PlaceName>
<Note>Attachable note 900a49e1</Note>
<Tag>Attachable tag 900a49e1</Tag>
</Attachable>
Response XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2013-11-12T05:58:25.296-08:00">
<AttachableResponse>
<Attachable domain="QBO" sparse="false">
<Id>100000000000543524</Id>
<SyncToken>0</SyncToken>
<MetaData>
<CreateTime>2013-11-12T05:58:26-08:00</CreateTime>
<LastUpdatedTime>2013-11-12T05:58:26-08:00</LastUpdatedTime>
</MetaData>
<FileName>images.jpg</FileName>
<FileAccessUri>/v3/company/688779980/download/100000000000543524</FileAccessUri>
<TempDownloadUri>https://intuit-qbo-prod-19.s3.amazonaws.com/688779980%2Fattachments%2Fimages-1384264705362.jpg?Expires=1384265606&AWSAccessKeyId=AKIAJEXDXKNYCBUNCCCQ&Signature=ESZPzeO%2B5YWxw8VOWMVMBRnvIdw%3D</TempDownloadUri>
<Size>58616</Size>
<ContentType>image/jpeg</ContentType>
<Lat>25.293112341223</Lat>
<Long>-21.3253249834</Long>
<PlaceName>Fake Place</PlaceName>
<Note>Attachable note 8219e79a</Note>
<Tag>Attachable tag 8219e79a</Tag>
<ThumbnailFileAccessUri>/v3/company/688779980/attachable-thumbnail/100000000000543524</ThumbnailFileAccessUri>
</Attachable>
</AttachableResponse>
</IntuitResponse>
From the ThumnailFileAccessUri property I got the following attachment URI which I had used for Download endpoint.
/v3/company/688779980/attachable-thumbnail/100000000000543524
Download Endpoint(GET)-
https://quickbooks.api.intuit.com/v3/company/688779980/download/100000000000543521
JAVA/.Net SDK download link - https://developer.intuit.com/docs/0025_quickbooksapi/0055_devkits
Hope it will be useful.
EDIT- Adding Java code snippet
public static void executeUploadForImageFilePoc(DataService service) throws ParseException, FMSException, FileNotFoundException {
System.out.println("executeUploadForImageFile");
String uuid = UUID.randomUUID().toString().substring(0, 8);
Attachable attachable1 = new Attachable();
//attachable.setSize(new Long("34234"));
attachable1.setLat("25.293112341223");
attachable1.setLong("-21.3253249834");
attachable1.setPlaceName("Fake Place");
attachable1.setNote("Attachable note " + uuid);
attachable1.setTag("Attachable tag " + uuid);
Attachable attachable = attachable1;
attachable.setFileName("images.jpg");
attachable.setContentType("image/jpeg");
File file = new File("C:\\images_new.jpg");
InputStream in = new FileInputStream(file);
//Upload endpoint call
Attachable attachableOutput = service.upload(attachable, in);
//Check return XML
attachable.getFileAccessUri();
System.out.println(attachableOutput.getFileAccessUri());
System.out.println(attachableOutput.getFileName());
//Download endpoint call
InputStream output = service.download(attachableOutput);
try {
BufferedImage bImageFromConvert = ImageIO.read(output);
ImageIO.write(bImageFromConvert, "jpg", new File("C:\\images_new_manas.jpg"));
} catch (IOException e) {
throw new FMSException("Error while reading the upload file.", e);
}
}
Thanks

I have a working code in dotnet. See if it works for you:
DataService.DataService commonServiceQBO= new DataService.DataService(qboContextoAuth);
string imagePath = string.Concat(AppDomain.CurrentDomain.BaseDirectory, "\\", "Services\\Resource\\image.jpg");
System.IO.FileInfo file = new System.IO.FileInfo(imagePath);
Attachable attachable = new Attachable();
attachable.AttachableRef = new AttachableRef[1];
attachable.AttachableRef[0].EntityRef = new ReferenceType();
attachable.AttachableRef[0].EntityRef.type = "Bill";
attachable.AttachableRef[0].EntityRef.Value = "12";
attachable.Lat = "25.293112341223";
attachable.Long = "-21.3253249834";
attachable.PlaceName = "Fake Place";
attachable.Note = "Attachable note123 ";
attachable.Tag = "Attachable tag123 ";
using (System.IO.FileStream fs = file.OpenRead())
{
attachable.ContentType = "image/jpeg";
attachable.FileName = file.Name;
Attachable attachableUploaded = commonServiceQBO.Upload(attachable, fs);
}
For download use:
byte[] responseByte = service.Download(attachableUploaded);

Related

Rest API return response XML format, but same xml not get in result by using httpclient

I have created web api which return xml format response. like below
<TXLife xmlns="http://ACORD.org/Standards/Life/2">
<TXLifeResponse>
<TransRefGUID>61ec7f39-5744-410e-b601-dcd89d8d6f27</TransRefGUID>
<TransType tc="510">Form Instance Update</TransType>
<TransSubType tc="1022500310">Annuity Application for Mani ManiM</TransSubType>
<TransExeDate>2016-08-25-04:00</TransExeDate>
<TransExeTime>16:36:41.157-04:00</TransExeTime>
<TransMode tc="2">Original</TransMode>
<NoResponseOK tc="0"/>
<TransResult>
<ResultCode tc="3">Received Pending</ResultCode>
</TransResult>
But when call API using httpclient, I received response but response data is not same as above instead i got decode of special character.
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"><?
xml version="1.0" encoding="utf-8"?>
<TXLife xmlns:acord="http://ACORD.org/Standards/Life/2">
<TXLifeResponse>
<TransRefGUID>dd8973e3-ac03-4690-908b-
65da3d6a770f</TransRefGUID>
<TransType tc="510">Form Instance Update</TransType>
<TransSubType tc="1022500310">
Annuity Application for SUZANNE M PERSON</TransSubType>
<TransExeDate>2020-08-17</TransExeDate>
<TransExeTime>15:28:24Z</TransExeTime>
<TransMode tc="2">Original</TransMode>
<NoResponseOK tc="0" />
<TransResult>
<ResultCode tc="3">Received Pendinge</ResultCode>
</TransResult>
</TXLifeResponse>
</TXLife></string>
I am using below code to call API
string response = string.Empty;
using (var client = new HttpClient())
{
client.BaseAddress = parameters.DestinationUri;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
var responseTask = client.PostAsXmlAsync(parameters.DestinationUri, request);
responseTask.Wait();
var result = responseTask.Result;
if (result.IsSuccessStatusCode)
{
response = result.Content.ReadAsStringAsync().Result;
}
Resolved. I fixed by just in API response. I return HttpResponseMessage instead of return string object. Changes in API like below
string response = "response string info";
return new HttpResponseMessage()
{
Content = new StringContent(response, Encoding.UTF8, "application/xml")
};

Consume tensor-flow serving inception model using C# client (PREDICT API)

I have been trying to implement a C# client application to interact with Tensorflow serving server for few weeks now with no success. I have a Python client which works successfully but I can not replicate its functionality with C#. The Python client
import requests
#import json
from keras.preprocessing.image import img_to_array, array_to_img, load_img
from keras.preprocessing import image
flowers = 'c:/flower_photos/cars/car1.jpg'
image1 = img_to_array(image.load_img(flowers, target_size=(128,128))) / 255
payload = {
"instances": [{"image":image1.tolist()},
]
}
print("sending request...")
r = requests.post('http://localhost:8501/v1/models/flowers/versions/1:predict', json=payload)
print(r.content)
The server responds correctly. I am using Tensorflow version 1.12.0 with corresponding latest serving image. They are all working fine.
According to REST API documentation, the API structure is given but its not clear to me at all. I need to send the image to server. How could I add the image payload to JSON request in C# ? After going through many sites, I found that image should be in base64string.
So I did the image conversion into base64
private string GetBase64ImageBytes(string ImagePath)
{
using (Image image = Image.FromFile(ImagePath))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
}
The request portion is as follows : (server responds with metadata correctly for the GET request)
public string PostImageToServerAndClassify(string imageArray)
{
//https://stackoverflow.com/questions/9145667/how-to-post-json-to-a-server-using-c
string result = null;
string ModelName = cmbProjectNames.Text.Replace(" ", "");
string status_url = String.Format("http://localhost:{0}/v1/models/{1}/versions/{2}:predict", txtPort.Text, ModelName, txtVersion.Text);
var httpWebRequest = (HttpWebRequest)WebRequest.Create(status_url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
try
{
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
// string json = "{"+ #"""instances""" + ": [{" + #"""image""" + #":" + imageArray + "}]}";
// imageArray = #""" + imageArray + #""";
string json = "{ " + #"""instances""" + ": [{" + #"""image""" + #": { " + #"""b64"": """ + imageArray + #"""}}]}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
catch (Exception ex)
{
result = ex.Message;
}
return result;
}
With the POST request, I get the error message "The remote server returned an error: (400) Bad Request.". Also server terminates its service. In Postman I get
the detailed error info as :
{ "error": "Failed to process element: 0 key: image of \'instances\' list. Error: Invalid argument: JSON Value: {\n \"b64\": \"/9j/4AAQSkZJRgABAQEAAAAAAAD/4QBSRXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAZKGAAcAAAAcAAAALAAAAABVTklDT0RFAABBAHAAcABsAGUATQBhAHIAaw ... .....(image string data)
So this feels like that I am sending the incorrect data format.
Could someone please tell me what is wrong here ? Any example of image conversion and POST request is highly appreciated. I can not find anywhere that base64string format is the right format for the image in TF site. Python client data format is different hence really need to know what is the right format with any reference documents.
The nearest reference I found here with JAVA client but did not work with mine may be due to TF version difference.

c# console application returning no results

This code runs fine in my windows form application using .net framework 4.6.2 but when I go to make it a console application so it can be ran from the task scheduler I get no results. I think I am losing something in translation.
RestClient restClient = new RestClient("https://api.vault.com");
string refreshToken = #"abc";
string encodedClientIdSecret = Base64Encode("AP-123");
string responseStr = "";
string url = "/v1/OAuth";
dynamic jsonObj = "";
RestRequest request = new RestRequest(url, Method.POST);
request.AddHeader("Authorization", encodedClientIdSecret);
request.AddParameter("grant_type", "refresh_token");
request.AddParameter("refresh_token", refreshToken);
IRestResponse response;
restClient.Execute(request);
response = restClient.Execute(request);
Console.WriteLine(response.Content + " || " + encodedClientIdSecret);
Console.ReadKey();
jsonObj = JsonConvert.DeserializeObject(response.Content);
responseStr = jsonObj.access_token;
return responseStr;
It basically tells me the value cannot be null, and when I look at response.Content I get nothing and the status code comes back as "0". Any thoughts?
Just added:
//Required For SSL/TLS Error Start
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
//Required For SSL/TLS Error End
and I got my results. Hope this helps someone else!

SMS integration web api consume by XML via HTTP RAW POST in c#.net

I need to integrate send sms api using like this URL. but I am getting the message XML is incorrect format.
http://<url>/action?application=msg&action=sendmsg&responsetype=xml
I am sending XML code like this
<data>
<phone> 1234567890 </phone>
<api_key 1234567890 </api_key>
<message_body> This is message text </message_body>
</data>
My code is
HttpWebRequest httpWReq =
(HttpWebRequest)WebRequest.Create(#"http:\\domain.com\page.asp");
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username=user";
postData += "&password=pass";
byte[] data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data,0,data.Length);
}
HttpWebResponse response = (HttpWebResponse)HttpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Please suggest how to resolve.strong text
Looks like just a typo.
Add > to end of the front api_key and the balance XML needs will work.

Embedded Signing api docusign

I'm using DocuSign to add eSignature to my requests and everything's working well. Right now I send my signature requests,by using Embedded method- to initiate my workflows immediately by navigating to a URL.
After login,and execute the bellow code,i get the (Embedded View: https://demo.docusign.net/Member/StartInSession.aspx?StartConsole=1&t=32598057-5a59-4d0b-bad8-a8ff8f2407f6&DocuEnvelope=168bc155-e013-4ffd-abb4-7608b56647f8&send=1), but whene i paste the url to try signing document in navigate, but will redirect me to an other url is(http://www.docusign.com/?event=Send&envelopeId=168bc155-e013-4ffd-abb4-7608b56647f8),
how can'i start wotkflow process to sign my enveloppe ?? i can't see my enveloppe to sign it.
// STEP 2 - Create an envelope with one recipient, document, and tab and send
//
String jsonBody = "{\"emailBlurb\":\"partail\"," +
"\"emailSubject\":\"API Call for adding signature request to document and sending\"," +
"\"documents\":[{" +
"\"documentId\":\"1\"," +
"\"name\":\"test.pdf\"}]," +
"\"recipients\":{" +
"\"signers\":[{" +
"\"email\":\"" + EmailRecipients + "\"," +
"\"name\":\"name\"," +
"\"recipientId\":\"1\"," +
"\"routingOrder\":\"1\","+
"\"clientUserId\":\"1000\","+
"\"tabs\":{" +
"\"signHereTabs\":[{" +
"\"xPosition\":\"300\"," +
"\"yPosition\":\"600\"," +
"\"documentId\":\"1\"," +
"\"pageNumber\":\"1\"" + "}]}}]}," +
"\"status\":\"sent\"}";
//DemandeSign.getenvelope();
File file = new File("D:/test.pdf");
InputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
inputStream.read(bytes);
inputStream.close();
String requestBody = "\r\n\r\n--BOUNDARY\r\n" +
"Content-Type: application/json\r\n" +
"Content-Disposition: form-data\r\n" +
"\r\n" +
jsonBody + "\r\n\r\n--BOUNDARY\r\n" + // our json formatted request body
"Content-Type: application/pdf\r\n" +
"Content-Disposition: file; filename=\"test.pdf\"; documentId=1\r\n" +
"\r\n";
// we break this up into two string since the PDF doc bytes go here and are not in string format.
// see further below where we write to the outputstream...
String reqBody2 = "\r\n" + "--BOUNDARY--\r\n\r\n";
// append "/envelopes" to the baseUrl and use in the request
conn = (HttpURLConnection)new URL(baseURL + "/envelopes").openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("X-DocuSign-Authentication", authenticateStr);
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=BOUNDARY");
conn.setRequestProperty("Content-Length", Integer.toString(requestBody.toString().length()));
conn.setRequestProperty("Accept", "application/xml");
// write the body of the request...
DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(requestBody.toString());
dos.write(bytes);
dos.writeBytes(reqBody2.toString());
dos.flush(); dos.close();
status = conn.getResponseCode(); // triggers the request
if( status != 201 ) // 201 = Created
{
System.out.println("Error calling webservice, status is: " + status);
InputStreamReader isr = new InputStreamReader( conn.getErrorStream() );
br = new BufferedReader(isr);
StringBuilder error_response = new StringBuilder();
while ( (line = br.readLine()) != null)
error_response.append(line);
System.out.println("Error response is " + error_response.toString() );
System.exit(-1);
}
// Read the response
InputStreamReader isr = new InputStreamReader( conn.getInputStream() );
br = new BufferedReader(isr);
StringBuilder response2 = new StringBuilder();
while ( (line = br.readLine()) != null)
response2.append(line);
//token1 = "//*[1]/*[local-name()='envelopeId']";
//String envelopeId = xPath.evaluate(token1, new InputSource(new StringReader(response2.toString())));
//--- display results
//System.out.println("Document sent! envelopeId is " + envelopeId );//envelopeId is e4c0659a-9d01-4ac3-a45f-02a80fd6bd96 at 04/07/2013 17:24
token1 = "//*[1]/*[local-name()='uri']";
String uri = xPath.evaluate(token1, new InputSource(new StringReader(response2.toString())));
//--- display results
System.out.println("uri = " + uri );
/// Step3
// construct another outgoing XML request body
String reqBody = "<returnUrlRequest xmlns=\"http://www.docusign.com/restapi\">" +
"<authenticationMethod>email</authenticationMethod>" +
"<email>***test#gmail.com***</email>" +
"<returnUrl>http://www.docusign.com</returnUrl>" +
"<userName>name</userName>" +
"<clientUserId>1000</clientUserId>" +
"</returnUrlRequest>";
// append uri + "/views/sender" to the baseUrl and use in the request
conn = (HttpURLConnection)new URL(baseURL + uri + "/views/sender").openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("X-DocuSign-Authentication", authenticateStr);
conn.setRequestProperty("Content-Type", "application/xml");
conn.setRequestProperty("Content-Length", Integer.toString(reqBody.length()));
conn.setRequestProperty("Accept", "application/xml");
// write the body of the request...
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(reqBody); dos.flush(); dos.close();
status = conn.getResponseCode(); // triggers the request
if( status != 201 ) // 201 = Created
{
System.out.println("Error calling webservice, status is: " + status);
System.exit(-1);
}
// Read the response
isr = new InputStreamReader( conn.getInputStream() );
br = new BufferedReader(isr);
StringBuilder response3 = new StringBuilder();
while ( (line = br.readLine()) != null)
response3.append(line);
token1 = "//*[1]/*[local-name()='url']";
//--- display results
System.out.println("Embedded View: " + xPath.evaluate(token1, new InputSource(new StringReader(response3.toString()))));`
Are you trying to access the URL immediately or are you waiting at all? Once you generate a URL token to access a given envelope it has a TTL (time to life) of 5 mins, meaning it expires after 5 minutes and you then need to generate a new one.
If that's not it, your problem might be related to how you are identifying your recipient. A recipient in DocuSign is identified by the unique combination of their name, email, recipientId, and in the case of embedding, the clientUserId. You seem to be setting all of those, however whatever the combination is when you first create the envelope, you need to refer to the same combination when you are requesting the Embedded URL token.
When you create your envelope I see you are setting the name literally to "name" and that you are setting the email through a variable called "EmailRecipients". But when you request the URL token you are using email "test#gmail.com", that might be causing your issue too.