【问题标题】:R convert python GET to R http GETR将python GET转换为R http GET
【发布时间】: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

标签: python r get httr


【解决方案1】:

在上面的 cmets 的帮助下,我想出了如何完成这项工作。 httr GET的python版本如下所示

response = requests.get(url,params=parameters, headers=headers)

对于我的问题,我有以下来自 python 程序的内容

parameters = {'databaseId': 'WOK', 'usrQuery': query, 'count': count, 'firstRecord': firstRecord}
headers={'accept':'application/json','X-ApiKey':'********'}

与 httr GET 等价的是

response <- httr::GET(url, httr::add_headers(accept = 'application/json', `X-APIKey` = wosliteKey), query = list(databaseId = 'WOK', usrQuery = query, count = count, firstRecord = firstRecord))

python 头信息被 httr 中的 add_headers 函数替换。 python 参数信息作为列表添加到查询选项中。

【讨论】:

    【解决方案2】:

    我搜索了一段时间,我发现一些东西看起来可以解决你的问题。

    android package - R.attr nested class -- employing an api key

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多