问题
I am trying to upload a pdf file on Quickbooks, using Intuit api and C#.
I am using the code as provided here http://developer.qbapi.com/Add-an-attachment-using-AttachableRef.aspx
But I am not able to upload any file, also I am not getting any error.
Here is what i have tried
public ActionResult CallUpload()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
List<OidcScopes> scopes = new List<OidcScopes>();
scopes.Add(OidcScopes.Accounting);
OAuth2RequestValidator oauthValidator = new OAuth2RequestValidator("access_token_value");
// Create a ServiceContext with Auth tokens and realmId
ServiceContext serviceContext = new ServiceContext(realmIdValue, IntuitServicesType.QBO, oauthValidator);
serviceContext.IppConfiguration.MinorVersion.Qbo = "30";
serviceContext.IppConfiguration.BaseUrl.Qbo = "https://quickbooks.api.intuit.com/";
serviceContext.IppConfiguration.Message.Request.SerializationFormat = Intuit.Ipp.Core.Configuration.SerializationFormat.Json;
serviceContext.IppConfiguration.Message.Response.CompressionFormat = CompressionFormat.GZip;
AttachableUploadDownloadAddTestUsingoAuth(serviceContext);
return View();
}
public void AttachableUploadDownloadAddTestUsingoAuth(ServiceContext qboContextoAuth)
{
string imagePath = string.Concat(AppDomain.CurrentDomain.BaseDirectory, "\\", "Uploads\\SalesOrderFiles\\testing.pdf");
System.IO.FileInfo file = new System.IO.FileInfo(imagePath);
Intuit.Ipp.Data.Attachable attachable = CreateAttachableUpload(qboContextoAuth);
using (System.IO.FileStream fs = file.OpenRead())
{
//attachable.ContentType = "image/jpeg";
attachable.ContentType = "application/pdf";
attachable.FileName = file.Name;
attachable =Upload(qboContextoAuth, attachable, fs);
}
}
public static Intuit.Ipp.Data.Attachable Upload(ServiceContext context, Intuit.Ipp.Data.Attachable attachable, System.IO.Stream stream)
{
//Initializing the Dataservice object with ServiceContext
DataService service = new DataService(context);
Intuit.Ipp.Data.Attachable uploaded = service.Upload(attachable, stream);
return uploaded;
}
public static Intuit.Ipp.Data.Attachable CreateAttachableUpload(ServiceContext context)
{
Intuit.Ipp.Data.Attachable attachable = new Intuit.Ipp.Data.Attachable();
attachable.Lat = "25.293112341223";
attachable.Long = "-21.3253249834";
attachable.PlaceName = "Fake Place";
attachable.Note = "Attachable note " + Guid.NewGuid().ToString("N").Substring(0, 5);
attachable.Tag = "Attachable tag " + Guid.NewGuid().ToString("N").Substring(0, 5);
//For attaching to Invoice or Bill or any Txn entity, Uncomment and replace the Id and type of the Txn in below code
Intuit.Ipp.Data.AttachableRef[] attachments = new Intuit.Ipp.Data.AttachableRef[1];
Intuit.Ipp.Data.AttachableRef ar = new Intuit.Ipp.Data.AttachableRef();
ar.EntityRef = new Intuit.Ipp.Data.ReferenceType();
ar.EntityRef.type = Intuit.Ipp.Data.objectNameEnumType.Invoice.ToString();
ar.EntityRef.name = Intuit.Ipp.Data.objectNameEnumType.Invoice.ToString();
ar.EntityRef.Value = "3535"; //Add the Id of your invoice here
////ar.EntityRef.type = objectNameEnumType.Bill.ToString();
////ar.EntityRef.name = objectNameEnumType.Bill.ToString();
////ar.EntityRef.Value = "1484";
attachments[0] = ar;
attachable.AttachableRef = attachments;
return attachable;
}
I am not able to understand what is wrong in the above code, as I am also not getting any error, and cannot see any file on Attachments list on quickbooks.
I am getting returned result as Null when calling service.Upload method here
Intuit.Ipp.Data.Attachable uploaded = service.Upload(attachable, stream);
I can see Stream
is fine and attachable
data is also passed properly.
回答1:
I had this problem before solving it. Upload return value will always be null, sort of ignored it.
Sample working code for me here
Attachable attachable = Helper.CreateAttachableUpload(//some params);
byte[] bytes = //byte array;
if (bytes != null)
{
using (MemoryStream stream = new MemoryStream(bytes))
{
attachable.FileName = //filename;
attachable.ContentType = "image/jpeg";
var attachableUploaded = serviceContext.Upload(serviceContext, attachable, stream);
stream.Close();
}}
来源:https://stackoverflow.com/questions/58047226/quickbooks-api-cannot-upload-attachment