假设用户提供了卡和电话并拥有有效的 Azure 帐户。创建了免费套餐服务。 (具有 key 和端点,例如 xyz.cognitiveservices.azure.com/bing/v7.0
使用免费套餐(每秒 3 名搜索者,每月最多 3 名搜索者)(请参阅此处 https://azure.microsoft.com/en-us/pricing/details/cognitive-services/ )
它是 GET 还是 POST 调用?正确的 header 参数是什么? 他们只有 Python 示例,但无法正常工作。 https://learn.microsoft.com/en-us/azure/cognitive-services/bing-web-search/quickstarts/python
问题是如何在 R 中做到这一点。
此代码不起作用
library(httr)
token='xxxxx'
server='https://xxxxx.cognitiveservices.azure.com/bing/v7.0/'
url=paste0(server,'search')
response = GET(url = url,
authenticate('',token, type = 'basic'))
response
res = content(response, encoding = 'json')
最佳答案
对于 /search
端点,需要带有非空搜索参数 (q
) 的 GET
请求。
基本身份验证
。相反,如 Python 示例所示,需要包含您的订阅 key 的 HTTP header Ocp-Apim-Subscription-Key
。
所以,我成功地使用了以下代码。它应该也适合你。
library(httr)
server = "https://xxxxx.cognitiveservices.azure.com/bing/v7.0/"
token = "subscription key for Bing Search APIs v7"
search_term = "search term"
url = paste0(server, "search")
response = GET(url = url,
query = list(q = search_term),
add_headers(`Ocp-Apim-Subscription-Key` = token)
)
res = content(response, encoding = "json")
res
参见Web Search API v7 reference有关 header 和查询参数的更多信息。
关于R:如何使用 R 使用 Bing 免费套餐网络搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59220614/