RE: CrmServiceClient connection stopped working today
ok so lets take the bazooka :-)
here is some code to connect with clientid/clientsecret
We are using this for some month now without any problems.
https://stackoverflow.com/questions/36160766/how-do-i-connect-a-server-service-to-dynamics-online
this is the class
public class DynamicsCeClientIdClient
{
public string TenantId = ""; // from your app registration overview "Directory (tenant) ID"
public string ClientId = ""; // from your app registration overview "Application (client) ID"
public string ClientSecret = ""; // secret generated in step 1
public string LoginUrl = "https://login.microsoftonline.com"; // aad login url
public string OrganizationName = ""; // check your dynamics login url, e.g. https://..dynamics.com
public string OrganizationRegion = "crm4"; // might be crm for north america, check your dynamics login url
public DynamicsCeClientIdClient(string organizationName, string clientId, string clientSecret, string tenantId, string region = "crm4")
{
TenantId = tenantId;
ClientId = clientId;
ClientSecret = clientSecret;
OrganizationName = organizationName;
OrganizationRegion = region;
}
private string GetServiceUrl()
{
return $"{GetResourceUrl()}/XRMServices/2011/Organization.svc/web";
}
private string GetResourceUrl()
{
return $"https://{OrganizationName}.api.{OrganizationRegion}.dynamics.com";
}
private string GetAuthorityUrl()
{
return $"{LoginUrl}/{TenantId}";
}
public async Task CreateClient(string sdkVersion = "9.1")
{
var context = new AuthenticationContext(GetAuthorityUrl(), false);
context.ExtendedLifeTimeEnabled = true;
var token = await context.AcquireTokenAsync(GetResourceUrl(), new ClientCredential(ClientId, ClientSecret));
return new OrganizationWebProxyClient(new Uri(GetServiceUrl()), true)
{
HeaderToken = token.AccessToken,
SdkClientVersion = "9.1"
};
}
public async Task CreateContext()
{
var client = await CreateClient();
return new OrganizationServiceContext(client);
}
public async Task TestApiCall()
{
var context = await CreateContext();
// send a test request to verify authentication is working
var response = (WhoAmIResponse)context.Execute(new WhoAmIRequest());
}
}
and this is how you create the client:
var organization = "myorg";
var clientId = "myclientid";
var clientSecret = "myclientsecret";
var tenantId = "mytenantid";
var region = "crm4";
var client = new DynamicsCeClientIdClient(organization, clientId, clientSecret, tenantId, region);
var c = client.CreateClient("9.1").Result;