Quickbooks api 无法上传附件 [英] Quickbooks api cannot upload Attachment

查看:120
本文介绍了Quickbooks api 无法上传附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to upload a pdf file on Quickbooks, using Intuit api and C#.

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

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.

这是我尝试过的

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

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

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.

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

I am getting returned result as Null when calling service.Upload method here

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

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

I can see Stream is fine and attachable data is also passed properly.

推荐答案

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

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

这篇关于Quickbooks api 无法上传附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆