- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试在我的 ASP.NET Core 2.0 网站中使用 eBay API。 eBay 的 .NET SDK 不适用于 .NET Core,因此我通过 WCF 添加了该服务。我是 WCF 的新手,但我无法让这个客户工作。无论我尝试什么,我都会收到此错误:
Exception thrown: 'System.ServiceModel.FaultException' in System.Private.CoreLib.dll
com.ebay.app.pres.service.hosting.WebServiceDisabledException: The web service eBayAPI is not properly configured or not found and is disabled.
GetItem
.我的凭据确实有效,我能够使用 SoapUI 确认这一点。
var rawEbayConfig = Configuration.GetSection("Ebay");
var ebayConfig = rawEbayConfig.Get<EbayConfig>();
// https://api.sandbox.ebay.com/wsapi
var ebayEndpoint = new EndpointAddress(ebayConfig.ApiBaseUrl);
var ebayCreds = new CustomSecurityHeaderType
{
Credentials = new UserIdPasswordType
{
AppId = ebayConfig.AppId,
DevId = ebayConfig.DevId,
AuthCert = ebayConfig.CertId
},
eBayAuthToken = ebayConfig.Token
};
var ebayClient = new eBayAPIInterfaceClient(
eBayAPIInterfaceClient.EndpointConfiguration.eBayAPI, ebayEndpoint);
var reqType = new GetItemRequestType
{
ItemID = "validItemId",
Version = "809"
};
var itemResp = await ebayClient.GetItemAsync(_header, reqType); // error thrown here
// do more stuff
最佳答案
好吧,我设法弄清楚了。让它工作的方法有点笨拙,可能有更好的方法,但我想不出一个。系上。
WCF 文档
在我开始之前,Microsoft 有关于 WCF 的精彩文档 here .如果您像我一样不熟悉 WCF,请查看它们。这是使用客户端,而不是服务器。我们只是在与 eBay 的服务器通信,而不是运行我们自己的服务器。
步骤 0:确保解决方案构建
在下一步之前,请确保您的解决方案已构建。除非成功构建,否则您无法添加连接的服务。
步骤 1:添加连接服务
第一步是将服务引用添加到项目并从 eBay 的服务器生成模型。 eBay 为此提供了 WSDL 文件。我们特别想要 this one .您可以使用 Add Connected Service
Visual Studio 中的工具来执行此操作。您必须选择一个 ASP.NET 项目,然后转到 Tools > Add Connected Service
.使用 Microsoft WCF Web Service Reference Provider
的选项.在 URI
中输入该 .wsdl URL框,点击Go
,将命名空间更改为您想要的任何名称,然后单击Finish
.命名空间纯粹是偏好,我做了我的 EbayService
.所有其他设置都可以保留为默认值。一个盒子会弹出一堆记录的东西,你可以忽略它并等到它完成。会有大量的黄色警告,这些也可以忽略。
(注意:您也可以使用 svcutility
来生成模型,而不是 Visual Studio 的 GUI,但我不会对此进行介绍。)
步骤 2:编辑服务引用文件
Web 项目现在应该在顶部附近的解决方案资源管理器中有一个名为 Connected Services
的部分。 .里面应该有一个文件夹,命名为您之前提供的任何命名空间。里面会有一个名为 Reference.cs
的文件。包含 eBay 服务的所有解析信息。这个文件很大,我的有将近 122k 行。我们需要在这里做几件事。
我们需要做几个查找/替换操作。为此,我们必须查找/替换两个字符串。 在替换窗口中打开正则表达式,然后替换 , Order=\d{1,3}
一无所有。这可以捕获 Order
的情况。是属性上的多个参数之一。然后做同样的事情,但替换 Order=\d{1,3}
.这样做会破坏我遇到的其中一个类(class),所以找到类(class) ReviseInventoryStatusRequestType
.它有一个名为 any1Field
的字段。和一个名为 Any1
的属性.删除这两个。
请记住,如果您从服务器重新生成服务,则需要重做这些步骤,因为该过程会覆盖 Reference.cs
.
(为什么这样做?eBay WSDL 文件指定了元素从 API 返回时的顺序,但它们甚至不接近正确。因此,几乎所有响应都不会真正正确解析。我们需要删除订单规范,让程序通过名称来处理解析。)
第 3 步:编写客户端包装器
可以直接使用此服务,但这样做会变得非常困惑。因此,我围绕客户端编写了一个包装器,大大简化了这一点。注释代码如下,DI 将在下一步出现,所以不用担心。您需要为您将使用的每个 API 端点添加一个函数。
public class EbayClient
{
// Don't do this, use a proper method of storing app secrets.
// I have it this way for simplicity in this example.
const string AppId = "MyAppId";
const string DevId = "MyDevId";
const string CertId = "MyCertId";
const string AuthToken = "MyAuthToken";
// This is the version of the API that your WSDL file is from. As of this answer
// the latest version is 1039. All calls need this passed as a parameter.
const string Version = "1039";
// This is the base URL for the API.
// Sandbox: https://api.sandbox.ebay.com/wsapi
// Production: https://api.ebay.com/wsapi
const string BaseApiUrl = "https://api.sandbox.ebay.com/wsapi";
// This is the actual client from the service we just imported. It's being injected
// here via the built-in DI in ASP.NET Core.
readonly eBayAPIInterfaceClient _ebay;
// All of the functions in this class need these credentials passed, so declare it in
// the constructor to make things easier.
readonly CustomSecurityHeaderType _creds;
public EbayClient(eBayAPIInterfaceClient ebay)
{
_ebay = ebay;
_creds = new CustomSecurityHeaderType
{
Credentials = new UserIdPasswordType
{
AppId = AppId,
DevId = DevId,
AuthCert = CertId
},
eBayAuthToken = AuthToken
};
}
// This is a wrapper around the API GetItem call.
public async Task<GetItemResponse> GetItemAsync(string itemId)
{
// All of the API requests and responses use their own type of object.
// This one, naturally, uses GetItemRequest and GetItemResponse.
var reqType = new GetItemRequest
{
GetItemRequest1 = new GetItemRequestType
{
ItemID = itemId,
Version = Version
},
RequesterCredentials = _creds
};
// The service isn't smart enough to know the endpoint URLs itself, so
// we have to set it explicitly.
var addr = new EndpointAddress($"{ApiBaseUrl}?callname=GetItem");
// This creates a channel from the built-in client's ChannelFactory.
// See the WCF docs for explanation of this step.
var ch = _ebay.ChannelFactory.CreateChannel(addr);
// Actually call the API now
var itemResp = await ch.GetItemAsync(reqType);
// Check that the call was a success
if (itemResp.GetItemResponse1.Ack == AckCodeType.Success)
{
// The call succeeded, so handle the data however you want. I created
// a class to simplify the API class because the API class is massive.
return new EbayItem
{
ItemId = itemResp.GetItemResponse1.Item.ItemID,
Price = Convert.ToDecimal(itemResp.GetItemResponse1.Item.BuyItNowPrice.Value),
QuantityAvailable = itemResp.GetItemResponse1.Item.QuantityAvailable
};
}
// Handle an API error however you want. Throw an
// exception or return null, whatever works for you.
return null;
}
}
Startup.cs
/
ConfigureServices()
:
// This is the base client
services.AddSingleton(new eBayAPIInterfaceClient(eBayAPIInterfaceClient.EndpointConfiguration.eBayAPI));
// This is our wrapper
services.AddScoped<EbayClient>();
public class ProductsIdExternalController : Controller
{
// This is the wrapper class
readonly EbayClient _ebay;
public ProductsIdExternalController(EbayClient ebay)
{
_ebay = ebay;
}
[HttpGet]
public async Task<IActionResult> Index(string itemId)
{
var item = await _ebay.GetItemAsync(itemId);
// Use the item however you want.
// Make sure to handle errors in case the item ID doesn't exist.
}
}
关于c# - 使用 WCF 查询 eBay API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48023631/
我在网上搜索但没有找到任何合适的文章解释如何使用 javascript 使用 WCF 服务,尤其是 WebScriptEndpoint。 任何人都可以对此给出任何指导吗? 谢谢 最佳答案 这是一篇关于
我正在编写一个将运行 Linux 命令的 C 程序,例如: cat/etc/passwd | grep 列表 |剪切-c 1-5 我没有任何结果 *这里 parent 等待第一个 child (chi
所以我正在尝试处理文件上传,然后将该文件作为二进制文件存储到数据库中。在我存储它之后,我尝试在给定的 URL 上提供文件。我似乎找不到适合这里的方法。我需要使用数据库,因为我使用 Google 应用引
我正在尝试制作一个宏,将下面的公式添加到单元格中,然后将其拖到整个列中并在 H 列中复制相同的公式 我想在 F 和 H 列中输入公式的数据 Range("F1").formula = "=IF(ISE
问题类似于this one ,但我想使用 OperatorPrecedenceParser 解析带有函数应用程序的表达式在 FParsec . 这是我的 AST: type Expression =
我想通过使用 sequelize 和 node.js 将这个查询更改为代码取决于在哪里 select COUNT(gender) as genderCount from customers where
我正在使用GNU bash,版本5.0.3(1)-发行版(x86_64-pc-linux-gnu),我想知道为什么简单的赋值语句会出现语法错误: #/bin/bash var1=/tmp
这里,为什么我的代码在 IE 中不起作用。我的代码适用于所有浏览器。没有问题。但是当我在 IE 上运行我的项目时,它发现错误。 而且我的 jquery 类和 insertadjacentHTMl 也不
我正在尝试更改标签的innerHTML。我无权访问该表单,因此无法编辑 HTML。标签具有的唯一标识符是“for”属性。 这是输入和标签的结构:
我有一个页面,我可以在其中返回用户帖子,可以使用一些 jquery 代码对这些帖子进行即时评论,在发布新评论后,我在帖子下插入新评论以及删除 按钮。问题是 Delete 按钮在新插入的元素上不起作用,
我有一个大约有 20 列的“管道分隔”文件。我只想使用 sha1sum 散列第一列,它是一个数字,如帐号,并按原样返回其余列。 使用 awk 或 sed 执行此操作的最佳方法是什么? Accounti
我需要将以下内容插入到我的表中...我的用户表有五列 id、用户名、密码、名称、条目。 (我还没有提交任何东西到条目中,我稍后会使用 php 来做)但由于某种原因我不断收到这个错误:#1054 - U
所以我试图有一个输入字段,我可以在其中输入任何字符,但然后将输入的值小写,删除任何非字母数字字符,留下“。”而不是空格。 例如,如果我输入: 地球的 70% 是水,-!*#$^^ & 30% 土地 输
我正在尝试做一些我认为非常简单的事情,但出于某种原因我没有得到想要的结果?我是 javascript 的新手,但对 java 有经验,所以我相信我没有使用某种正确的规则。 这是一个获取输入值、检查选择
我想使用 angularjs 从 mysql 数据库加载数据。 这就是应用程序的工作原理;用户登录,他们的用户名存储在 cookie 中。该用户名显示在主页上 我想获取这个值并通过 angularjs
我正在使用 autoLayout,我想在 UITableViewCell 上放置一个 UIlabel,它应该始终位于单元格的右侧和右侧的中心。 这就是我想要实现的目标 所以在这里你可以看到我正在谈论的
我需要与 MySql 等效的 elasticsearch 查询。我的 sql 查询: SELECT DISTINCT t.product_id AS id FROM tbl_sup_price t
我正在实现代码以使用 JSON。 func setup() { if let flickrURL = NSURL(string: "https://api.flickr.com/
我尝试使用for循环声明变量,然后测试cols和rols是否相同。如果是,它将运行递归函数。但是,我在 javascript 中执行 do 时遇到问题。有人可以帮忙吗? 现在,在比较 col.1 和
我举了一个我正在处理的问题的简短示例。 HTML代码: 1 2 3 CSS 代码: .BB a:hover{ color: #000; } .BB > li:after {
我是一名优秀的程序员,十分优秀!