谷歌API-URL缩短器,带R
我正在尝试使用R快速缩短一批URL。GoogleAPI文档提供了下面使用curl的解决方案谷歌API-URL缩短器,带R,r,api,curl,rcurl,R,Api,Curl,Rcurl,我正在尝试使用R快速缩短一批URL。GoogleAPI文档提供了下面使用curl的解决方案 curl https://www.googleapis.com/urlshortener/v1/url \ -H 'Content-Type: application/json' \ -d '{"longUrl": "http://www.google.com/"}' 我试着用R把它转换成R,但我总是得到“错误:错误的请求”。这就是我的工作 library(RCurl) library(RJSO
curl https://www.googleapis.com/urlshortener/v1/url \
-H 'Content-Type: application/json' \
-d '{"longUrl": "http://www.google.com/"}'
我试着用R把它转换成R,但我总是得到“错误:错误的请求”。这就是我的工作
library(RCurl)
library(RJSONIO)
postForm( "https://www.googleapis.com/urlshortener/v1/url" ,
.params= c(data = '{"longUrl":"www.google.com"}'),
.opts = list( httpheader = "Content-Type: application/json",
ssl.verifypeer = FALSE))
下面是一个使用httr作为RCurl包装器的解决方案
> library("httr")
> POST('https://www.googleapis.com/urlshortener/v1/url',
add_headers("Content-Type"="application/json"),
body='{"longUrl": "http://www.google.com/"}')
Response [https://www.googleapis.com/urlshortener/v1/url]
Status: 200
Content-type: application/json; charset=UTF-8
{
"kind": "urlshortener#url",
"id": "http://goo.gl/fbsS",
"longUrl": "http://www.google.com/"
}
在按照来自的建议使用Rcurl+JSONIO时,我更幸运地解析了输出
库(RCurl)
图书馆(RJSONIO)
测试跳过json中的斜杠。它对您有用吗?如果是这样的话,你可以发布代码,因为它总是给我同样的错误。
library(RCurl)
library(RJSONIO)
test <- postForm("https://www.googleapis.com/urlshortener/v1/url",
.opts = list(postfields = toJSON(list(longUrl = "http://www.google.com/")),
httpheader = c('Content-Type' = 'application/json', Accept = 'application/json'),
ssl.verifypeer = FALSE))