sferik / x-ruby

A Ruby interface to the X API.
MIT License
74 stars 13 forks source link

400 Bad Request without details #5

Closed noraj closed 12 months ago

noraj commented 12 months ago

My code is https://gitlab.com/rawsec/rawsec-inventory-twitter-bot/-/blob/master/xBot.rb

In short (without the unnecessary parts):

require 'x'

X_CREDENTIALS = {
  api_key: ENV['twitter_api_key'],
  api_key_secret: ENV['twitter_api_key_secret'],
  access_token: ENV['twitter_access_token'],
  access_token_secret: ENV['twitter_access_token_secret']
}
# Initialize an X API client with OAuth credentials
x_client = X::Client.new(**X_CREDENTIALS)

x_client.post('tweets', "{\"text\":\"#{message}\"}")

And I'm just receiving this error without much details:

➜ ruby xBot.rb 
/home/noraj/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/x-0.8.1/lib/x/response_handler.rb:41:in `handle': 400 Bad Request (X::BadRequestError)
        from /home/noraj/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/x-0.8.1/lib/x/client.rb:80:in `send_request'
        from /home/noraj/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/x-0.8.1/lib/x/client.rb:50:in `post'
        from xBot.rb:81:in `<main>'

PS: That's no credential issues, I had another error (401 or 403) when the credentials were wrong.

drmikexo2 commented 12 months ago

I'm not able to replicate. Try adding the 'httplog' gem in your Gemfile to see Twitter's response in greater detail. Also make sure you're using 3-legged OAuth user-context authentication and not app-context authentication.

json = { "text": "ok" }.to_json
client.post("tweets", json)
[...httplog headers omitted]
[httplog] 2023-09-22 16:12:29 -0600 Response: {"data":{"edit_history_tweet_ids":["1705344340552220696"],"id":"1705344340552220696","text":"ok"}}

When I try it again:

client.post("tweets", json)
[httplog] 2023-09-22 16:15:53 -0600 Response: {"detail":"You are not allowed to create a Tweet with duplicate content.","type":"about:blank","title":"Forbidden","status":403}
noraj commented 12 months ago

json = { "text": "ok" }.to_json client.post("tweets", json)

Yeah was just a JSON escaping issue, my bad, I feel so lame.