【问题标题】:Quickbooks api cannot upload AttachmentQuickbooks api 无法上传附件
【发布时间】:2019-09-22 08:01:08
【问题描述】:

我正在尝试使用 Intuit api 和 C# 在 Quickbooks 上上传 pdf 文件。

我正在使用此处提供的代码 http://developer.qbapi.com/Add-an-attachment-using-AttachableRef.aspx

但我无法上传任何文件,也没有收到任何错误。

这是我尝试过的

    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;
    }

我无法理解上述代码有什么问题,因为我也没有收到任何错误,并且在快速手册的附件列表中看不到任何文件。

在此处调用 service.Upload 方法时,我得到的返回结果为 Null

 Intuit.Ipp.Data.Attachable uploaded = service.Upload(attachable, stream);

我可以看到Stream 很好,attachable 数据也正确传递。

【问题讨论】:

    标签: c# quickbooks quickbooks-online


    【解决方案1】:

    我在解决之前遇到了这个问题。上传返回值始终为空,有点忽略它。

    在这里为我提供示例工作代码

    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();
     }}
    

    【讨论】:

    • 谢谢,但我的问题是我为 AttachableRef.EntityRef.Value 传递了错误的值,所以它在 C# 代码中返回 null,而不是显示错误。使用 Fiddler 调用 api 时发现错误。
    猜你喜欢
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    相关资源
    最近更新 更多