balanv
balanv

Reputation: 10898

Uploading image with status to Twitter using Rails

I am using a function called prepare_access_token to contact Twitter api to perform Read, Write functions.

Below is the code i use to update twitter message


@tweet = "Rails Rails"
@access_token = prepare_access_token(@oauth_token, @oauth_secret)
@response = @access_token.request(:post, "http://api.twitter.com/1/statuses/update.json", :status => @tweet)

def prepare_access_token(oauth_token, oauth_token_secret)

  consumer_key = Rails.application.config.consumer_key
  consumer_secret = Rails.application.config.consumer_secret  

  consumer = OAuth::Consumer.new(consumer_key, consumer_secret,
    { :site => "http://api.twitter.com",
      :scheme => :header
    })
  # now create the access token object from passed values
  token_hash = { :oauth_token => oauth_token,
                 :oauth_token_secret => oauth_token_secret
               }
  access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
  return access_token
end

This works for status update without attached image , but when i try to update status with image attachment like below

@access_token = prepare_access_token(@oauth_token, @oauth_secret)
@response = @access_token.request(:post,  "https://upload.twitter.com/1/statuses/update_with_media.json"  , :media => $image , :status  => "Status")

it shows like {"request":"/1/statuses/update_with_media.json","error":"Error creating status."}

what am i doing wrong? please suggest.

Upvotes: 1

Views: 1509

Answers (2)

Interceptor
Interceptor

Reputation: 21

For the consumer:site, try setting it to http://upload.twitter.com instead of http://api.twitter.com. the api site does not support media upload.

Upvotes: 2

air-dex
air-dex

Reputation: 4180

Unlike other REST methods to the Twitter API, you have to set the Content-Type to multipart/form-data for this POST https://upload.twitter.com/1/statuses/update_with_media.json method.

Upvotes: 0

Related Questions