Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Last active September 23, 2023 07:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrewheiss/fa6b43cfa94bb11f0c1144b6cb4a0272 to your computer and use it in GitHub Desktop.
Save andrewheiss/fa6b43cfa94bb11f0c1144b6cb4a0272 to your computer and use it in GitHub Desktop.
# ------------------------------------------------------------------------------------------
# Basically all translated from the Python example at https://atproto.com/blog/create-post
# ------------------------------------------------------------------------------------------
library(httr2)
# Create a logged-in API session object
session <- request("https://bsky.social/xrpc/com.atproto.server.createSession") |>
req_method("POST") |>
req_body_json(list(
identifier = Sys.getenv("BSKY_USER"),
password = Sys.getenv("BSKY_PASS")
)) |>
req_perform() |>
resp_body_json()
# This is the token you can use for other API calls:
# session$accessJwt
# Create a post as a list
post_body <- list(
"$type" = "app.bsky.feed.post",
text = "Test post from {httr2}",
createdAt = format(as.POSIXct(Sys.time(), tz = "UTC"), "%Y-%m-%dT%H:%M:%OS6Z"),
langs = list("en-US")
)
# Post the post
resp <- request("https://bsky.social/xrpc/com.atproto.repo.createRecord") |>
req_method("POST") |>
req_headers(Authorization = paste0("Bearer ", session$accessJwt)) |>
req_body_json(list(
repo = session$did,
collection = "app.bsky.feed.post",
record = post_body
))
# Actually make the request and post the thing
resp |> req_perform()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment