I am trying to tweet an image from Mathematica. I know, this should work on 11, but I am still using 10. My bad.

How I understand that it should work

  1. get an authorisation token. Check.
  2. Upload an image, JPG format. Initiate the upload (command=INIT):

     bytes=" <> bytecount<> "&media_data=<>jpeg<>&media_type=image/jpeg"  

    I used ExportToString to convert the jpg in a string format. This gives me the media_id _string

  3. Tweet using the obtained media_id _string: "api...statuses/update.json?status=2CV&media_ids=803254834484879360"

Which gives the error:

{"errors":[{"code":324,"message":"Invalid media id 803254834484879360"}]}

Checking with FINALISE suggests "error":"Segments do not add up to provided total file size." i.e. I did not do the base64 encoding and byte count correctly. Which probably is the case, since upload and post using this nice site: http://django-rest-apis.herokuapp.com/media from which i learned it should work.

Anybody any suggestion?

  • $\begingroup$ Could you show the entire code you used, with all commands etc? $\endgroup$ – MarcoB Nov 28 '16 at 16:34
  • $\begingroup$ Is there a reason you are not using ServiceConnect to tweet image? Its an inbuilt function and you do not have to bother about the intricacies of converting the image into string etc. More on this can be found at reference.wolfram.com/language/ref/service/Twitter.html under Sending tweets section. $\endgroup$ – dsingh Nov 28 '16 at 16:52
  • $\begingroup$ Prepare image: mImage = Import["/test.jpg"]; jpeg = ExportString[mImage, "JPEG"]; base64 = ExportString[jpeg, "Base64"]; $\endgroup$ – Brusino Nov 28 '16 at 18:47
  • $\begingroup$ urlUpload = "...upload.json&command=INIT&total_bytes=" <> ToString[ByteCount[jpeg]] <> "&media_data=" <> ToString[jpeg] <> "&media_type=image/jpeg"; mImgID = URLExecute[urlUpload, "OAuthAuthentication" -> token, "Method" -> "POST"] (* Output: \ "{\"media_id\":803254834484879360,\"media_id_string\":\"\ 803254834484879360\",\"expires_after_secs\":86399}" *) $\endgroup$ – Brusino Nov 28 '16 at 18:47
  • $\begingroup$ mMediaIDStr = "media_id_string" /. ImportString[mImgID, "JSON"]; wURL = "api.twitter.com/1.1/statuses/update.json?status=\ TestPic&media_ids=" <> ToString[mMediaIDStr]; URLExecute[wURL, "OAuthAuthentication" -> token, "Method" -> "POST"] (* Output: "{\"errors\":[{\"code\":324,\"message\":\"Invalid media id \ 803254834484879360\"}]}" *) $\endgroup$ – Brusino Nov 28 '16 at 18:47

This should work with ServiceConnect.

In[6]:= twitter = ServiceConnect["Twitter"]

Out[6]= ServiceObject["Twitter", 
  "ID" -> "connection-5c77beed5f5bfdf02b1a8074d281a438"]

In[10]:= twitter["Tweet", "Message" -> "Hi Twitter!", 
 "Image" -> Import["ExampleData/spikey.tiff"] ]

Out[10]= "Hi Twitter! https://t.co/YYXZ58lQwJ"

Make sure you use latest paclet.

 In[9]:= PacletInformation["ServiceConnection_Twitter"]

 Out[9]= {"Name" -> "ServiceConnection_Twitter", "Version" ->"11.1.8",.....

 In[11]:= $Version

 Out[11]= "10.4.1 for Mac OS X x86 (64-bit) (April 11, 2016)"
| improve this answer | |
  • $\begingroup$ Great. Thank you! $\endgroup$ – Brusino Nov 28 '16 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.