Interact with the Twitter API v1.1 on Pharo.
- Pharo 10 reference platform.
- If you do not have a Twitter app set up:
- Sign in to Twitter and go to https://developer.twitter.com/ to create an app.
- Retrieve the app API key, API key secret, access token, and access token secret.
- Load this project in Pharo. In a Playground, Do it:
Metacello new
repository: 'github://brackendev/TwitterSDK-Pharo/src';
baseline: 'TwitterSDK';
load.
twitterSDK := TwitterSDK createWithAPIKey: API_KEY apiKeySecret: API_KEY_SECRET accessToken: ACCESS_TOKEN accessTokenSecret: ACCESS_SECRET.
"Follow @brackendev"
parameters := Dictionary newFrom: {'screen_name' -> 'brackendev'}.
twitterSDK postPathSegment: 'friendships/create.json' parameters: parameters.
"Post a Tweet with an image"
"Note: The image resides in the Pharo image root directory"
mediaUpload := TwitterSDKTools mediaUploadFile: 'test.jpg' additionalOwners: nil twitterSDK: twitterSDK.
mediaID := (mediaUpload at: 'media_id') asString.
parameters := Dictionary newFrom: {('status' -> 'Test tweet'). ('media_ids' -> mediaID)}.
twitterSDK postPathSegment: 'statuses/update.json' parameters: parameters.
"...or"
TwitterSDKTools postTweetStatus: 'Test tweet' image: 'test.jpg' twitterSDK: twitterSDK.
"Retrieve a Tweet"
parameters := Dictionary newFrom: {('id' -> '1178944243106242560')}.
tweet := (twitterSDK getPathSegment: 'statuses/lookup.json' parameters: parameters) last.
"...or"
tweet := TwitterSDKTools retrieveTweetForID: '1178944243106242560' twitterSDK: twitterSDK.
"Retrieve a Tweet's text"
parameters := Dictionary newFrom: {('id' -> '1178944243106242560')}.
tweet := (twitterSDK getPathSegment: 'statuses/lookup.json' parameters: parameters) last.
text := tweet at: 'text'.
"...or"
text := TwitterSDKTools textForTweetID: '1178944243106242560' twitterSDK: twitterSDK.
"Retrieve a Tweet's media URLs"
tweet := TwitterSDKTools retrieveTweetForID: '1206776380844838914' twitterSDK: twitterSDK.
mediaURLs := TwitterSDKTools mediaURLsForTweet: tweet.
"...and inspect it"
mediaURL := mediaURLs first.
(ImageReadWriter formFromStream: (ZnEasy get: mediaURL) contents readStream) asMorph inspect.
Bracken Spencer
TwitterSDK-Pharo is released under the MIT license. See the LICENSE file for more info.