我正在尝试使用内置的Admin API从R连接到本地的Ghost CMS实例。有一个很好的文档(https://ghost.org/docs/admin-api/#token-authentication),介绍了如何在各种语言中进行连接,但不幸的是没有提供给R的文档。我已经编写了以下代码,但不幸的是在尝试创建一个测试文章时收到了401错误。非常感谢任何帮助。
R代码:
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))
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号