【问题标题】:415 code using httr and RCurl, but not just curl415 代码使用 httr 和 RCurl,但不仅仅是 curl
【发布时间】:2015-10-21 01:52:10
【问题描述】:

我正在尝试编写一个函数来处理 Spotify 的 API 的一些身份验证。我可以用一个相当简单的 curl 命令让它工作,但是当我尝试使用 httr 或 RCurl 时,我得到 415 Unsupported Media Type 响应。在这一点上,我有些不知所措。我已经让 POST()GET() 使用此 API,但此端点无法正常工作。

使用 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')

我收到了 415 status_code(response)

使用 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 输出:

* 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

通过system 调用(或从命令行)使用 curl 确实有效:

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)


目前不确定这是否有用。

> 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)        

【问题讨论】:

    标签: r api curl rcurl httr


    【解决方案1】:

    那个特定的 Spotify 端点正在寻找 Content-Type: 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() 与构建自己的基本身份验证标头(我将所有密钥存储在环境变量中,因此使用Sys.getenv()

    【讨论】:

    • 是的。这非常有效。谢谢,我以前没有使用过身份验证功能,但这看起来更干净。谢谢!
    【解决方案2】:

    HTTP 415 响应代码通常表明您指定了服务器不支持的 Content-Type 标头,或者您根本没有指定。

    415 不支持的媒体类型: 服务器拒绝为请求提供服务,因为实体 请求的格式不受请求的资源支持 请求的方法。

    您上面的 postForm 示例指定了Content-Type: multipart/form-data;。我可以使用以下curl 重现 HTTP 415 响应:

    curl -v -X POST 'https://accounts.spotify.com/api/token' -H 'Content-Type: multipart/form-data'
    

    如果您将内容类型更改为 application/x-www-form-urlencoded 它应该可以工作。

    curl -v -X POST 'https://accounts.spotify.com/api/token' -H 'Content-Type: application/x-www-form-urlencoded'
    

    【讨论】:

    • 在 R 中,它给出了什么?
    • OP 说他们的curl 版本有效,所以我对这个curl 答案的需求感到有点困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 2013-04-13
    相关资源
    最近更新 更多