拒绝问题通过 API 查询 NIH

Rejection Issue Querying NIH via API

这是我第一次连接 API。我正在尝试查询 NIH 项目数据,当我获得状态代码 200 时似乎可以正确连接到它。我遇到的问题是当我尝试打印输出时。尽管有访问权限,我还是收到了 URL 的拒绝。文档表明,如果出现任何问题,可能是由于各种原因导致我的 IP 被阻止。我联系了那里的 API 支持团队,我对被阻止的 IP 没有任何问题。他们通过命令提示符向我 运行 提出了一些 curl 请求,我能够正确执行这些请求。这让我相信我有代码问题。我在这里做错了什么?

NIH API Info

import requests


params = {
     "criteria":
     {
       "fiscal_years":[2019,2018]
     },
     "include_fields": [
        "ApplId","SubprojectId","FiscalYear","Organization", "ProjectNum","OrgCountry",
        "ProjectNumSplit","ContactPiName","AllText","FullStudySection",
        "ProjectStartDate","ProjectEndDate"
     ],
     "offset":0,
     "limit":25,
     "sort_field":"project_start_date",
     "sort_order":"desc"
 }
response = requests.post("https://api.reporter.nih.gov/v2/projects/Search", data = params)

#print(response.status_code)

print(response.text)

示例 curl 脚本和输出:

curl -X POST "https://api.reporter.nih.gov/v2/projects/search" -H "accept: application/json" -H "Content-Type: application/json" -d "{​​​​​​​\"criteria\":{​​​​​​​\"covid_response\":[\"Reg-CV\",\"CV\"]}​​​​​​​,\"include_fields\":[\"ApplId\",\"SubprojectId\",\"FiscalYear\",\"Organization\",\"ProjectNum\",\"OrgCountry\",\"ProjectNumSplit\",\"ContactPiName\",\"AllText\",\"FullStudySection\",\"ProjectStartDate\",\"ProjectEndDate\"],\"offset\":0,\"limit\":10}​​​​​​​"

解决方法简单,但容易漏掉。你需要 json=params 看这个问题:How to POST JSON data with Python Requests?

response = requests.post("https://api.reporter.nih.gov/v2/projects/search", json=params)

下面是完整的代码,稍有改动:

import requests

params = {
  "criteria":
    {
      "fiscal_years":[2019,2018]
    },
  "include_fields": [
    "ApplId","SubprojectId","FiscalYear","Organization", "ProjectNum","OrgCountry",
    "ProjectNumSplit","ContactPiName","AllText","FullStudySection",
    "ProjectStartDate","ProjectEndDate"
  ],
  "offset":0,
  "limit":25,
  "sort_field":"project_start_date",
  "sort_order":"desc"
}

response = requests.post("https://api.reporter.nih.gov/v2/projects/search", json=params)

print(response.status_code)
print(response.text)

结果的开头如下所示:

{"meta":{"search_id":null,"total":160216,"offset":0,"limit":25,"sort_field":"project_start_date","sort_order":"desc","sorted_by_relevance":false,"properties":{}},"results":[{"appl_id":10396858,"subproject_id":null,"fiscal_year":2018,"project_num":"7K01AG046366-06","organization":{"org_name":"UNIVERSITY OF CONNECTICUT SCH OF MED/DNT","city":null,"country":null,"org_city":"FARMINGTON","org_country":"UNITED STATES","org_state":"CT","org_state_name":null,"dept_type":"NEUROSCIENCES","fips_country_code":null,"org_duns":["022254226"],...