调用 crm4 web 服务时出现错误 401

发布于 2024-07-25 17:11:53 字数 840 浏览 8 评论 0原文

我的这段代码可以在单元测试中工作,但在插件上下文中执行时不起作用。 该代码的作用是尝试通过调用 crm4 Web 服务来创建潜在客户。

当插件执行时,我收到以下异常:“HTTP 状态 401:未经授权”

这是初始化 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"));

有人对我下一步可以尝试什么有建议吗? 测试运行时会创建线索,单元测试中的配置信息与应用程序执行插件时的配置信息相同。

I have this code that works in a unit test but doesn't work when executed in the context of a plugin. What the code does is try to create a lead by calling the crm4 webservice.

When the plugin executes I get the following exception: "HTTP status 401: Unauthorized"

This is the code that initialises an instance of the webservice

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"));

Anyone have advice on what I can try next? The lead is created when the test runs, and the configuration information is the same in the unit test as it is when the app is executing the plugin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2024-08-01 17:11:54

您不必自己实例化 CrmService,也可以通过获取 IPluginExecutionContext 的引用并调用 CreateCrmService 方法来获取 CrmService。

请参阅此 关于从 IPluginExecutionContext 创建 CrmService 的链接

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"));
}

问候,

hadi

Rather than instantiating the CrmService by yourself, alternatively you can obtain the CrmService by obtaining the reference of the IPluginExecutionContext and invoke the CreateCrmService method

Please refer to this link regarding creating the CrmService from 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"));
}

Regards,

hadi

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文