【问题标题】:Adobe Sign (echo sign) API sending document using C#Adobe Sign (echo sign) API 使用 C# 发送文档
【发布时间】:2016-09-07 01:45:47
【问题描述】:

好的,我对使用 API 的理解有限

我试图掌握 Adob​​e Sign API 并遇到了死胡同,在测试页面上我已经输入了它并且它可以工作

但我不知道如何在 C# 中做到这一点

我尝试了以下方法,但知道它缺少 OAuth 的东西,我只是不确定接下来要尝试什么。 顺便说一句 foo.GetAgreementCreationInfo() 只是获取屏幕截图中的字符串,我只是将它移出,因为它又大又丑

var foo = new Models();
var client = new RestClient("https://api.na1.echosign.com/api/rest/v5");
// client.Authenticator = new HttpBasicAuthenticator(username, password);
var request = new RestRequest("agreements/{AgreementCreationInfo}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("AgreementCreationInfo",                     foo.GetAgreementCreationInfo()); // replaces matching token in request.Resource
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string

【问题讨论】:

    标签: c# api asp.net-web-api restsharp echosign


    【解决方案1】:

    您误解了 API 文档。 API 中需要的 Access-Token 参数显然是 HTTP 标头,而 AgreementCreationInfo 只是 JSON 格式的请求正文。没有URI段,所以重写你的代码如下:

    var foo = new Models();
    //populate foo
    var client = new RestClient("https://api.na1.echosign.com/api/rest/v5");
    var request = new RestRequest("agreements", Method.POST);
    request.AddHeader("Access-Token", "access_token_here!");
    // request.AddHeader("x-api-user", "userid:jondoe"); //if you want to add the second header
    request.AddParameter("application/json", foo.GetAgreementCreationInfo(), ParameterType.RequestBody);
    
    IRestResponse response = client.Execute(request);
    var content = response.Content;
    

    还请注意,在 RESTSharp 中,您根本不需要手动将正文序列化为 JSON。如果您创建一个与最终 JSON 具有相同结构的强类型对象(或者只是一个匿名对象就足够了),RESTSharp 将为您序列化它。

    为了更好的方法,我强烈建议您替换此行:

    request.AddParameter("application/json", foo.GetAgreementCreationInfo(), ParameterType.RequestBody);
    

    这些:

    request.RequestFormat = DataFormat.Json;
    request.AddBody(foo);
    

    假设您的 foo 对象的类型为 Models,并且具有以下结构及其属性:

    public class Models
    {
        public DocumentCreationInfo documentCreationInfo { get; set; }
    }
    
    public class DocumentCreationInfo
    {
        public List<FileInfo> fileInfos { get; set; }
        public string name { get; set; }
        public List<RecipientSetInfo> recipientSetInfos { get; set; }
        public string signatureType { get; set; }
        public string signatureFlow { get; set; }
    }
    
    public class FileInfo
    {
        public string transientDocumentId { get; set; }
    }
    
    public class RecipientSetInfo
    {
        public List<RecipientSetMemberInfo> recipientSetMemberInfos { get; set; }
        public string recipientSetRole { get; set; }
    }
    
    public class RecipientSetMemberInfo
    {
        public string email { get; set; }
        public string fax { get; set; }
    }
    

    【讨论】:

    • 为此干杯,它工作正常。我知道我错了只是无法到达那里。我打算更改为传入的对象,但我有一个很大的 JSON 字符串,并且在它获得最基本的工作方式之前不会将其更改为对象。我使用 Edit>Paste Special> Json to Class 创建类
    • 是的,这是正确的做法。我很高兴这对您有所帮助。
    • 如何获取Access-Token?他们不是为了誓言而禁用它吗?是否所有用户都需要拥有 Adob​​e 帐户?
    • 在您帐户中的 API 设置下。它称为集成密钥。创建一个集成密钥,此密钥将在作为令牌的值传递时使用 [链接]secure.na2.echosign.com/public/docs/restapi/v6,然后您可以使用request.AddHeader("Authorization", "Bearer [YourIntegrationKey]");@Toolkit
    • 在您帐户中的 API 设置下。它称为集成密钥。创建一个集成密钥,此密钥将在作为令牌的值传递时使用 [secure.na2.echosign.com/public/docs/restapi/v6] 然后您可以使用request.AddHeader("Authorization", "Bearer [YourIntegrationKey]"); 您将能够实现无缝集成,因此您系统的用户不需要 Adob​​e帐户。但请记住,它将代表帐户所有者发送,参与者将使用电子邮件地址进行工作流程。 @Toolkit
    【解决方案2】:

    指向 Adob​​eSign 存储库的链接:

    ADOBE SIGN SDK C# SHARP API Ver. 6

    Adobe Sign API 集成商 - 这有点隐藏在 Adob​​eSigns GIT 存储库中。指向 GIT 项目中 C# 和 REST 客户端集成 C# 项目的所有生成的 SWAGGER 类(模型/方法)的链接,您可以在项目中作为项目引用或编译的 DLL 直接编译和使用。此项目已更新为使用 API 版本 6。这对我来说是一个巨大的节省时间。我在下面提供了一个关于如何使用它的快速示例。我希望这也有助于其他人节省时间。

    请注意,您可能必须在 configuration.cs 中关闭 BasePath,以便在收到 404 错误时检索初始 Adob​​e URI“BaseURI”调用。

    更改 BasePath = "http://localhost/api/rest/v6";

    收件人:

    BasePath = "https://api.echosign.com/api/rest/v6";

    //include namespaces: 
    using IO.Swagger.Api;
    using IO.Swagger.model.agreements;
    using IO.Swagger.model.baseUris;
    using IO.Swagger.model.transientDocuments;
    using System.IO;
    

    然后这个快速最小演示 BaseUri,上传 PDF 即临时文档,然后创建协议(示例 1 基本签名者最小选项

            string transientDocumentId = "";
            string adobesignDocKey = "";
            string baseURI = "";
            var apiInstanceBase = new BaseUrisApi();
            var authorization = "Bearer " + apiKey;   //Example as Integration Key, see adobesign docs For OAuth.
    
            try
            {
                //___________________GET BASEURI ADOBE SIGN_________________________
    
                BaseUriInfo resultBase = apiInstanceBase.GetBaseUris(authorization);
                baseURI = resultBase.ApiAccessPoint; //return base uri
    
                //___________________UPLOAD YOUR PDF THEN REF ADOBE SIGN_________________________
    
                var apiInstanceFileUpload = new TransientDocumentsApi(baseURI + "api/rest/v6/");
                TransientDocumentResponse resultTransientID = apiInstanceFileUpload.CreateTransientDocument(authorization, File.OpenRead([ENTER YOUR LOCAL FILE PATH]), null, null, _filename, null);
    
                if (!String.IsNullOrEmpty(resultTransientID.TransientDocumentId))
                {
                    transientDocumentId = resultTransientID.TransientDocumentId; //returns the transient doc id to use below as reference                    
                }
    
                var apiInstance = new AgreementsApi(baseURI + "api/rest/v6/");
    
                //___________________CREATE ADOBE SIGN_________________________
    
                var agreementId = "";  // string | The agreement identifier, as returned by the agreement creation API or retrieved from the API to fetch agreements.
    
                var agreementInfo = new AgreementCreationInfo();                
    
                //transientDocument, libraryDocument or a URL (note the full namespace/conflicts with System.IO
                List<IO.Swagger.model.agreements.FileInfo> useFile = new List<IO.Swagger.model.agreements.FileInfo>();
                useFile.Add(new IO.Swagger.model.agreements.FileInfo { TransientDocumentId = transientDocumentId });
                agreementInfo.FileInfos = useFile;
    
                //Add Email To Send To:
                List<ParticipantSetMemberInfo> partSigners = new List<ParticipantSetMemberInfo>();
                partSigners.Add( new ParticipantSetMemberInfo { Email = "[ENTER VALID EMAIL SIGNER]", SecurityOption=null });
    
                //Add Signer To Participant
                List<ParticipantSetInfo> partSetInfo = new List<ParticipantSetInfo>();
                partSetInfo.Add(new ParticipantSetInfo { Name = "signer1", MemberInfos = partSigners, Role = ParticipantSetInfo.RoleEnum.SIGNER, Order=1, Label="" });
                agreementInfo.ParticipantSetsInfo = partSetInfo;
    
                agreementInfo.SignatureType = AgreementCreationInfo.SignatureTypeEnum.ESIGN;
                agreementInfo.Name = "Example Esign For API";
    
                agreementInfo.Message = "Some sample Message To Use Signing";
    
                agreementInfo.State = AgreementCreationInfo.StateEnum.INPROCESS;
    
                AgreementCreationResponse result = apiInstance.CreateAgreement(authorization, agreementInfo, null, null);
                adobesignDocKey = result.Id; //returns the document Id to reference later to get status/info on GET                
            }
            catch (Exception ex) 
            {
                //Capture and write errors to debug or display to user 
                System.Diagnostics.Debug.Write(ex.Message.ToString());
            }
    

    【讨论】:

    • 这仍然对您有用吗?无论我尝试调用哪个端点,我都会得到 404。我可以使用 Postman 验证我的访问令牌是否正常工作,但是这个 C# 项目使用起来非常痛苦。
    • 我使用的方法是将令牌作为“Bearer ”作为字符串传递.. 然后我最终调整了上面提到的 Git 项目中某处的 Uri。我在寻找 Uri 的 GIT 项目中遇到了类似的问题。又名 API 端点的网址。抱歉无法引用代码行,但我确实记得通过运行/调试来查找分配 Uri 的位置来发现问题。在过去的 2 年里,我一直在成功使用上述 GIT 项目,尚未进行任何修改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多