Hi All
I am working on Dynamics 365 Online..I'm facing a Permission error when i try to call an external Azure WebService from my plugin.
The code I wrote for that works fine in a console application, but it fails in the plugin. The code looks like the following:
error: 400 (Bad Request)
string keyName = "MyKey";
string key = "V2333344YUzgyXP83cIBgfYqFo=";
var url = "myazure.servicebus.windows.net/.../CBD06A18-0CF9-E811-A97B-000D3AA04914";
var token = GetToken(url, keyName, key);
var response = GetResultFromService(url, token);
private HttpResponseMessage GetResultFromService(string url, string token)
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token);
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.Timeout = new TimeSpan(0, 10, 30);
var response = httpClient.PutAsync(url,null).Result;
var responseText = response.Content.ReadAsStringAsync().Result;
return response;
}
public string GetToken(string resourceUri, string keyName, string key)
{
var expiry = GetExpiry();
string stringToSign = WebUtility.UrlEncode(resourceUri) + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
WebUtility.UrlEncode(resourceUri), WebUtility.UrlEncode(signature), expiry, keyName);
return sasToken;
}
Any help?
Thanks
BV
Thanks for your help Johnny. Same code works in the CRM 2015 (on premises version) and console application.
I also tried simplest way like this one:
using (WebClient wc = new WebClient())
{
wc.Headers.Add("Authorization: Bearer ey8927eg534gnm89u24xcm4856454c5t....."); // If you need auth
string output = wc.DownloadString("api.examplewebservice.com/customers");
}
I hope anyone can help me out with this.
Hi Biju,
400 is a common HTTP response codes which means server was unable to process the request sent by the client due to invalid syntax
I would suggest you to refer to the general rules for trying to access an external webservice from a plug-in from the following post:
community.dynamics.com/.../253009
Or you could raise service request to get the corresponded D365 Online trace logs or add the ITracingService in your program for debugging.
docs.microsoft.com/.../gg509030(v=crm.8)
www.cloudfronts.com/call-web-service-plugin-dynamics-crm
Regards
Johnny
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156