. . .

Facebook Module

images/download/attachments/78782023/db3-module-facebook.PNG


Using the Facebook module you can let your application users authorize themselves with their Facebook ID. An accessToken is stored on the authenticated user object.


Configuration

configuration key

description

Facebook Scope

The scopes that you want the user to authorize. Default value here is “email, user_posts” which you should leave like it is until more functionality is added to this module. Read more about possible permissions here.

Facebook App Secret

With the Facebook app secret it’s possible to request data which the user of the Facebook app has approved. If you want to access this data you have to insert the app secrets here.

Facebook App ID

The ID of the Facebook app where you want your users be authorized against. The default value is the ApiOmat Facebook app ID; if you have your own Facebook app, you may enter this one here.


Usage

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

URL

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

If you develop for android or iOS you have to use a WebView to call the URL.

You have to replace YOURAPPNAME with the name of your app, the USERID with the ID of your current User and SYSTEM with the system information. To fetch the UserID you can use this:

Android

Android
int start = user.getHref().lastIndexOf("/");
String id = user.getHref().substring(start + 1);

You can easily access the system information by calling the attribute “system” from the class “User”. So if you are programming with java it may look like this:

Android

Android
webview.loadUrl("http://apiomat.org/yambas/rest/modules/Facebook" +
"/spec/TestApp/auth?memberId=" + id + "&usedSystem=" +User.system);

The Facebook site is presented to the user asking them if the requesting app (with the ID set in the configuration) may get access to the scopes (set in the configuration) after they log in. The authentication starts if the user clicks OK, otherwise it is cancelled.

If authentication was successful, the user object will have an additional field accessToken on server side. To fetch this, you have to do a simple reload of the user, so your code may look like this:

Android

Android
callAuthURL();
// do other things
// later on get the actualized user object
user.loadMeAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
Log.d("MyActivity", "accessToken: " + user.getAccessToken());
}
}
});

Objective-C

Objective-C
callAuthURL();
// do other things
// later on get the actualized user object
[user loadMe:^(NSError * error) {
NSLog(@"accessToken: %@ " , [user getAccessToken]);
}];

Swift

Swift
callAuthURL()
// do other things
// later on get the actualized user object
user.loadMe { (error) in
}

JavaScript

JavaScript
var user = new Apiomat.User();
 
callAuthUrl();
 
user.loadMe({
onOk : function() {
console.log("accessToken: " + user.getAccessToken());
},
onError : function(error) {
}
});

Backbone.js

Backbone.js
var user = apiomat.User();
 
callAuthUrl();
 

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

But you can also set the accessToken from your client by calling the following method on your user object:

Android

Android
user.setAccessToken("<the_token>");

Objective-C

Objective-C
[user setAccessToken:@"<the_token>"];

JavaScript

JavaScript
user.setAccessToken("<the_token>");

Backbone.js

Backbone.js
user.set({"accessToken", "<the_token>"})

Read here (android) or here (iOS) how you get the accessToken from the native Facebook client libraries.
If you set the accessToken by your own you don’t need to set the “Facebook App Secret” in your module config.

If you added the Facebook module to your app setup, you will be able to work with Facebook models just like with ‘normal’ models. The following models will handling the communication with Facebook:

  • FBUser — represent the user in facebook

  • FBPost — represent a post in facebook

  • FBComment — A comment in facebook graph API

  • FBLike — represents a like in facebook system

Working with posts

You can get your own posts by loading the list of posts from a FBUser object. In the following example we’re getting our own posts.

Android

Android
final FBUser me = new FBUser( );
// You have to save the model first, because you need a valid href from the server
me.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
// Get the last 25 own posts
me.loadPostsAsync("", new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
List<FBPost> posts = me.getPosts();
//Print out the posts
for ( FBPost fbPost : posts )
{
Log.d("MyActivity", "Message: " + fbPost.getMessage( ) );
}
}
});
}
}
});

Objective-C

Objective-C
AOMFBUser *me = [[AOMFBUser alloc] init];
// You have to save the model first,
// cause you need a valid href from the server
[me saveObjCWithLoadAfterwards:true completion:^(NSError * error) {
// Get the last 25 own posts
[me loadPostsObjC:@"" completion:^(NSArray<AbstractClientDataModel *> * models, NSError * error) {
if(error == FALSE)
{
//Print out the posts
for (AOMFBPost *fbPost in [me posts]) {
NSLog(@"Message: %@", [fbPost message]);
}
}
}];
}];

Swift

Swift
let me = AOMFBUser()
// You have to save the model first,
// cause you need a valid href from the server
me.save { (error) in
// Get the last 25 own posts
me.loadPosts { (models, error) in
if error == nil {
//Print out the posts
for let fbPost in me.posts {
print("Message: \(fbPost.message)")
}
}
}
}

JavaScript

JavaScript
var me = new Apiomat.FBUser( );
var loadPostsCB = {
onOk : function() {
var posts = me.getPosts();
//Print out the posts
for (var i=0;i<posts.length;i++) {
console.log("Message: " + posts[i].getMessage( ) );
}
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
//You have to save the model first, because you need a valid href from the server
var saveCB = {
onOk : function() {
// Get the last 25 own posts
me.loadPosts("", loadPostsCB);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
me.save(saveCB);

Backbone.js

Backbone.js
var loadPostsCB = {
success: function(model, response, options) {
var posts = model.get("posts");
 
//print out the posts
for(var i = 0; i < posts.length; i++) {
console.log("Message: " + posts[i].get("Message"));
}
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
 
var saveUserCB = {
success: function(model, response, options) {
model.loadPosts(loadPostsCB);
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}}
 
var me = apiomat.FBUser();
 
//you have to save the model first, because you need a valid href from server
me.save(saveUserCB);

C#

C#
FBUser me = new FBUser ();
//you have to save the model first in order to get a valid href from server
await me.SaveAsync ();
 
//get the latest 25 own posts
await me.LoadPostsAsync();
IList<FBPost> posts = me.Posts;
 
//print the posts
foreach (FBPost post in posts)
{
Console.WriteLine ("message content: " + post.Message);
}

It’s also very easy and straightforward to send a post to your own wall.

Android

Android
FBPost newPost = new FBPost();
newPost.setMessage("A test post from api o mat");
try {
// Send to your own wall
newPost.save( );
} catch (Exception e) {
System.err.println("Error occured: " + e.getMessage());
}

Objective-C

Objective-C
AOMFBPost *newPost = [[AOMFBPost alloc] init];
[newPost setMessage:@"A test post from ApiOmat"];
//Send to your own wall
[newPost saveObjCWithLoadAfterwards:true completion:^(NSError * error) {
if(error)
{
NSLog(@"Error occured: %@", [error userInfo]);
}
}];

Swift

Swift
var newPost = AOMFBPost()
newPost.message = "A test post from ApiOmat"
//Send to your own wall
newPost.save { (error) in
if error != nil {
print("Error occurred: \(error.userInfo)")
}
}

JavaScript

JavaScript
var newPost= new Apiomat.FBPost();
newPost.setMessage("A test post from ApiOmat");
 
// Send to your own wall newPost.save( );
var saveCB = {
onOk : function() {
console.log("saved");
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
newPost.save(saveCB);

Backbone.js

Backbone.js
var savePostCB = {
success: function(model, response, options) {
console.log("post successfully saved");
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
 
var newPost = apiomat.FBPost();
newPost.set({"Message", "a test message from apiomat"});
newPost.save(savePostCB);

C#

C#
FBPost post = new FBPost();
post.Message = "A test post from ApiOmat";
 
//send to our own wall
await post.SaveAsync ();

Note: Cause Facebook changes their Graph API it isn’t longer possible to pin a message to the wall of a friend!

Delete a current post:

Android

Android
//...get posts (see above)
//Delete object of type FBPost
fbPost.deleteAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
// ...
}
}
});

Objective-C

Objective-C
//...get posts (see aboove)
//Delete object of type FBPost
[fbPost deleteObjCWithCompletion:^(NSError * error) {
 
}];

Swift

Swift
//...get posts (see aboove)
//Delete object of type FBPost
fbPost.delete { (error) in
 
}

JavaScript

JavaScript
//... get posts have a look to the example above
//delete object of type FBPost
var deleteCB = {
onOk : function() {
console.log("deleted");
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
newPost.deleteModel(deleteCB);

Backbone.js

Backbone.js
var deletePostCB = {
success: function(model, response, options) {
console.log("successfully deleted post");
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
 
newPost.destory(deletePostCB);

C#

C#
//... get posts have a look to the example above
//delete object of type FBPost
await post.DeleteAsync();

You can fetch the comments of a post with the following code snippet:

Android

Android
private void test() {
final FBUser me = new FBUser();
me.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
printCommentsOfPostOfUser(me, 0);
}
}
});
}
 
private void printCommentsOfPostOfUser(final FBUser user, final int postIndex){
// Now download the list of posts
user.loadPostsAsync("", new AOMEmptyCallback() {
List posts = user.getPosts();
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
printCommentsOfPost(posts.get(postIndex));
}
}
});
}
 
private void printCommentsOfPost(final FBPost post) {
// Get the comments for the first post
post.loadCommentsAsync("", new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null){
List<fbcomment> comments = post.getComments();
for (FBComment comment : comments) {
String message = String.format("Comment from %s: %s", comment
.getFrom().get("name"), comment.getMessage()));
Log.d("MyActivity", message );
}
}
}
});
}

Objective-C

Objective-C
AOMFBUser *me = [[AOMFBUser alloc] init];
[me saveObjCWithLoadAfterwards:true completion:^(NSError * error) {
// Now download list of posts
[me loadPostsObjC:@"" completion:^(NSArray<AbstractClientDataModel *> * models, NSError * error) {
if(error == FALSE)
{
// Get the comments for the first post
[[me posts][0] loadCommentsObjC:@"" completion:^(NSArray<AbstractClientDataModel *> * models, NSError * error) {
for (AOMFBComment *comment in [[me posts][0] comments]) {
NsLog(@"Comment from %@: %@", [[comment from] objectForKey:@"name"], [comment message]);
}
}];
}
else
{
NSLog(@"Error occured: %@", [error userInfo]);
}
}];
}];

Swift

Swift
let me = AOMFBUser()
me.save { (error) in
// Now download list of posts
me.loadPosts { (models, error) in
if error == nil {
// Get the comments for the first post
me.posts[0].loadComments { (models, error) in
for let comment in me.posts[0].comments {
print("Comment from \(comment.from["name"]): \(comment.message)")
}
}
} else {
print("Error occurred: \(error.userInfo)")
}
}
}

JavaScript

JavaScript
function test() {
var me= new Apiomat.FBUser();
 
var saveCB = {
onOk : function() {
printCommentsOfPostOfUser(me,0);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
me.save(saveCB);
}
 
function printCommentsOfPostsOfUser(var user, var postIndex) {
var loadPostsCB = {
onOk : function() {
var posts=user.getPosts();
printCommentsOfPost(posts.get(postIndex));
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
user.loadPosts(loadPostsCB);
 
}
 
function printCommentsOfPost(var post) {
var loadCommentsCB = {
onOk : function() {
var comments=posts.getComments();
for (index = 0; index < comments.length; ++index) {
console.log(comments[index]);
}
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
post.loadComments(loadCommentsCB);
 
}

Backbone.js

Backbone.js
function printCommentsOfPost(var post) {
var loadCommentsCB = {
success: function(model, response, options) {
var comments = posts.get("Comments");
for (var i = 0; i comments.length; i++) {
console.log(comments[i]);
}
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
 
post.loadComments(loadCommentsCB);
}
 
function printCommentsOfPostOfUser(var user, var postIndex) {
var loadPostsCB = {
success: function(model, response, options) {
var posts = model.get("Posts");
printCommentsOfPost(posts[postIndex]);
},
error: function (model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
 
user.loadPosts(loadPostsCB);
}
 
function test() {
var me = apiomat.FBUser();
 
var saveUserCB = {
success: function(model, response, options) {
printCommentsOfPostOfUser(model, 0);
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
me.save(saveUserCB);
}

C#

C#
FBUser me = new FBUser ();
await me.SaveAsync ();
 
//load list of posts
me.LoadPostsAsync ();
if (me.Posts.Count > 0)
{
FBPost post = me.Posts [0];
Console.WriteLine ("content of first post: " + post.Message);
 
await post.LoadCommentsAsync()
IList<FBComment> comments = post.Comments;
 
foreach (FBComment comment in comments)
{
Console.WriteLine ("Comment from " + comment.From.Values ["name"] + ": " + comment.Message);
}
}

When you want to add a comment to a post do the following things:

Android

Android
private void test() {
final FBUser me = new FBUser( );
me.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null){
addCommentToPostOfUser(me, 0, "A comment");
}
}
});
}
 
private void addCommentToPostOfUser(final FBUser user, final int postIndex, final String commentText) {
user.loadPostsAsync("", new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null){
FBPost post = user.getPosts().get(postIndex);
addCommentToPost(post, commentText);
}
}
});
}
 
private void addCommentToPost(final FBPost post, final String commentText){
final FBComment comment = new FBComment();
comment.setPostId(post.getForeignId());
comment.setMessage(commentText);
comment.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null){
linkCommentToPost(post, comment);
}
}
});
}
 
private void linkCommentToPost(final FBPost post, final FBComment comment) {
post.postCommentsAsync(comment, new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null)
{
// ...
}
}
});
}

Objective-C

Objective-C
AOMFBUser *me = [[AOMFBUser alloc] init];
[me saveObjCWithLoadAfterwards:true completion:^(NSError * error) {
// download list of posts
[me loadPostsObjC:@"" completion:^(NSArray<AbstractClientDataModel *> * models, NSError * error) {
if(error == FALSE)
{
// Create a new comment
AOMFBComment *comment = [[AOMFBComment alloc] init];
 
// Set the post id
AOMFBPost *postWithComment = [me posts][0];
[comment setPostId:[postWithComment getForeignId]];
// add a text
[comment setMessage:@"A comment"];
[comment saveObjCWithLoadAfterwards:true completion:^(NSError * error) {
// Also link together
[postWithComment postCommentsObjC:comment completion:^(NSString * models, NSError * error) {
if(error)
{
NSLog(@ "Error occured: %@", [error userInfo]);
}
}];
}];
}
}];
}];

Swift

Swift
let me = AOMFBUser()
me.save { (error) in
// download list of posts
me.loadPosts { (models, error) in
if error == nil {
// Create a new comment
var comment = AOMFBComment()
 
// Set the post id
let postWithComment = me.posts[0]
comment.postId = postWithComment.foreignId
// add a text
comment.message = "A comment"
comment.save { (error) in
// Also link together
postWithComment.postComments(comment) { (href, error) in
if error != nil {
print("Error occurred: \(error.userInfo)")
}
}
}
}
}
}

JavaScript

JavaScript
function test() {
var me= new Apiomat.FBUser();
 
var saveCB = {
onOk : function() {
addCommentToPostOfUser(me, 0, "A comment");
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
me.save(saveCB);
}
 
function addCommentToPostOfUser(var user, var postIndex, var commentText) {
var loadPostsCB = {
onOk : function() {
var post = user.getPosts().get(postIndex);
addCommentToPost(post, commentText);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
user.loadPosts(loadPostsCB);
}
 
function addCommentToPost (var post, var commentText) {
var comment= new Apiomat.FBComment();
comment.setPostId(post.getForeignId());
comment.setMessage(commentText);
 
var commentSaveCB = {
onOk : function() {
linkCommentToPost(post, comment);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
 
 
comment.save(commentSaveCB);
}
 
function linkCommentToPost(var post, var comment) {
var postCommentsCB = {
onOk : function() {
console.log("saved");
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
post.postComments(postCommentsCB);
}

Backbone.js

Backbone.js
function linkCommentToPost(comment, post) {
var postCommentToPostCB = {
success: function(model, reponse, options) {
console.log("successfully posted comment to post");
},
error: function(model, resposne, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
 
post.postComment(comment, postCommentToPostCB);
}
 
function addCommentToPost(var post, var commentMessage) {
var comment = apiomat.FBComment();
comment.set({"PostId", post.get("foreignId")});
comment.set({"Message", commentMessage});
 
saveCommentCB = {
success: function(model, response, options) {
linkCommentToPost(comment, post);
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
 
comment.save(saveCommentCB);
}
 
function addCommentToPostOfUser(var user, var postIndex, var commentMessage) {
var loadPostCB = {
success: function(model, reponse, options) {
var post = model.get("Posts")[postIndex];
addCommentToPost(post, commentMessage);
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.statusCode);
}
}
 
user.loadPosts(loadPostCB);
}
 
function test() {
var me = apiomat.FBUser();
 
var saveUserCB = {
success: function(model, response, options) {
addCommentToPostOfUser(model, 0, "A comment");
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
 
me.save(saveUserCB);
}

C#

C#
FBUser me = new FBUser ();
await me.SaveAsync ();
 
//load list of posts
me.LoadPostsAsync ();
 
//create new comment
FBComment comment = new FBComment();
 
FBPost postWithComment = me.Posts[0];
//set the post id
comment.PostId = postWithComment.Id;
 
//add a message for your comment
comment.Message = "my comment";
 
//now save the comment
await comment.SaveAsync();
 
//also link together
await postWithComment.PostCommentsAsync(comment);

Certainly it’s possible to add a comment to your friend's posts.

unwanted comments can be easily deleted:

Android

Android
//do stuff like above
//If you have the comment downloaded
//than you can delete it
comment.delete();

Objective-C

Objective-C
//do stuff like above
//If you have the comment downloaded
//than you can delete it
[comment deleteObjCWithCompletion:^(NSError * error) {
 
}];

Swift

Swift
//do stuff like above
//If you have the comment downloaded
//than you can delete it
comment.delete { (error) in
 
}

JavaScript

JavaScript
//do stuff like above
//If you have the comment downloaded
//than you can delete it
var deleteCB = {
onOk : function() {
console.log("deleted");
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
comment.deleteModel(deleteCB);

Backbone.js

Backbone.js
//do stuff like above
//If you have the comment downloaded
//than you can delete it
 
var destroyCommentCB = {
success: function(model, reponse, options) {
console.log("successfully deleted comment");
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
 
comment.destroy(destroyCommentCB);

C#

C#
//do stuff like above
//If you have the comment downloaded
//than you can delete it
await comment.DeleteAsync();

Accessing the likes of a post:

Android

Android
private void test() {
final FBUser me = new FBUser();
me.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
printLikesOfPost(me, 0);
}
}
});
}
 
private void printLikesOfPost(final FBUser user, final int postIndex) {
user.loadPostsAsync("", new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
FBPost post = user.getPosts().get(postIndex);
printLikesOfPost(user, post);
}
}
});
}
 
private void printLikesOfPost(final FBUser user, final FBPost post) {
post.loadLikesAsync("", new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
// Load likes of first post
List<fblike> likes = post.getLikes();
Log.d("MyActivity", "Count likes: " + likes.size());
// Print out the name of the persons which liked the post
for (FBLike like : likes) {
Log.d("MyActivity", like.getName());
}
}
}
});
}

Objective-C

Objective-C
AOMFBUser *me = [[AOMFBUser alloc] init];
[me saveObjCWithLoadAfterwards:true completion:^(NSError * error) {
// download list of posts
[me loadPostsObjC:@"" completion:^(NSArray<AbstractClientDataModel *> * models, NSError * error) {
if(error == FALSE)
{
// Load likes of first post
[[me posts][0] loadLikesObjC:@"" completion:^(NSArray<AbstractClientDataModel *> * models, NSError * error) {
if(error == FALSE)
{
NSLog(@"Count likes: %i", [[[me posts][0] likes] count] );
// Print out the name of the persons which liked the post
for (AOMFBLike *like in [[me posts][0] likes]) {
NSLog(@"%@", [like name]);
}
}
else
{
NSLog(@ "Error occured: %@", [error userInfo]);
}
}];
}
}];
}];

Swift

Swift
let me = AOMFBUser()
me.save { (error) in
// download list of posts
me.loadPosts { (models, error) in
if error == nil {
// Load likes of first post
me.posts[0].loadLikes { (models, error) in
if error == nil {
print("Count likes: \(me.posts[0].likes.count.description)")
// Print out the name of the persons which liked the post
for let like in me.posts[0].likes) {
print("\(like.name)")
}
} else {
print("Error occurred: \(error.userInfo)")
}
}
}
}
}

JavaScript

JavaScript
function test() {
var me= new Apiomat.FBUser();
var saveCB = {
onOk : function() {
printLikesOfPost(me, 0);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
me.save(saveCB);
}
 
function printLikesOfPost(var user, var postIndex) {
var loadPostsCB = {
onOk : function() {
var post = user.getPosts().get(postIndex);
printLikesOfPost(user, post);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
user.loadPosts(loadPostsCB);
}
 
function printLikesOfPost(var user, var post) {
var loadLikesCB = {
onOk : function() {
// Load likes of first post
var likes = post.getLikes();
console.log("Count likes: " + likes.length);
for (index = 0; index < comments.length; ++index) {
console.log(comments[index]);
}
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
post.loadLikes(loadLikesCB);
}

Backbone.js

Backbone.js
function printLikesOfPost(var post) {
var loadLikesCB = {
success: function(model, response, options) {
var likes = post.get("Likes");
for (var i = 0; i likes.length; i++) {
console.log(likes[i]);
}
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.statusCode);
}
}
 
post.loadLikes(loadLikesCB);
}
 
function printLikesOfPostOfUser(var user, var postIndex) {
var loadPostsCB = {
success: function(model, response, options) {
var posts = model.get("Posts");
printLikesOfPost(posts[postIndex]);
},
error: function (model, reponse, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.statusCode);
}
}
 
user.loadPosts(loadPostsCB);
}
 
function test() {
var me = apiomat.FBUser();
 
var saveUserCB = {
success: function(model, response, options) {
printLikesOfPostOfUser(model, 0);
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
 
me.save(saveUserCB);
}

C#

C#
FBUser me = new FBUser ();
await me.SaveAsync ();
 
me.LoadPostsAsync ();
if (me.Posts.Count > 0)
{
FBPost post = me.Posts [0];
Console.WriteLine ("content of first post: " + post.Message);
 
await post.LoadLikesAsync();
IList<FBLike> likes = post.Likes;
 
foreach (FBLike like in likes)
{
Console.WriteLine ("Like from: " + like.Name);
}
}

As of Nov 17, 2016 Facebook changed the behavior of publishing likes, this action is only possible with Page Access Tokens. As the SDKs are using a User Access Token, it is not possible to publish likes using any SDK. For documentation purposes, you can see the former code snippet below:

Android

Android
private void test() {
final FBUser me = new FBUser();
me.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
likePost(me, 0);
}
}
});
}
 
private void likePost(final FBUser user, final int postIndex) {
user.loadPostsAsync("", new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
FBPost post = user.getPosts().get(postIndex);
likePost(post);
}
}
});
}
 
private void likePost(final FBPost post) {
// Create a new like
final FBLike like = new FBLike();
// persist the like object first
like.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
likePost(post, like);
}
}
});
}
 
private void likePost(final FBPost post, final FBLike like) {
post.postLikesAsync(like, new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null)
{
// ...
}
}
});
}

Objective-C

Objective-C
AOMFBLike *likeIt = [[AOMFBLike alloc] init];
AOMFBUser *myUser = [[AOMFBUser alloc] init];
[myUser saveAsyncWithBlock:^(NSError *error) {
// persist the like object first
[likeIt saveAsyncWithBlock:^(NSError *error) {
// download list of posts
[myUser loadPostsAsync:@"" andWithBlock:^(NSError *error) {
if(error == FALSE)
{
NSMutableArray *posts = [me posts];
[posts[1] postLikesAsync:likeIt andWithBlock:^(NSError *error) {
 
}];
}
else
{
NSLog(@ "Error occured: %@", [error userInfo]);
}
}];
 
}];
}];

JavaScript

JavaScript
function test() {
var me= new Apiomat.FBUser();
var saveCB = {
onOk : function() {
likePost(me, 0);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
me.save(saveCB);
}
 
function likePostLoad(var user, var postIndex) {
var loadPostsCB = {
onOk : function() {
var post = user.getPosts().get(postIndex);
likePostSave(post);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
user.loadPosts(loadPostsCB);
}
 
function likePostSave(var post) {
var like= new Apiomat.FBLike();
var saveCB = {
onOk : function() {
var post = user.getPosts().get(postIndex);
likePost(post);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
like.save(saveCB);
}
 
function likePost( var post, var like) {
var postLikesCB = {
onOk : function() {
//console.log();
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
post.postLikes(postLikesCB);
}

Backbone.js

Backbone.js
function linkCommentToPost(like, post) {
var postLikeToPostCB = {
success: function(model, response, options) {
console.log("successfully posted like to post");
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
 
post.postLike(like, postLikeToPostCB);
}
}
 
function addLikeToPost(var post) {
var like = apiomat.FBLike();
saveLikeCB = {
success: function(model, response, options) {
linkLikeToPost(like, post);
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
 
comment.save(saveLikeCB);
}
 
function addLikeToPostOfUser(var user, var postIndex) {
var loadPostCB = {
success: function(model, response, options) {
var post = model.get("Posts")[postIndex];
addLikeToPost(post);
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.statusCode);
}
}
 
user.loadPosts(loadPostCB);
}
 
function test() {
var me = apiomat.FBUser();
 
var saveUserCB = {
success: function(model, response, options) {
addLikeToPostOfUser(model, 0);
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.statusCode);
}
 
me.save(saveUserCB);
}

C#

C#
FBUser me = new FBUser ();
await me.SaveAsync ();
 
//load list of posts
me.LoadPostsAsync ();
 
//create new like
FBLike like = new FBLike();
 
FBPost postWithComment = me.Posts[0];
 
//now save the like
await like.SaveAsync();
 
//also link together
await postWithComment.PostLikesAsync(like);

Deleting your own likes is easy:

Android

Android
private void test() {
final FBUser me = new FBUser();
me.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
deleteLike(me, 0, 0);
}
}
});
}
 
private void deleteLike(final FBUser user, final int postIndex, final int likeIndex) {
user.loadPostsAsync("", new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
FBPost post = user.getPosts().get(postIndex);
deleteLike(post, likeIndex);
}
}
});
}
 
private void deleteLike(final FBPost post, final int likeIndex) {
post.loadLikesAsync("", new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
FBLike like = post.getLikes().get(likeIndex);
deleteLike(post, like);
}
}
});
}
 
private void deleteLike( final FBPost post, final FBLike like) {
post.removeLikesAsync(like, new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
// ...
}
}
});
}

Objective-C

Objective-C
AOMFBUser *myUser = [[AOMFBUser alloc] init];
[myUser saveObjCWithLoadAfterwards:true completion:^(NSError * error) {
// download list of posts
[myUser loadPostsObjC:@"" completion:^(NSArray<AbstractClientDataModel *> * models, NSError * error) {
if(error == FALSE)
{
NSMutableArray *posts = [me posts];
AOMFBPost *post = posts[1];
deleteLike(post);
}
}];
}];
 
void deleteLike(AOMFBPost *post)
{
// get likes of post
[post loadLikesObjC:@"" completion:^(NSArray<AbstractClientDataModel *> * models, NSError * error) {
// You can only delete your own like
[post removeLikesObjC:[post likes][0] completion:^(NSError * error) {
if(error)
{
NSLog(@ "Error occured: %@", [error userInfo]);
}
}];
}];
}

Swift

Swift
let myUser = AOMFBUser()
myUser.save { (error) in
// download list of posts
myUser.loadPosts { (models, error) in
if error == nil {
let posts = me.posts
if let post = posts[1] {
deleteLike(post: post)
}
}
}
}
 
func deleteLike(post: AOMFBPost) {
// get likes of post
post.loadLikes { (models, error) in
// You can only delete your own like
post.removeLikes(post.likes][0]) { (error) in
if error != nil {
print("Error occurred: \(error.userInfo)")
}
}
}
}

JavaScript

JavaScript
function test() {
var me= new Apiomat.FBUser();
var saveCB = {
onOk : function() {
deleteLike(me, 0, 0);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
me.save(saveCB);
 
}
 
function deleteLike(var user, var postIndex, var likeIndex) {
var loadPostsCB = {
onOk : function() {
var post = user.getPosts().get(postIndex);
deleteLikePosts(post, likeIndex);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
user.loadPosts(loadPostsCB);
 
}
 
function deleteLikePosts(var post, var likeIndex) {
var loadLikesCB = {
onOk : function() {
var like = post.getLikes().get(likeIndex);
deleteLikePost(post, like);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
post.loadLikes(loadLikesCB);
}
 
function deleteLikePost(var post,var like) {
var removeLikesCB = {
onOk : function() {
//console.log();
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
post.removeLikes(removeLikesCB);
 
}

Backbone.js

Backbone.js
function deleteLike(post, like) {
var removeLikeFromPostCB = {
success: function(model, response, options) {
console.log("successfully removed like from post");
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
 
post.removeLike(like, removeLikeFromPostCB);
}
 
function deleteLikeFromPost(var post, var likeIndex) {
loadLikesCB = {
success: function(model, response, options) {
deleteLike(post, post.get("Likes")[likeIndex]);
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
post.loadLikes(loadLikesCB);
}
 
function deleteLike(var user, var postIndex, var likeIndex) {
var loadPostCB = {
success: function(model, reponse, options) {
var post = user.get("Posts")[postIndex];
deleteLikeFromPost(post, likeIndex);
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
 
user.loadPosts(loadPostCB);
}
 
function test() {
var me = apiomat.FBUser();
 
var saveUserCB = {
success: function(model, response, options) {
deleteLike(model, 0, 0);
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
 
me.save(saveUserCB);
}

C#

C#
FBUser me = new FBUser ();
await me.SaveAsync ();
 
//load list of posts
me.LoadPostsAsync ();
 
FBPost post = me.Posts[0];
if(post != null)
{
await post.LoadLikesAsync ();
FBLike likeToDelete = post.Likes [0];
 
if (likeToDelete != null)
{
await post.RemoveLikesAsync (likeToDelete);
}
}

Get information about you

Here is an example how you get information about yourself. Don’t forget to set the facebook scope (about_me) in the app setup on dashboard (see also information about App Setup)

Android

Android
final FBUser me = new FBUser();
me.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
//Now you have access to your information like the name
Log.d("MyActivity", "Name: " + me.getName( ));
}
}
});

Objective-C

Objective-C
AOMFBUser *myUser = [[AOMFBUser alloc] init];
// You have to save the model first
[myUser saveObjCWithLoadAfterwards:true completion:^(NSError * error) {
if(error == FALSE)
{
//Now you have access to your information
//e.g. the name
NSLog(@"Name: %@", [myUser name]);
}
}];

Swift

Swift
let myUser = AOMFBUser()
// You have to save the model first
myUser.save { (error) in
if error == nil {
//Now you have access to your information
//e.g. the name
print("Name: \(myUser.name)")
}
}

JavaScript

JavaScript
var me= new Apiomat.FBUser();
var saveCB = {
onOk : function() {
console.log(me.getName());
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
me.save(saveCB);

Backbone.js

Backbone.js
var me = apiomat.FBUser();
 
var saveUserCB = {
success: function(model, response, options) {
console.log(model.get("Name"));
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
 
me.save(saveUserCB);

C#

C#
FBUser me = new FBUser ();
await me.SaveAsync ();
 
//Now you have access to your information like the name
Console.WriteLine("Name: " + me.Name);

Fetch information about friends and posts of friends

You can get information about your friends by loading the friend list on your FBUser. Don’t forget to set the permission ‘friends_about_me’ in the dashboard (see also information about App Setup).

If the friend has authorized the same app on Facebook, you can load the posts from them. These posts can then be loaded using "loadPosts".

Android

Android
private void test() {
final FBUser me = new FBUser();
me.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
printFriendInfo(me);
}
}
});
}
 
private void printFriendInfo(final FBUser user) {
user.loadFriendsAsync("", new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
List<fbuser> friends = me.getFriends();
for (FBUser friend : friends) {
Log.d("MyActivity", "Facebook ID: " + friend.getForeignId( ) );
Log.d("MyActivity", "Name of friend: " + friend.getName( ) );
}
}
}
});
}

Objective-C

Objective-C
AOMFBUser *me = [[AOMFBUser alloc] init];
// You have to save the model first
[me saveObjCWithLoadAfterwards:true completion:^(NSError * error) {
// Now download the list of friends
[me loadFriendsObjC:@"" completion:^(NSArray<AbstractClientDataModel *> * models, NSError * error) {
if(error == FALSE)
{
NSMutableArray *friends = [me friends];
for (AOMFBUser *friend in friends) {
NSLog(@"Facebook ID: %@", [friend getForeignId]);
NSLog(@"Name of friend: %@", [friend name]);
}
}
else
{
NSLog(@ "Error occured: %@", [error userInfo]);
}
}];
}];

Swift

Swift
let me = AOMFBUser()
// You have to save the model first
me.save { (error) in
// Now download the list of friends
me.loadFriends { (models, error) in
if error == nil {
if let friends = me.friends {
for let friend in friends {
print("Facebook ID: \(friend.foreignId)")
print("Name of friend: \(friend.name)")
}
}
} else {
print("Error occurred: \(error.userInfo)")
}
}
}

JavaScript

JavaScript
function test() {
var me= new Apiomat.FBUser();
var saveCB = {
onOk : function() {
printFriendInfo(me);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
me.save(saveCB);
}
 
function printFriendInfo(var user) {
var loadFriendsCB = {
onOk : function() {
var friends = user.getFriends();
for (index = 0; index < friends.length; ++index) {
console.log(friends[index]);
}
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
me.loadFriends(loadFriendsCB);
}

Backbone.js

Backbone.js
function printFriendInfo(var user) {
var loadFriendsCB = {
success: function(model, response, options) {
var friends = user.get("Friends");
for(var i = 0; i < friends.length; i++) {
console.log(friends[i]);
}
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
user.loadFriends(loadFriendsCB);
}
 
function test() {
var me = apiomat.FBUser();
 
var saveUserCB = {
success: function(model, response, options) {
printFriendInfo(model);
},
error: function(model, response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
 
me.save(saveUserCB);
}

C#

C#
FBUser me = new FBUser ();
await me.SaveAsync ();
 
await me.LoadFriendsAsync ();
IList<FBUser> friends = me.Friends;
 
foreach (FBUser friend in friends)
{
Console.WriteLine ("Facebook ID: " + friend.ForeignId);
Console.WriteLine ("Facebook Name: " + friend.Name);
}

User Signup and Login with Facebook

When you read about how our SDKs work, you know that you need a user to configure the Datastore, so that saving and loading objects work (because the related requests need to be authenticated with the user credentials). The same works for our REST API.

But when you want your users to be able to login with their existing Facebook credentials, you don’t want to ask for separate credentials to create an object of the ApiOmat user class or a subclass. That’s what our Facebook login mechanism is for. You supply the Facebook token of your user (obtained from the official Facebook SDK) and receive a tuple that contains an ApiOmat User object and a token map with the tokens you need to authenticate:

Android

Android
private void fbLogin(final String fbToken) {
FBUser.loginFacebookUserAsync(fbToken, new AOMCallback() {
@Override
public void isDone(UserTokenContainerTuple userTokenContainerTuple, boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null)
{
User user = userTokenContainerTuple.getUser();
String sessionToken = userTokenContainerTuple.getTokenContainer().getSessionToken();
user.setSessionToken(sessionToken);
Datastore.configureWithSessionToken(user);
}
}
});
}

Objective-C

Objective-C
 - (void) fbLogin: (NSString*) fbToken
{
AOMFBUser* me = [AOMFBUser new];
[me loginFacebookUserWithToken:@"" completion:^(UserTokenContainer * data, NSError * error) {
if(error == nil) {
[DataStore configureWithSessionToken:data.tokenContainer.sessionToken configuration:nil];
} else {
//handle error
}
}];
}

JavaScript

JavaScript
function fbLogin(fbToken) {
Apiomat.FBUser.loginFacebookUser(fbToken, {
onOk: function(userTokenMap) {
var user = userTokenMap.user;
var tokenMap = userTokenMap.tokenMap;
 
user.setSessionToken(tokenMap.sessionToken);
Apiomat.Datastore.configureWithUserSessionToken(user);
},
onError: function(error) {
console.log("Some error occured. " + error.statusCode + " --> " + error.message);
}
)
}

Backbone.js

Backbone.js
function fBLogin(var fbToken) {
var fBUser = apiomat.FBUser();
var fbLoginCB = {
success: function(success, response) {
if(success) {
var tokenMap = response.tokenMap;
apiomat.authenticationService().configureWithUserSessionToken(tokenMap.access_token);
}
},
error: function(model response, options) {
console.log("some error occured: (" + options.error.statusCode + ") " + options.error.message);
}
}
fbUser.loginFacebookUser(fbToken, fbLoginCB);
}

Swift

Swift
func fbLogin(fbToken: String) {
let comp: CompletionHandlerWithUserTokenContainer = { data, error in
if(error == nil) {
DataStore.configureWithSessionToken(data.tokenContainer.sessionToken!)
//now you can go on
} else {
//handle error
}
}
 
let fbUser = AOMBUser();
fbuser.loginFacebookUser(fbToken, completion: comp)
}

C#

C#
private void FbLoginAsync(string fbToken)
{
Tuple<User, IDictionary> userTokenMapTuple = await FBUser.GetOrCreateUserAsync(fbToken).ConfigureAwait(false);
User user = userTokenMapTuple.Item1;
IDictionary tokenMap = userTokenMapTuple.Item2;
user.SessionToken = tokenMap["SessionToken"];
Datastore.ConfigureWithSessionToken(user);
}

The example code already configures the Datastore with the received session token. So from then on normal requests to the backend can be made, like myObj.saveAsync(); in Java.

This mechanism also works if your user signed up the conventional way, and now wants to login on Facebook. When the user object that already exists in the backend already has a Facebook ID attached and it’s the same as the one of the Facebook User with whose credentials your user just logs in, you also get the same return type with a user and a tokenMap. The existing user object also gets assigned additional data if the attributes don’t have any values yet (first name, last name, date of birth).