From 9ed3560c2707709fd2903f20fb751094689f1e05 Mon Sep 17 00:00:00 2001 From: Erik Berlin Date: Wed, 4 Jun 2025 21:26:53 -0700 Subject: [PATCH] Add example for uploading profile media --- examples/upload_profile_media.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 examples/upload_profile_media.rb diff --git a/examples/upload_profile_media.rb b/examples/upload_profile_media.rb new file mode 100644 index 0000000..1fce8c9 --- /dev/null +++ b/examples/upload_profile_media.rb @@ -0,0 +1,22 @@ +require "base64" +require "uri" +require "x" + +x_credentials = { + api_key: "INSERT YOUR X API KEY HERE", + api_key_secret: "INSERT YOUR X API KEY SECRET HERE", + access_token: "INSERT YOUR X ACCESS TOKEN HERE", + access_token_secret: "INSERT YOUR X ACCESS TOKEN SECRET HERE" +} + +client = X::Client.new(base_url: "https://api.twitter.com/1.1/", **x_credentials) + +avatar_path = "path/to/avatar.jpg" +avatar_data = Base64.encode64(File.binread(avatar_path)) +avatar_body = URI.encode_www_form(image: avatar_data) +client.post("account/update_profile_image.json", avatar_body, headers: {"Content-Type" => "application/x-www-form-urlencoded"}) + +banner_path = "path/to/banner.jpg" +banner_data = Base64.encode64(File.binread(banner_path)) +banner_body = URI.encode_www_form(banner: banner_data) +client.post("account/update_profile_banner.json", banner_body, headers: {"Content-Type" => "application/x-www-form-urlencoded"})