【发布时间】:2019-05-09 23:13:11
【问题描述】:
我正在尝试将使用 api 密钥从网站检索信息的工作 python 程序转换为其 R 等效项。因为我对 httr 或 python 了解不多,所以这是一个挑战。 python代码是(有点缩写,并带有一个虚拟的X-ApiKey)
url = 'https://api.clarivate.com/api/woslite'
query = 'ts=((land AND ocean AND climate AND change)) AND PY=2013-2019'
count = 100
firstRecord = 1
parameters = {'databaseId': 'WOK', 'usrQuery': query, 'count': count, 'firstRecord': firstRecord}
headers={'accept':'application/json','X-ApiKey':'********'}
response = requests.get(url,params=parameters, headers=headers)
我对 R 版本的尝试是
library(httr)
wosliteKey <- Sys.getenv("wosliteKey")
firstRecord <- 1
count <- 100
url <- 'https://api.clarivate.com/api/woslite'
query <- 'ts=(land AND ocean AND climate AND change) AND PY=2013-2019'
r <- GET(url, query = list(api_key = wosliteKey, usrQuery = query, databaseId = 'WOK', count = count, firstRecord = firstRecord))
运行以上返回
Response [https://api.clarivate.com/api/woslite]
Date: 2019-05-09 22:50
Status: 401
Content-Type: application/json; charset=utf-8
Size: 41 B
状态 401 表示未经授权的访问。 python 代码使用X-ApiKey 而不是api_key。但我想不通。有什么区别和b。如何将其放入查询列表中。
【问题讨论】:
-
除了key之外是否需要登录才能访问api?通常这意味着你的身份验证不正确,需要传入变量进行身份验证,通常是用户名/密码。
-
不。无需用户名或密码。我没有编写 python 代码,但它带有这条评论 -
#header includes authentication X-ApiKey specific to this API and user