Ebay API GetSuggestedCategories呼叫返回没有类别

时间:2017-09-02 18:57:24

标签: vb.net ebay ebay-api

我似乎遇到了一些API调用问题,包括GetSuggestedCategories和UploadSiteHostedPictures调用。我将重点关注此处的GetSuggestedCategories调用,因为两者都返回成功,但预期值为空值。

如果有人能查看我的代码并指出我正确的方向来确定导致返回为null的话,我会很高兴。我花了几天的时间试图确定这个没有运气。

对于GetSuggestedCategories调用,我的代码如下所示:

Public Function GetSuggestedCategories(keywords As String) As List(Of String)
    '<STAThread>
    Dim endpoint As String = "https://api.ebay.com/wsapi"
    Dim callName As String = "GetSuggestedCategories"
    Dim siteId As String = SiteCodeType.US
    Dim appId As String = "My App ID"
    Dim devId As String = "My Dev ID"
    Dim certId As String = "My Cert ID"
    Dim version As String = "1031"

    'build the request URL
    Dim requestURL As String = endpoint +
        "?callname=" + callName +
        "&siteid=" + siteId +
        "&appid=" + appId +
        "&version=" + version +
        "&routing=default"

    'Create the service
    Dim service As eBayAPIInterfaceClient = New eBayAPIInterfaceClient("eBayAPI", requestURL)
    Dim requesterCredentials As CustomSecurityHeaderType = New CustomSecurityHeaderType
    requesterCredentials.eBayAuthToken = "My User Token"
    requesterCredentials.Credentials = New UserIdPasswordType
    requesterCredentials.Credentials.AppId = appId
    requesterCredentials.Credentials.DevId = devId
    requesterCredentials.Credentials.AuthCert = certId

    'Make the call to GetSuggestedCategories
    Dim request As eBayWSDL.GetSuggestedCategoriesRequestType = New GetSuggestedCategoriesRequestType
    request.Version = version
    Dim ipHost As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())        
    For Each ipaddress As IPAddress In ipHost.AddressList
        Debug.Print(ipaddress.ToString)
        If ipaddress.AddressFamily = Sockets.AddressFamily.InterNetwork Then
            request.EndUserIP = ipHost.AddressList(2).ToString
            Exit For
        End If
    Next
    request.Query = keywords
    Dim detaillevels As New List(Of DetailLevelCodeType)
    detaillevels.Add(DetailLevelCodeType.ReturnAll)
    request.DetailLevel = detaillevels.ToArray

    Dim returnCategories As New List(Of String)
    Dim response As GetSuggestedCategoriesResponseType
    Try
        response = service.GetSuggestedCategories(requesterCredentials, request)
        If response.Ack <> AckCodeType.Failure Then
            returnCategories.Add("0: Complete with no errors.")
            returnCategories.Add(response.SuggestedCategoryArray.SuggestedCategory.FirstOrDefault.Category.CategoryID)
        Else
            returnCategories.Add("1: Errors returned.")
            Dim err As ErrorType
            For Each err In response.Errors
                returnCategories.Add(response.Errors(0).ErrorCode + ": " + response.Errors(0).ShortMessage)
            Next err

        End If
    Catch ex As Exception
    Finally
        service.Close()
        service = Nothing
        requesterCredentials = Nothing
        request = Nothing
        response = Nothing
    End Try
    Return returnCategories

当我运行此代码时,响应返回的ACK代码为&#34;成功&#34;但价值都是空的。下面是一个显示具有空值的对象的图像。

response Watch List

根据eBay的文档,我希望response.SuggestedCategoryArray.SuggestedCategory可以填写一个或多个类别,我可以提取该类别的名称,ID等。

我试图运行查询,包括&#34;家具&#34;,&#34;红色椅子&#34;,&#34;绘画&#34;,&#34;艺术&#34;和&# 34;○&#39;基夫&#34;因为文档中特别推荐了其中一些作为示例,以便在提出广泛或特定请求时查看回报的差异。一切都以空response.SuggestedCategoryArray返回。

1 个答案:

答案 0 :(得分:0)

经过一天的研究,试验和无数错误后,我似乎遇到了解决这个问题的方法。

关键是响应对象有许多属性,但它们都没有以任何方式填充。可能是因为这些在早期的对象实现中使用但不再使用。你可以看到除了“Any”属性之外什么都没有填充,这是关键。

“Any”属性接收一个XmlElement对象,该对象包含XmlFormat中的完整响应。我所做的只是将Xml解析为XDocument,然后使用LINQ查找和读取我正在寻找的元素。在Getsuggestedcategories的情况下,我找到了CategoryID元素并将值输出到列表中。

以下是我的代码更改的一小部分,从响应对象的实例化开始。

Dim response As GetSuggestedCategoriesResponseType = asyncResponse.GetSuggestedCategoriesResponse1
        If response.Ack <> AckCodeType.Failure Then
            If Not response.SuggestedCategoryArray Is Nothing Then
                For Each cID In response.SuggestedCategoryArray.SuggestedCategory
                    returnCategories.Add(cID.Category.CategoryID)
                Next
            ElseIf Not response.Any Is Nothing Then
                returnCategories.Add("0: Complete with no errors.")
                For Each xmlResponse In response.Any
                    Dim xmlConverter As New XmlDocumentConverter(xmlResponse)
                    returnCategories.AddRange(xmlConverter.ConvertToStringList("CategoryID"))
                Next
            Else
                returnCategories.Add("2: No Suggested Categories Were Found.")
            End If
        Else
            returnCategories.Add("1: Errors returned.")
            Dim err As ErrorType
            For Each err In response.Errors
                returnCategories.Add(response.Errors(0).ErrorCode + ": " + response.Errors(0).ShortMessage)
            Next err

        End If
    Catch ex As Exception
    Finally
        service.Close()
        service = Nothing
        requesterCredentials = Nothing
        request = Nothing
        asyncResponse = Nothing
    End Try
    Return returnCategories

这是我为XmlConverter创建的代码,它读取xmlElement并返回字符串值列表。

Imports System.Globalization
Imports System.IO
Imports System.Xml

Namespace Converters.XML
Public Class XmlDocumentConverter

    Private xmlDoc As XDocument

    Public Sub New(xmlDocumentToRead As XmlDocument)
        xmlDoc = XDocument.Parse(xmlDocumentToRead.OuterXml)
    End Sub

    Public Sub New(xmlElementToRead As XmlElement)
        xmlDoc = XDocument.Parse(xmlElementToRead.OuterXml)
    End Sub

    ''' <summary>
    ''' This will take the nodeToFind representing the xDocument.Name.LocalName and will return the contained value within all found elements.
    ''' </summary>
    ''' <param name="nodeToFind">The string matching the xDocument.Name.LocalName that you would like to find.</param>
    ''' <param name="returnAllResults">If True, returns all results. If False, returns the first result</param>
    ''' <returns></returns>
    Public Function ConvertToStringList(nodeToFind As String, Optional returnAllResults As Boolean = True) As List(Of String)
        Dim returnStringList = New List(Of String)

        Dim xElementList As New List(Of XElement)
        xElementList.AddRange(xmlDoc.Root.Descendants.Where(Function(x) x.Name.LocalName = nodeToFind))
        For Each e In xElementList
            returnStringList.Add(e.Value)
            If returnAllResults = False Then Exit For
        Next

        Return returnStringList
    End Function

End Class

结束命名空间

仍然欢迎反馈和建议,因为这是我能提出的最佳答案,但欢迎任何有更好的想法或eBay API实施的人。