Twitter API -- update statuses endpoint

api

#1

Hey guys, I’m trying to use the twitter api to post a status with an attached image. How can I make use of the media_ids property? I don’t understand how I can derive a media_id from an actual image element.


#2

You’ll have to upload it through the media endpoint(s) which will then return an id for you to use in the status.


#3

Would you mind providing a simple example?


#4

It seems to me that uploading the media through the media endpoint would post the photo. I don’t want to have to post twice for one share.


#5

I use this framework: https://github.com/abraham/twitteroauth

Basically it goes:

    $request = new \Abraham\TwitterOAuth\TwitterOAuth(
                    'consumer_key',
                    'consumer_secret'
                );

                $request->setOauthToken(
                    $post['oauth_token_unserialized']->getToken(),
                    $post['oauth_token_unserialized']->getTokenSecret()
                );

                $media = $request->upload('media/upload', [
                    'media' => $attachment['path']
                ]);

                $mediaIds[] = $media['media_id_str'];

               $result = $request->post(
                'statuses/update',
                [
                    'status' => 'Your message',
                    'media_ids' => implode(',', $mediaIds)
                ]
            );

It won’t post the image twice, it just allows you to upload multiple images as up to 4 images can be used per tweet. You can also attach that media id to more than one tweet.


#6

I don’t use PHP, is there a way I can do this using JavaScript?
Also, just for clarity, $request.result.media_id’s would hold the media_ids value that would be used in the tweet?


#7

Yes it can be done with Javascript but I don’t have any code snippets available for that.

The result from the $request->upload call contains media_id and media_id_str which can be used in the tweet, correct.


#9

I was trying to accomplish this task by just reading excerpts from the documentation.
I’ll stop being lazy and do my research thoroughly. Thanks for the help!


#10

I realise you’re using Javascript, but we do have a sample app in Python that is quite readable and leads you through the whole process of uploading media and creating Tweets with media attached. You may find that a useful reference.


#11

It’s always good to read.