我正在尝试从Infogroup API中提取数据,我能够通过GET查询提取数据但无法提取需要POST方法的数据。在Infogroup API控制台[http://developer.infoconnect.com/api-console][1]我给出的参数为apikey = 4iRztNp5euNzjK3i69xlnpIMa3K6AZCv,在正文中我给出了以下代码:
{"Fields":
["Id",
"Companyname",
"Assets",
"BankAsset",
"BigBusinessSegmentation",
"BusinessStatus",
"CallStatus",
"CompanyDescription",
"CompDate",
"CreditCardsAccepted",
"CreditLimit",
"FortuneRanking",
"HoldingStatus",
"InWealthyArea",
"IsActiveWebAddress",
"IsHomeBusiness",
"IsFranchise",
"Population",
"ProductsSold",
"StockExchange",
"SquareFootage",
"Subsidiary",
"TeleResearchUpdateDate",
"YearEstablished",
"RecordStatus",
"Location"],
"CompanyName":"DORAN MECHANICAL"
}
它在控制台中工作正常,但我想在Excel中提取这些数据。请帮忙!
Function Post(CompName As String) As String
Dim companyname As String
Dim strQuery As String
Dim googleService As New MSXML2.XMLHTTP
Dim rspText As String
companyname = URLEncode(CompName)
strQuery = "https://api.infoconnect.com/v1/companies/search?apikey=4iRztNp5euNzjK3i69xlnpIMa3K6AZCv"
googleService.Open "POST", strQuery, False
googleService.setRequestHeader "Content-Type", "application/json"
googleService.setRequestBody "Fields": ["Id","Companyname","Assets","BigBusinessSegmentation","CreditRatingScore", "Location"] , "companyname": CompName
rspText = googleService.responseText
Creditrating = "NA"
If InStr(1, rspText, """CreditRatingScore"":""") Then
Creditrating = Split(Split(rspText, """CreditRatingScore"":""")(1), Chr(34))(0)
End If
End Function
答案 0 :(得分:2)
看起来你的代码中的setRequestBody是无效的json,这个代码会导致你的错误。对于显示字段,外壳很重要(例如,您的代码使用Companyname,它应该是CompanyName),并且在我的测试中,它并不总是返回所请求字段的值(例如,没有返回资产的& #34; Doran Mechanical")。
有效正文(注意:{
和}
包围JSON对象,键和值字符串引用""
):
"{""CompanyName"":""" & CompanyName & """,""Fields"":[""Id"",""CompanyName"",...]}"
以下是此请求使用VBA-Web的示例:
Function CreditRating(CompanyName As String) As Dictionary
Dim Client As New WebClient
Client.BaseUrl = "https://api.infoconnect.com/v1"
Dim Request As New WebRequest
Request.Resource = "companies/search"
Request.Method = WebMethod.HttpPost
Request.Format = WebFormat.Json
Request.AddQuerystringParam "apikey", "4iRz..."
Request.AddBodyParameter "CompanyName", CompanyName
Request.AddBodyParameter "Fields", Array("Id", "CompanyName", "CreditRatingScore", "Assets", "BankAsset", "BigBusinessSegmentation", "BusinessStatus")
Dim Response As WebResponse
Set Response = Client.Execute(Request)
' Example Response:
' [{"ETag":"...","Id":"...","Links":[...],"CompanyName":"Doran Mechanical",...,"CreditRatingScore":"B"}]
' (JSON Array of Objects -> Collection, 1-based of Dictionary -> Return first Dictionary
CreditRating = Response.Data(1)("CreditRatingScore")
End Function