Docusign 400 BadRequest for AddUser帐户REST API

时间:2016-03-23 06:42:18

标签: c# docusignapi

我设法使用我的代码

获取访问令牌
  private async Task<string> GetAccessToken()
    {
        string refreshToken = string.Empty;
        string accessToken = string.Empty;

        HttpClient client = new HttpClient();

        client.BaseAddress = new Uri("https://demo.docusign.net/restapi/v2/oauth2/token");

        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));

        string body =
            "username=user%40company.net&password=mypassword&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxc&grant_type=password&scope=api";               

        HttpContent content = new System.Net.Http.StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");

        HttpResponseMessage messge = await client.PostAsync("https://demo.docusign.net/restapi/v2/oauth2/token", content);
        //string description = string.Empty;
        if (messge.IsSuccessStatusCode)
        {
            string result = messge.Content.ReadAsStringAsync().Result;

            dynamic returnObj = JsonConvert.DeserializeObject(result);

            var scope = returnObj.scope.Value;
            accessToken = returnObj.access_token.Value;

        }

        return accessToken;
    }

这为我提供了访问令牌, 现在我正在尝试使用该令牌并将用户添加到帐户

    private async Task<string> AddUser(string accessToken, string usersBaseUri)
    {
        usersBaseUri = "https://demo.docusign.net/restapi/v2/accounts/<myaccountId>/users";
        string resultStr = string.Empty;

        HttpClient client = new HttpClient();

        client.BaseAddress = new Uri(usersBaseUri);
        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);           


        DocuSignUser user = new DocuSignUser();
        user.Email = "user2@company.net";
        user.UserName = "user2@company.net";
        user.FirstName = "user2";
        user.LastName = "dev";

        var json = JsonConvert.SerializeObject(user);
        HttpContent content = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");

        HttpResponseMessage messge = await client.PostAsync(usersBaseUri, content);
        //string description = string.Empty;
        if (messge.IsSuccessStatusCode)
        {
            string result = messge.Content.ReadAsStringAsync().Result;

            dynamic returnObj = JsonConvert.DeserializeObject(result);

            var scope = returnObj.scope.Value;
            accessToken = returnObj.access_token.Value;
        }

        return resultStr;
    }

这是我用来序列化的Docusign User类,

public class DocuSignUser
{
    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("userName")]
    public string UserName { get; set; }

    [JsonProperty("firstName")]
    public string FirstName { get; set; }

    [JsonProperty("lastName")]
    public string LastName { get; set; }

}

.Net SDK不支持用户API。因此,我必须通过引用Docusign Api test playground并使用Fiddler检查请求/响应来编写此代码。

感谢您的任何帮助。

修改

这是POST请求

POST https://demo.docusign.net/restapi/v2/accounts/156xxxx/users HTTP/1.1
Accept: application/json
Authorization: Bearer *******<AccessToken>
Content-Type: application/json; charset=utf-8
Host: demo.docusign.net
Content-Length: 100
Expect: 100-continue
    {"email":"user1@company.net","userName":"user1@company.net","firstName":"Sam1","lastName":"Cooper1"}

1 个答案:

答案 0 :(得分:1)

找到答案,

问题出在Json消息上。 所需的Json格式按照Api Guide

的格式采用以下格式
{
  "newUsers":[
     {         
        "email":"user@company.net",         
        "firstName":"name1",         
        "lastName":"lastname1",
        "password":"Password01",         
        "userName":"user1@bcompany.net"         
     }
   ]
}