Erro 401 Chamando CRM4 Web Service
-
21-08-2019 - |
Pergunta
Eu tenho esse código que funciona em um teste de unidade, mas não funciona quando executado no contexto de um plug -in. O que o código faz é tentar criar um lead chamando o CRM4 WebService.
Quando o plug -in é executado, recebo a seguinte exceção: "Status HTTP 401: não autorizado"
Este é o código que inicializa uma instância do serviço da web
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = GetConfig("crm.organisation_name");
_crmService = new CrmService(GetConfig("webservice.crm"));
_crmService.CrmAuthenticationTokenValue = token;
_crmService.UseDefaultCredentials = false;
_crmService.PreAuthenticate = false;
_crmService.Credentials = new NetworkCredential(GetConfig("crm.user_username"),
GetConfig("crm.user_password"),
GetConfig("crm.user_domain"));
Alguém tem conselhos sobre o que posso tentar a seguir? O lead é criado quando o teste é executado e as informações de configuração são as mesmas no teste de unidade, como é quando o aplicativo está executando o plug -in.
Solução
Em vez de instanciar o CrmService sozinho, alternativamente, você pode obter o CrmService obtendo a referência do iPluginexecutionContext e invocar o método CreateCrmService
Por favor, consulte isso link Em relação à criação do CrmService a partir de iPluginexecutionContext
Here is some code snippet
public void Execute(IPluginExecutionContext context)
{
// the below code means, the CrmService will be created
// by referring to the user account who is registered to
// run the CRM Application Pool
ICrmService crmService = context.CreateCrmService(false);
// the below code means, the CrmService will be created
// by taking account the user account who login and run
// the current plugin
ICrmService crmService = context.CreateCrmService(true);
// the below code means, the CrmService will be created
// by impersonating a valid user
ICrmService crmService = context.CreateCrmService(new Guid("3F2504E0-4F89-11D3-9A0C-0305E82C3301"));
}
Cumprimentos,
hadi