R interface to the Ghost CMS API
P粉134288794
2023-08-03 12:45:37
<p>I'm trying to connect to a local Ghost CMS instance from R using the built-in Admin API. There is a good documentation (https://ghost.org/docs/admin-api/#token-authentication) on how to connect in various languages, but unfortunately there is no documentation for R. I've written the following code but unfortunately am receiving a 401 error when trying to create a test article. Any help is greatly appreciated. <br /><br />R code:</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>
Looks like the problem is with the secret= parameter you passed to jwt_encode_hmac(). The charToRaw function cannot understand hexadecimal numbers, it only uses ASCII character codes. To do the conversion, you need to use one of the hex_to_raw functions from the existing question. I'm using a function here to do the conversion.
Also, you don't need to specify alg and typ in the header, because these will be added automatically by the function. So you can build your token using:
I tested each token using the debugger at https://jwt.io/ and they appear to be equivalent. In the debugger, the Base64 encoded value of the hexadecimal value "12bd18f2cd12" is "Er0Y8s0S".