国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Ghost CMS API 的 R 接口
P粉134288794
P粉134288794 2023-08-03 12:45:37
0
1
728
<p>我正在嘗試使用內(nèi)置的Admin API從R連接到本地的Ghost CMS實例。有一個很好的文檔(https://ghost.org/docs/admin-api/#token-authentication),介紹了如何在各種語言中進行連接,但不幸的是沒有提供給R的文檔。我已經(jīng)編寫了以下代碼,但不幸的是在嘗試創(chuàng)建一個測試文章時收到了401錯誤。非常感謝任何幫助。<br /><br />R代碼:</p><p><strong></strong></p> <pre class="brush:php;toolbar:false;">api_admin_key <- "xxxxxx:yyyyyyyyyyyyyyy" api_admin_key <- unlist(strsplit(x = api_admin_key, split = ":")) names(api_admin_key) <- c("id", "secret") # Prepare header and payload iat <- as.integer(Sys.time()) header <- list(alg = 'HS256', typ = 'JWT', kid = api_admin_key[["id"]]) # Create the token (including decoding secret) payload <- jose::jwt_claim(iat = iat, exp = iat + 5 * 60, aud = '/admin/') token <- jose::jwt_encode_hmac( claim = payload, secret = charToRaw(api_admin_key[["secret"]]), size = 256, header = header ) # Make an authenticated request to create a post url <- 'http://localhost:2368/ghost/api/admin/posts/' headers <- c('Authorization' = paste("Ghost", token)) body <- list(posts = list( "title" = 'Hello World', "html" = "<p>My post content. Work in progress...</p>", "status" = "published" ) ) httr::POST(url, body = body, encode = "json", httr::add_headers(.headers = headers))</pre> <p><br /></p>
P粉134288794
P粉134288794

全部回復(fù)(1)
P粉739706089

看起來問題出在你傳遞給jwt_encode_hmac()的secret=參數(shù)上。charToRaw函數(shù)無法理解十六進制數(shù)字,它只使用ASCII字符碼。要進行轉(zhuǎn)換,你需要使用現(xiàn)有問題中的其中一個hex_to_raw函數(shù)。我在這里使用一個函數(shù)來進行轉(zhuǎn)換。

hex_to_raw <- function(x) {
  digits <- strtoi(strsplit(x, "")[[1]], base=16L)
  as.raw(bitwShiftL(digits[c(TRUE, FALSE)],4) + digits[c(FALSE, TRUE)])
}

另外,你不需要在頭部指定alg和typ,因為這些會由函數(shù)自動添加。所以你可以使用以下方式構(gòu)建你的令牌:

api_admin_key <- "adam:12bd18f2cd12"

api_admin_key <- unlist(strsplit(x = api_admin_key, split = ":"))
names(api_admin_key) <- c("id", "secret")

# Prepare header and payload
iat <- as.integer(Sys.time())
header <- list(kid = api_admin_key[["id"]])

# Create the token (including decoding secret)
payload <-
  jose::jwt_claim(iat = iat,
                  exp = iat + 5 * 60,
                  aud = '/admin/')

token <-
  jose::jwt_encode_hmac(
    claim = payload,
    secret = hex_to_raw(api_admin_key[["secret"]]),
    size = 256,
    header = header
  )

我使用https://jwt.io/上的調(diào)試器測試了每個令牌,它們似乎是等價的。在調(diào)試器中,十六進制值"12bd18f2cd12"的Base64編碼值是"Er0Y8s0S"。

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板