3

好的,我对使用 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
4

2 回答 2

5

您误解了 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; }
}
于 2016-05-11T22:10:30.763 回答
2

链接到 Adob​​eSign 存储库:

ADOBE SIGN SDK C# SHARP API 版本。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 aka Transient Document,然后创建协议(示例 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());
        }
于 2019-12-15T21:48:48.580 回答