2014-01-28 100 views
3

我試圖從R內通過HTTP請求查詢Yelp。我無法使oauth_signature參數匹配Yelp的預期。使用http請求查詢yelp - 無效簽名

這是我的代碼到目前爲止。

YWSID <- "..." 
CONSUMER_KEY <- "..." 
CONSUMER_SECRET <- "..." 
TOKEN <- "..." 
TOKEN_SECRET <- "..." 

yelp_bus <- function(rest, ywsid, cons_key, cons_sec, tok, tok_sec) { 
    require(package = "digest") 
    require(package = "rjson") 
    require(package = "RCurl") 
    require(package = "stringr") 
    rest1 <- gsub(pattern = " ", replacement = "%2520", x = rest) 
    rest2 <- gsub(pattern = " ", replacement = "+", x = rest) 
    nonce <- paste(sample(x = c(0:9, letters, LETTERS, "_"), size = 32, replace = TRUE), collapse = "") 
    tm <- format(x = Sys.time(), "%s") 
    api_url <- paste("GET\u0026http%3A%2F%2Fapi.yelp.com%2Fv2%2Fsearch\u0026", 
        "limit%3D1%26location%3DChicago%2520IL%26", 
        "oauth_consumer_key%3D", cons_key, "%26", 
        "oauth_nonce%3D", nonce, "%26", 
        "oauth_signature_method%3DHMAC-SHA1%26", 
        "oauth_timestamp%3D", tm, "%26", 
        "oauth_token%3D", tok, "%26", 
        "term%3D", rest1, 
        sep = "") 
    signature <- hmac(key = tok_sec, object = api_url, algo = "sha1") 
    api_url <- paste("http://api.yelp.com/v2/search?", 
        "limit=1&location=Chicago+IL&", 
        "oauth_consumer_key=", cons_key, "&", 
        "oauth_nonce=", nonce, "&", 
        "oauth_signature=", signature, "&", 
        "oauth_signature_method=HMAC-SHA1&", 
        "oauth_timestamp=", tm, "&", 
        "oauth_token=", tok, "&", 
        "term=", rest2, 
        sep = "") 
    return(api_url) 
} 

yelp_bus(rest = "chez moi", 
     ywsid = YWSID, 
     cons_key = CONSUMER_KEY, 
     cons_sec = CONSUMER_SECRET, 
     tok = TOKEN, 
     tok_sec = TOKEN_SECRET) 

每次都遇到了這個錯誤了。

{"error": {"text": "Signature was invalid", "id": "INVALID_SIGNATURE", "description": "Invalid signature. Expected signature base string: ..."}} 
+0

你比較在錯誤信息的描述,你是在本地生成的一個嗎?它們應該是相同的。 – John

+0

@John當我在散列之前打印'api_url'時,「\ u0026」變成了&符號。所以我猜這是我的問題。任何想法如何離開文本是? – Max

回答

2

創建簽名如下:

signature <- as.character(curlPercentEncode(base64(hmac(key=paste(cons_sec, tok_sec, sep="&"), object=api_url, algo="sha1", serialize=FALSE, raw=TRUE))))