1

The new Twitter v2 is a major pain. I really hate that I have to re-authorize every two hours with Oauth2, which means that I can't tweet auto-generated charts once a day unless I happen to be sitting by my computer to re-authorize. But I digress.

This post was helpful Twitter Setup for R / rtweet and I have managed to use my new developer account and tweet_post in rtweet to post a text tweet after authorizing. However, I can't for the life of me figure out how to post a .png along with text.

library(rtweet);
myclient <-rtweet_client(client_id,client_secret,app,scopes=NULL)
auth<-rtweet_oauth2(client = myclient, scopes = NULL)
png_path <- "P:/myfiles/projCPI.png"
statTweet <- "what i am saying to you"
tweet_post(statTweet, media=png_path, media_alt_text="what i am showing to you",token=auth)

The aforementioned code produces the following error and traceback (which, since I'm not a coder by trade, is not clear to me):

Error in `httr2::req_perform()`:
! Failed to parse error body with method defined in req_error()
Caused by error in `FUN()`:
! is.list(x) is not TRUE
Run `rlang::last_trace()` to see where the error occurred.

> rlang::last_trace()
> \<error/rlang_error\>
> Error in `httr2::req_perform()`:
> ! Failed to parse error body with method defined in req_error()
> Caused by error in `FUN()`:
> ! is.list(x) is not TRUE

---

Backtrace:
x

 1. -rtweet::tweet_post(...)
 2. -httr2::req_perform(req_final)
 3.     +-httr2:::resp_abort(resp, error_body(req, resp), call = error_call)
 4.     | \-rlang::abort(...)
 5.     |   \-rlang::is_formula(message, scoped = TRUE, lhs = FALSE)
 6.     \-httr2:::error_body(req, resp)
 7.       +-rlang::try_fetch(...)
 8.       | +-base::tryCatch(...)
 9.       | | \-base (local) tryCatchList(expr, classes, parentenv, handlers)
10.       | |   \-base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
11.       | |     \-base (local) doTryCatch(return(expr), name, parentenv, handler)
12.       | \-base::withCallingHandlers(...)
13.       \-httr2:::req_policy_call(req, "error_body", list(resp), default = NULL)
14.         +-rlang::exec(req$policies[[name]], !!!args)
15.         \-rtweet (local) `<fn>`(`<httr2_rs>`)
16.           +-base::do.call(rbind, lapply(r$errors, list2DF))
17.           \-base::lapply(r$errors, list2DF)
18.             \-base (local) FUN(X[[i]], ...)
19.               \-base::stopifnot(is.list(x), is.null(nrow) || nrow >= 0L)
20.                 \-base::stop(simpleError(msg, call = if (p <- sys.parent(1L)) sys.call(p)))

Run rlang::last_trace(drop = FALSE) to see 3 hidden frames.

I've tried to post the image as an object, or as a path to an uploaded image...I really am at a loss here. Seems like most of the twitter API applications involve reading, and very few involve posting - but I have no interest in reading and just want to communicate certain useful stuff to followers...

I have tried twitteR but seem to have similar issues. And I can't find any other package. Maybe I could do this directly with httr2 but as I said I'm not a coder and http is a new language to me. Any suggestions appreciated!

MrFlick
  • 195,160
  • 17
  • 277
  • 295

1 Answers1

0

Note that the basic API access only allows to post and destroy your tweets (besides reading data about your own account). Other plans allow more like blocking, liking, following...

Currently the API v2 still needs the API v1 to post images. You should read the API reference page to learn how to post images but I'll try to add an example about how to do it.

I am not sure how combining both APIs will work, it might be too complicated or it might not work.

llrs
  • 3,308
  • 35
  • 68
  • Right - I only want to post. Not even destroy. Not read data. Just post! I'm not sure how to use both APIs in one command...do I have to pass two tokens? Or heck, if I can just post images using the old post_tweet approach, which doesn't have the two hour timeout, as long as I don't include a text part of the tweet...I'd happily do that. – Michael Ashton Jun 28 '23 at 16:29
  • No, passing two tokens won't work (I'm the maintainer of the package). You can't post the image with the old `post_tweet` because that API endpoint no longer works. I created an issue in rtweet to add the example https://github.com/ropensci/rtweet/issues/778. First some internal function of rtweet should be used and then `tweet_post` should use the output of this as one of the arguments. – llrs Jun 29 '23 at 09:46
  • Yes - you're a legend to me! :-) I eagerly await the example. Thank you very much. – Michael Ashton Jun 29 '23 at 19:57