gpt4 book ai didi

r - 使用 http 请求从 r 查询 yelp - 签名无效

转载 作者:行者123 更新时间:2023-12-02 00:01:00 30 4
gpt4 key购买 nike

我正在尝试通过 HTTP 请求从 R 中查询 Yelp。我在获取 oauth_signature 参数以匹配 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: ..."}}

最佳答案

创建签名如下:

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

关于r - 使用 http 请求从 r 查询 yelp - 签名无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21396536/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com