Skip to content

Instantly share code, notes, and snippets.

@kalendar
Last active July 20, 2019 22:58
Show Gist options
  • Save kalendar/80aad6f46660143f65a97dd192ebbdcb to your computer and use it in GitHub Desktop.
Save kalendar/80aad6f46660143f65a97dd192ebbdcb to your computer and use it in GitHub Desktop.
Publishing to the Wordpress REST API from R with JSON Web Tokens and httr
library(httr)
# Get the JWT
get_jwt <- POST("https://example.com/throwaway/wp-json/jwt-auth/v1/token",
query = list(username = "your_username", password = "your_password"))
token <- content(get_jwt)$token
# Confirm auth is working
test <- POST("https://example.com/throwaway/wp-json/jwt-auth/v1/token/validate",
add_headers('Authorization' = paste("Bearer", token, sep = " ")), encode = "json")
content(test)
# Publish page
create_page <- POST("https://example.com/throwaway//wp-json/wp/v2/pages",
add_headers('Authorization' = paste("Bearer", token, sep = " ")),
body = list(title = page_title, content = payload, status = "published"), encode = "json")
@ryanscharf
Copy link

you've got a whitespace in the assignment aarow on line 4

@kalendar
Copy link
Author

Thanks! Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment