我想通过API获取所有50个州的天气数据。我试图从中获取此信息的网站是https://www.ncdc.noaa.gov/cdo-web/webservices/v2#gettingStarted
我希望按州获得2017年1月至今的温度数据和降水数据。
以下是我到目前为止的内容。我收到了一封名为“天气秘密”的令牌的电子邮件
##Accessing Weather Data
weatherkey <- "nickknauer1@gmail.com"
weatherSecret <- "tzDOSkVWMToIXgVKfdOuOytFQvPkXTjv"
library("httr")
library("jsonlite")
response = POST(
'https://www.ncdc.noaa.gov/cdo-web/api/token',
accept_json(),
authenticate(weatherkey, weatherSecret),
body = list(grant_type = 'client_credentials'),
encode = 'form',
verbose()
)
token = content(response)$access_token
HeaderValue = paste0('Bearer ', token)
这是输出:
-> POST /cdo-web/api/token HTTP/1.1
-> Host: www.ncdc.noaa.gov
-> Authorization: Basic bmlja2tuYXVlcjFAZ21haWwuY29tOnR6RE9Ta1ZXTVRvSVhnVktmZE91T3l0RlF2UGtYVGp2
-> User-Agent: libcurl/7.59.0 r-curl/3.2 httr/1.3.1
-> Accept-Encoding: gzip, deflate
-> Content-Type: application/x-www-form-urlencoded
-> Accept: application/json
-> Content-Length: 29
->
>> grant_type=client_credentials
<- HTTP/1.1 500 Internal Server Error
<- Date: Mon, 22 Oct 2018 21:08:52 GMT
<- Server: Apache-Coyote/1.1
<- Strict-Transport-Security: max-age=31536000
<- Content-Type: application/json;charset=UTF-8
<- Access-Control-Allow-Origin: *
<- Vary: Accept-Encoding
<- Content-Encoding: gzip
<- Content-Length: 117
<- Connection: close
任何人都知道为什么此连接不起作用吗?
我也在下面尝试过,但这也不起作用:
id_secret <- RCurl::base64(paste(weatherkey,weatherSecret,sep=':'))[[1]]
my_headers <- httr::add_headers(c(Authorization=paste('Basic',
id_secret,sep=' ')))
my_body <- list(grant_type='client_credentials')
my_token <- httr::content(httr::POST('https://www.ncdc.noaa.gov/cdo-web/api/token',
my_headers,body=my_body,encode='form'))