415代码使用httr和RCurl,但不只是curl [英] 415 code using httr and RCurl, but not just curl
问题描述
我想写一个函数来处理Spotify的API的一些认证。我可以让它使用一个相当简单的curl命令,但是当我尝试使用httr或RCurl,我得到 415不支持的媒体类型响应。我在这一点上有点失落。我已经得到 POST()
和 GET()
使用这个API,但这个端点不是
accept_json(),
add_headers('Authorization'= paste('Basic',base64 (client_id,':',client_secret)),sep ='')),
body = list(grant_type ='client_credentials'),
encode ='json')
我得到一个415 status_code(response)
使用 RCurl :
httpheader< - c '= paste('Basic',base64(paste(client_id,':',client_secret,sep ='')),sep =''))
jsonbody< - toJSON(list(grant_type ='client_credentials' ))
postForm('https://accounts.spotify.com/api/token/',
postfields = jsonbody,
#grant_type ='client_credentials',
.opts = list(httpheader = httpheader,
verbose = TRUE))
postForm输出: p>
*在DNS缓存中找不到主机名
*尝试194.132.198.228 ...
*连接到帐户.spotify.com(194.132.198.228)port 443(#9)
*在/etc/ssl/certs/ca-certificates.crt中找到173个证书
*服务器证书验证OK
*通用名称:* .spotify.com(匹配)
*服务器证书过期日期OK
*服务器证书激活日期OK
*证书公钥:RSA
*证书版本: 3
*主题:C = SE,ST =斯德哥尔摩,L =斯德哥尔摩,O = Spotify AB,CN = *。spotify.com
*开始日期:星期二,2014年04月15日00:00:00 GMT
*过期日期:Wed,21 Jun 2017 12:00:00 GMT
*发行商:C = US,O = DigiCert Inc,CN = DigiCert SHA2安全服务器CA
* compression:NULL
* cipher:AES-256-CBC
* MAC:SHA256
> POST / api / token / HTTP / 1.1
Host:accounts.spotify.com
接受:* / *
授权:Basic ZWE ... zE =
Content-Length: 182
预期:100-continue
Content-Type:multipart / form-data; boundary = ------------------------ 95ce917e6abd21b1
< HTTP / 1.1 100继续
< HTTP / 1.1 415不支持的媒体类型
*服务器nginx未列入黑名单
<服务器:nginx
<日期:Wed,21 Oct 2015 01:38:39 GMT
< Content-Length:990
<连接:keep-alive
< Keep-Alive:timeout = 10
*发送结束之前的HTTP错误,停止发送
<
*关闭连接9
错误:不支持的媒体类型
< curl 通过系统
调用(或从命令行)实际工作:
code> system(
paste('curl https://accounts.spotify.com/api/token -HAuthorization:Basic',
base64(paste(client_id,':') client_secret,sep ='')),
'-d grant_type = client_credentials',
sep =''),
intern = TRUE)
不确定这在此时是否有用。
> devtools :: session_info()
会话信息-------------------------------------- -------------------------------------------------- ---------------
设置值
版本R版本3.2.2(2015-08-14)
系统x86_64,linux-gnu
ui RStudio(0.99.441)
language en_US
collate en_US.UTF-8
tz< NA>
date 2015-10-20
包------------------------------- -------------------------------------------------- --------------------------
package *版本日期源
bitops 1.0-6 2013-08-17 CRAN( R 3.2.1)
caTools 1.17.1 2014-09-10 CRAN(R 3.2.1)
colorpace 1.2-6 2015-03-11 CRAN(R 3.2.1)
curl 0.9.3 2015-08-25 CRAN(R 3.2.2)
DBI 0.3.1 2014-09-24 CRAN(R 3.2.1)
devtools * 1.9.1.9000 2015-10-21 Github (hadley / devtools @ 0295d20)
digest 0.6.8 2014-12-31 CRAN(R 3.2.1)
git2r 0.11.0 2015-08-12 CRAN(R 3.2.2)
httr * 1.0.0.9000 2015-10-21 github(hadley / httr @ f7593b7)
jsonlite * 0.9.17 2015-09-06 CRAN(R 3.2.2)
knitr 1.11 2015-08- 14 CRAN(R 3.2.2)
magrittr 1.5 2014-11-22 CRAN(R 3.2.1)
memoise 0.2.1 2014-04-22 CRAN(R 3.2.1)
R6 2.1.1 2015-08-19 CRAN(R 3.2.2)
Rcpp 0.12.1 2015-09-10 CRAN(R 3.2.2)
RCurl * 1.96-0 2015-07-27本地
RJSONIO * 1.3-0 2014-07-28 CRAN(R 3.2.2)
roxygen2 4.1.1 2015-04-15 CRAN(R 3.2.1)
RSelenium * 1.3。 5 2014-10-26 CRAN(R 3.2.2)
rstudioapi 0.3.1 2015-04-07 CRAN(R 3.2.1)
stringi 0.5-5 2015-06-29 CRAN(R 3.2 .2)
stringr * 1.0.0 2015-04-30 CRAN(R 3.2.2)
XML * 3.98-1.3 2015-06-30 CRAN(R 3.2.2)
解决方案特定的Spotify端点正在寻找
Content- :application / x-www-form-urlencoded
与JSON相同。这应该工作(它为我做):library(httr)
response< POST('https://accounts.spotify.com/api/token',
accept_json(),
authenticate(Sys.getenv(SPOTIFY_CLIENT_ID),Sys.getenv(SPOTIFY_CLIENT_SECRET)) ,
body = list(grant_type ='client_credentials'),
encode ='form',
verbose())
(您可以移除生产中的
verbose()
)
注意
encode ='form'
,但也要注意,你可以使用authenticate()
auth头(我存储env变量中的所有键,因此使用Sys.getenv()
。I'm trying to write a function that handles some of the authentication for Spotify's API. I can get it to work with a fairly simple curl command, but when I try to use httr or RCurl, I get 415 Unsupported Media Type responses. I'm somewhat at a loss at this point. I've gotten
POST()
, andGET()
to work with this API already, but this endpoint is not working.Using httr:
response <- POST('https://accounts.spotify.com/api/token', accept_json(), add_headers('Authorization'=paste('Basic',base64(paste(client_id,':',client_secret)),sep=' ')), body=list(grant_type='client_credentials'), encode='json')
I get a 415
status_code(response)
Using RCurl:
httpheader <- c('Authorization'=paste('Basic ',base64(paste(client_id,':',client_secret,sep='')),sep='')) jsonbody <- toJSON(list(grant_type='client_credentials')) postForm('https://accounts.spotify.com/api/token/', postfields=jsonbody, # grant_type='client_credentials', .opts = list(httpheader=httpheader, verbose=TRUE))
postForm output:
* Hostname was NOT found in DNS cache * Trying 194.132.198.228... * Connected to accounts.spotify.com (194.132.198.228) port 443 (#9) * found 173 certificates in /etc/ssl/certs/ca-certificates.crt * server certificate verification OK * common name: *.spotify.com (matched) * server certificate expiration date OK * server certificate activation date OK * certificate public key: RSA * certificate version: #3 * subject: C=SE,ST=Stockholm,L=Stockholm,O=Spotify AB,CN=*.spotify.com * start date: Tue, 15 Apr 2014 00:00:00 GMT * expire date: Wed, 21 Jun 2017 12:00:00 GMT * issuer: C=US,O=DigiCert Inc,CN=DigiCert SHA2 Secure Server CA * compression: NULL * cipher: AES-256-CBC * MAC: SHA256 > POST /api/token/ HTTP/1.1 Host: accounts.spotify.com Accept: */* Authorization: Basic ZWE...zE= Content-Length: 182 Expect: 100-continue Content-Type: multipart/form-data; boundary=------------------------95ce917e6abd21b1 < HTTP/1.1 100 Continue < HTTP/1.1 415 Unsupported Media Type * Server nginx is not blacklisted < Server: nginx < Date: Wed, 21 Oct 2015 01:38:39 GMT < Content-Length: 990 < Connection: keep-alive < Keep-Alive: timeout=10 * HTTP error before end of send, stop sending < * Closing connection 9 Error: Unsupported Media Type
using curl through a
system
call (or from command line) actually works:system( paste('curl https://accounts.spotify.com/api/token -H "Authorization: Basic ', base64(paste(client_id,':',client_secret,sep='')), '" -d grant_type=client_credentials ', sep=''), intern=TRUE)
Not sure if this is useful at this point.
> devtools::session_info() Session info ------------------------------------------------------------------------------------------------------- setting value version R version 3.2.2 (2015-08-14) system x86_64, linux-gnu ui RStudio (0.99.441) language en_US collate en_US.UTF-8 tz <NA> date 2015-10-20 Packages ----------------------------------------------------------------------------------------------------------- package * version date source bitops 1.0-6 2013-08-17 CRAN (R 3.2.1) caTools 1.17.1 2014-09-10 CRAN (R 3.2.1) colorspace 1.2-6 2015-03-11 CRAN (R 3.2.1) curl 0.9.3 2015-08-25 CRAN (R 3.2.2) DBI 0.3.1 2014-09-24 CRAN (R 3.2.1) devtools * 1.9.1.9000 2015-10-21 Github (hadley/devtools@0295d20) digest 0.6.8 2014-12-31 CRAN (R 3.2.1) git2r 0.11.0 2015-08-12 CRAN (R 3.2.2) httr * 1.0.0.9000 2015-10-21 Github (hadley/httr@f7593b7) jsonlite * 0.9.17 2015-09-06 CRAN (R 3.2.2) knitr 1.11 2015-08-14 CRAN (R 3.2.2) magrittr 1.5 2014-11-22 CRAN (R 3.2.1) memoise 0.2.1 2014-04-22 CRAN (R 3.2.1) R6 2.1.1 2015-08-19 CRAN (R 3.2.2) Rcpp 0.12.1 2015-09-10 CRAN (R 3.2.2) RCurl * 1.96-0 2015-07-27 local RJSONIO * 1.3-0 2014-07-28 CRAN (R 3.2.2) roxygen2 4.1.1 2015-04-15 CRAN (R 3.2.1) RSelenium * 1.3.5 2014-10-26 CRAN (R 3.2.2) rstudioapi 0.3.1 2015-04-07 CRAN (R 3.2.1) stringi 0.5-5 2015-06-29 CRAN (R 3.2.2) stringr * 1.0.0 2015-04-30 CRAN (R 3.2.2) XML * 3.98-1.3 2015-06-30 CRAN (R 3.2.2)
解决方案That particular Spotify endpoint is looking for
Content-Type: application/x-www-form-urlencoded
vs JSON as you have it. This should work (it did for me):library(httr) response <- POST('https://accounts.spotify.com/api/token', accept_json(), authenticate(Sys.getenv("SPOTIFY_CLIENT_ID"), Sys.getenv("SPOTIFY_CLIENT_SECRET")), body=list(grant_type='client_credentials'), encode='form', verbose())
(You can remove the
verbose()
in production)Note the
encode='form'
but also note that you can useauthenticate()
vs build your own basic auth header (I store all keys in env variables, hence the use ofSys.getenv()
.这篇关于415代码使用httr和RCurl,但不只是curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!