Reputation: 8636
I managed to get access token with my code,
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;
}
This gives me the access token, Now I am trying to use that token and add a user to the account,
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 = "[email protected]";
user.UserName = "[email protected]";
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;
}
Here is the Docusign User class I use to serialize,
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; }
}
User api is not supported in the .Net SDK. Therefor I had to write this code by referring to Docusign Api test playground and checking the Request/Response with Fiddler.
Appreciate any help on this.
EDIT
Here is the POST request
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":"[email protected]","userName":"[email protected]","firstName":"Sam1","lastName":"Cooper1"}
Upvotes: 1
Views: 260
Reputation: 8636
Found the answer,
Issue was with the Json message. Required Json format is in the following format as per Api Guide
{
"newUsers":[
{
"email":"[email protected]",
"firstName":"name1",
"lastName":"lastname1",
"password":"Password01",
"userName":"[email protected]"
}
]
}
Upvotes: 1