參考資料:http://stackoverflow.com/questions/24097998/google-api-url-shortener-with-r

參考資料中並沒有使用google api key,但是Google似乎會限制使用次數,所以比較好的作法是先去申請Google Api key!

#使用httr這個package
library(httr)
apikey <- "你的google api key"
oriurl <- "http://www.pixnet.net"    #這邊放你想要縮短的原始網址
urldata <- POST(paste("https://www.googleapis.com/urlshortener/v1/url?key=", apikey, sep=""),
                add_headers("Content-Type"="application/json"),
                body=toJSON(list(longUrl = oriurl)))
shorturl <- content(urldata)
print(shorturl[["id"]])

這樣就會得到縮短之後的網址啦!

arrow
arrow
    全站熱搜

    Person 發表在 痞客邦 留言(0) 人氣()