. . .

Twitter Module

Using the Twitter module you can let your application users connect themselves with Twitter. OAuth attributes are stored on the user model. Additionally, you can create tweets, read your timeline and mentions and send/receive direct messages.

images/download/attachments/19431609/db3-module-twitter.PNG

Configuration

Consumer key

Consumer key of the Twitter app which you want to use to authenticate against. Defaults to the one of ApiOmat, change only if you want to use your own app.

Callback URL

Callback URL of the Twitter app which you want to use to authenticate against. Defaults to the one of ApiOmat, change only if you want to use your own app.

Consumer secret

Consumer secret of the Twitter app which you want to use to authenticate against. Defaults to the one of ApiOmat, change only if you want to use your own app.
More can be found in the twitter docs.

Usage

After setting the configuration parameters, you may call the following URL in your app to start authentication for a user, replacing YOURAPPNAME with the name of your app:

https://apiomat.org/yambas/rest/modules/Twitter/spec/YOURAPPNAME/auth?memberId=USERID&usedSystem=SYSTEM

The user is presented the Twitter site asking him if the requesting app (with the ID set in the configuration) may be used to authenticate him. If the user clicks OK the authentication starts, otherwise it is cancelled.

If authentication was successful, the user object will have three additional attributes set on the users object on server side: oauthToken, oAuthVerifier and oAuthSecret. To fetch these you have to do a simple reload of the user, so your code may look like this:

Android
callAuthURL();
// do other things
 
// later on get the actualized user object
user.loadMeAsync(new AOMEmptyCallback() {
 
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null){
System.out.println("Auth code: " + user.getOauthToken());
}
}
});
iOS
callAuthURL();
// do other things
 
// later on get the actualized user object
[user loadMeWithBlock:^(NSError *error) {
NSLog(@"Auth code: %@", [user getOAuthToken]);
}];
JavaScript
var user= new Apiomat.User();
 
callAuthUrl();
 
user.loadMe({
onOk : function() {
console.log("accessToken: " + user.getAccessToken());
},
onError : function(error) {
}
});

Using these values you can access Twitter API without doing a rather complicated OAuth handshake by yourself.

Sending tweets

Android
Tweet t = new Tweet();
t.setText("my tweet from ApiOmat");
t.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
// ...
}
}
});
iOS
AOMTweet *t = [[AOMTweet alloc] init];
[t setText:@"my tweet from ApiOmat"];
[t saveAsyncWithBlock:^(NSError *error) {
}];
JavaScript
var t = new Apiomat.Tweet();
t.setText("my tweet from ApiOmat");
 
var saveCB = {
onOk : function() {
console.log("saved");
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
t.save(saveCB);

Receiving your timeline

Android
private void test() {
final Timeline t = new Timeline();
t.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
processTweets(t);
}
}
});
}
 
private void processTweets(final Timeline t) {
t.loadTweetsAsync("", new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
List tweets = t.getTweets();
// process..
}
}
});
}
iOS
AOMTimeline *t = [[AOMTimeline alloc] init];
//just to get href back from server
[t saveAsyncWithBlock:^(NSError *error) {
[t loadTweetsAsync:@"" andWithBlock:^(NSError *error) {
NSMutableArray *tweets = [t tweets];
}];
}];
JavaScript
function test() {
var t = new Apiomat.Timeline();
var saveCB = {
onOk : function() {
processTweets(t);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
t.save(saveCB);
}
 
function processTweets(t) {
var loadTweetsCB = {
onOk : function() {
var tweets=t.getTweets();
//process
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
/* The first argument describes a filter query. If you do not want to filter the tweets, just pass undefined */
t.loadTweets(undefined, loadTweetsCB);
}

Sending direct messages

Android
DirectMessage msg = new DirectMessage();
msg.setText("Hello world");
msg.setRecipientName("APIOMAT"); //insert the twitter user name of the receiver here
msg.saveAsync(new AOMEmptyCallback() {
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
// ...
}
}
});
iOS
AOMDirectMessage *msg = [[AOMDirectMessage alloc] init];
[msg setText:@"Hello world"];
[msg setRecipientName:@"APIOMAT"]; //insert the twitter user name of the receiver here
[msg saveAsyncWithBlock:^(NSError *error) {
}];
JavaScript
var msg = new Apiomat.DirectMessage();
msg.setText("Hello world");
msg.setRecipientName("APIOMAT"); //insert the twitter user name of the receiver here
 
var saveCB = {
onOk : function() {
console.log("saved");
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
msg.save(saveCB);

Getting direct messages

Android
private void test() {
final DirectMessages msgContainer = new DirectMessages(); //Notice the container class is used here!
msgContainer.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
processDirectMessages(msgContainer);
}
}
});
}
 
private void processDirectMessages(final DirectMessages msgContainer) {
msgContainer.loadDirectMessagesAsync("", new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
List<Tweet> myMessages = msgContainer.getDirectMessages();
// ...
}
}
});
}
iOS
AOMDirectMessages *msgContainer = [[AOMDirectMessages alloc] init]; //Notice the container class is used here!
//just to get href back from server
[msgContainer saveAsyncWithBlock:^(NSError *error) {
[msgContainer loadDirectMessagesAsync:@"" andWithBlock:^(NSError *error) {
NSMutableArray *myMessages = [msgContainer directMessages];
}];
}];
JavaScript
function test() {
var msgContainer = new Apiomat.DirectMessages(); //Notice the container class is used here!
var saveCB = {
onOk : function() {
processDirectMessages(msgContainer);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
msgContainer.save(saveCB);
}
function processDirectMessages(msgContainer) {
var loadDirectMessagesCB = {
onOk : function() {
var myMessages =msgContainer.getDirectMessages();
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
msgContainer.loadDirectMessages(loadDirectMessagesCB);
}
* link only available in Enterprise Documentation