4

Code written for calling message statistics-

 SentEmailPayload payload = new SentEmailPayload()
 {
     ManagerRootId = "{xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx}",
     PageIndex = 0,
     PageSize = 100,
     Search = "",
     Sorting = "dendDate",
     Type = "{xxxxxx-xxxx-xxxx-xxxx-xxxxxx}"
 };
 result = Sitecore.Context.User.Name;
 var url = "https://host_name/sitecore/api/ssc/exm/MessageStatistics";

 var request = (HttpWebRequest)WebRequest.Create(url);
 request.Method = "POST";
 request.ContentType = "application/json";
 request.CookieContainer = cookies; // cookie after login api being called
 var requestBody = JsonConvert.SerializeObject(payload);
 var data = new UTF8Encoding().GetBytes(requestBody);

 using (var dataStream = request.GetRequestStream())
 {
     dataStream.Write(data, 0, data.Length);
 }

 var response = (HttpWebResponse)request.GetResponse();

For this above code, I keep getting 401 unauthorized in the last line. Can anyone help me with this?

Thanks, Sai

1 Answer 1

1

The Exm api requires authentication to post the request. In order to authenticate the request, you will need to generate the bearer token using below request.

POST URL: https://{{Sitecore_IdentityServerURL}}/connect/token
Content-Type: application/x-www-form-urlencoded

Body:
grant_type: client_credentials
client_id: {{ClientID}}
client_secret: {{ClientSecret}}

Above request will return you the bearer token. This bearer token then can be used to authenticate the EXM api as below.

https://host_name/sitecore/api/ssc/exm/MessageStatistics
Authorization: Bearer {{BearerToken}}

Using above way, the authorization issue will be solved.

Note: The client id and client secret has to configured using non-interactive client login on the Sitecore Identity server. You can follow the Sitecore documentation link to configure the same.

Hope this helps!!! Let me know in case you have any queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.