livecode.com
  • Home
  • Download
  • Developers
  • Academy
  • Community
  • Store
  • Support
  • About
LiveCode Lessons » How To - LiveCode Mobile Tasks » Accessing Facebook Api's using LiveCode

Topics

  • Getting Started with Android 3
    • How do I Become an Android Developer on a PC?
    • How do I Become an Android Developer on a Mac?
    • The Basics: How do I Create Hello World on Android?
  • Getting Started with iOS 2
    • How do I Become an iOS Developer?
    • How do I use Core Location in revMobile
  • LiveCode Mobile Tasks 22
    • How do I Develop Cross-Platform in LiveCode?
    • How do I send an email from my mobile device?
    • How do I use multi-touch to move more than one object?
    • How do I use the Question and Password Dialogues in LiveCode Mobile?
    • How do I Capture Images in LiveCode Mobile?
    • How do I detect a shaking motion using LiveCode mobile?
    • LiveCode Mobile Video How-To's - Shake
    • How do I implement a multi-touch pinch motion?
    • LiveCode Mobile Video How-To's - Multi-touch
    • How do I play a video in part of the screen in iOS
    • Displaying Assets On Differing Screen Resolutions
    • How to create and use an SQLite database
    • How do I get an image from my mobile photo library
    • How do I implement in-app purchases in LiveCode?
    • Accessing Facebook Api's using LiveCode
    • Mobile Orientations
    • Using local notifications
    • Using custom URL schemes
    • Using multi-channel audio on mobile
    • How do I get the Location and use the Digital Compass?
    • How do I use Ads in LiveCode?
    • Creating a native scroller to scroll a field
  • iOS Tasks 19
    • How do I build an iPhone application for iOS?
    • How do I Configure the Status Bar in iOS?
    • How do I make a phone call on the iPhone?
    • How do I use Native Text Controls on iOS
    • How do I use the Browser Control?
    • How do I read/write to files in iOS?
    • How do I Send HTML E-Mails with Attachments in iOS?
    • Installing custom fonts on iOS
    • How do I use the Question and Password Dialogues in iOS?
    • How do I play sounds on an iOS device?
    • How do I use the Picker View on an iPhone?
    • How do I Access Maps on iOS?
    • Creating a simple stock control application for the iPad
    • How do I Create a Distribution Profile for iOS?
    • How do I Submit an iOS App to the App Store?
    • How do I set up an App for Submission to iTunes Connect?
    • How do I use the Date Picker View on an iPhone?
    • How do I use Push Notifications with iOS?
    • How do I Develop iOS Code for Standard and Retina Devices?
  • Android Tasks 4
    • Using the hardware "back" button on Android
    • How do I Create an Android App for Distribution?
    • How do I Create a Self-Signed Certificate for an Android App?
    • How do I use Push Notifications with Android?

Last Updated

Nov 11, 2011

Download Lesson PDF

Other Resources

  • Getting Started with LiveCode

  • Get Up and Running with LiveCode
  • Getting Started with LiveCode Development
  • LiveCode Concepts

  • Features, concepts and aspects of LiveCode
  • LiveCode Lessons

  • How To - Step-By-Step Guides To Tasks In LiveCode
  • How To - LiveCode Server Tasks
  • How To - LiveCode Mobile Tasks
  • How To - LiveCode Sample Scripts
  • How to - LiveCode Marketplace Products
  • How to Purchase and License LiveCode
  • Tutorials

  • Creating a Video Library Application
  • Data Grid

  • LiveCode Data Grid
  • Data Grid Tips & Tricks
  • Converting the Stock Program

Comments

0 for this lesson

  • Prev: How do I implement in-app purchases in LiveCode?
  • Next: Mobile Orientations

Accessing Facebook Api's using LiveCode

This lesson shows you how to connect to Facebook and access your friends details using LiveCode. We have used Andre Garzia's LiveCode Facebook plugin for accessing the Api's. In this lesson we have developed an example which allows you to post messages to your wall, add photos to your album, get your friends profile picture, show a list of your Facebook friends and get birthdays for today. You can download the associated sample stack here.

Step 1: Create a new App in Facebook

Zoom

Please go to https://developers.facebook.com/apps and click Create New App. Only when we create a new app we will be able to get the App ID and App Secret Key. These are the main variables used to establish a connection with Facebook from LiveCode. Select Website, enter your test url and save the App. Now we have created an App called LiveCodeTest.

Step 2: Use Graph Api to get details from Facebook

Zoom

Goto https://developers.facebook.com/tools/explorer/.

In order to get details from Facebook you should enable permissions. Here you can use the Graph Api explorer to give the correct permissions and test them. To give the correct permissions, first you have to select your Application from the drop down list and then click on the "Get Access Token". This will pop up a list of permissions. Select the necessary permissions depending on your App. In our case we have selected

User_permissions:
(*) user_about_me
(*) user_photos

friends_permissions:
(*) friends_about_me
(*) friends_photos
(*) friends_birthdays

Then click the "Get Access Token" button, this will create the necessary permisions for your App. To check whether the permissions are working you can add this path in the Get field:

/friends?fields=id,name,picture,birthday

This will generate a json which will have your friends id,name,profile picture and birthday. All we have to do now is present the json through LiveCode.

Note: You will get details of people only if they have shared them with you.

Step 3: Creating the LiveCode stack - Design

This is the basic layout of the stack.

Step 4: Starting the plugins

Now is the time to use Andre's plugins. In the attached example stack you will be able to find two stacks "lib.aag.json" and "lib.aag.facebook". Open both the stacks and check the option "start using this stack".

Step 5: Authorization Button

Zoom

This is where we create the link between the Facebook Api and LiveCode.

Before doing the code for Authorize button, add the libraries to the stack:

on preOpenCard
start using "lib.aag.json"
start using "lib.aag.facebook"
end preOpenCard

 

Authorize button code:

local sRefreshCode, sAuthToken

on mouseUp
-- Use you Facebook App ID and Secret key which you got from creating a App in Facebook.
fb.set "AppID", "123456"
fb.set "AppSecret", "Secret Key"

-- call the request authorization method
-- here we request Authorization for writing to the wall, the rest of the authorizations we have set in Step 2

if pRefreshCode is empty or sAuthToken is empty then
fb.requestAuthorization "publish_stream"
else
answer "you are already authorized"
end if

end mouseUp

on facebookerror pReason
-- this is the callback if there is an error.
answer error pReason
end facebookerror

on facebookcallback pRefreshCode, pAuthToken
-- this is the callback if Authorization is successful.
put pRefreshCode into sRefreshCode
put pAuthToken into sAuthToken
fb.closebrowser
answer info "You are authorized"

end facebookcallback

Step 6: Post on Wall

Zoom

on mouseUp
put text of field "FBPost" into tData["message"]
--call This will post the message to your wall
put fb.post("/me/feed",tData) into tR

--This function will convert the result json to Array
put jsontoarray(tR) into tA

if tA["id"] is not empty then
answer "post succeded. ID:" && tA["id"]
else
answer "post failed." && tA["error"]["message"]
breakpoint
end if
end mouseUp

Step 7: Get your Profile Picture

Zoom

on mouseUp
--This will fetch your profile picture
put fb.get("/me/picture?type=large") into image "pic"
end mouseUp

 

on facebookerror pReason
answer error "facebook error" && pReason
put fb.data("last url") &cr&fb.data("last error")
end facebookerror

Step 8: Upload Photo to your App Album

Zoom

on mouseUp
answer file "Where is the photo?"
if it is not empty then
put ("<file>" & it) into tPhotoData
else
exit to top
end if

ask "Enter caption for photo"
if it is not empty then
put it into tPhotoCaption
else
exit to top
end if

put empty into tPostData
--This will store the caption and the photo
get libURLMultipartFormData(tPostData, "message", tPhotoCaption, "source", tPhotoData)

--This will post the photo to facebook
put fb.post("/me/photos", tPostData, true) into tR
put tR
end mouseUp

 

on facebookerror pReason
answer error pReason
put fb.data("last error")
end facebookerror

Step 9: Show Friends List

Zoom

on mouseUp
## This will fetch the json.
put fb.get("/me/friends?fields=id,name,picture") into tR
put jsonToArray(tR) into tA

## This will insert friends to the DataGrid

repeat for each key element in tA["data"]
add 1 to tcount
put tA["data"][tcount]["name"] into theDataA[tcount]["Name"]
end repeat


lock screen
set the dgData of group "DataGrid 1" to theDataA

## Hilite first row
set the dgHilitedLines of group "DataGrid 1" to 1
unlock screen

end mouseUp

Note: You will have to modify the DataGrid Row template accordingly. Please see this lesson on creating list of people using Datagrid.

http://lessons.runrev.com/s/lessons/m/datagrid/l/7305-Example-Creating-a-List-of-People

Step 10: Show Birthday's Today

Zoom

on mouseUp

put the date into tDate
set the itemDel to "/"
if the number of chars in item 1 of tDate is 1 then
put 0 before item 1 of tDate
end if
if the number of chars in item 2 of tDate is 1 then
put 0 before item 2 of tDate
end if
put "20" before item 3 of tDate
put tDate into tShort
delete char 6 to 11 of tShort

## This will fetch the json.

put fb.get("/me/friends?fields=id,name,birthday") into tR
put jsonToArray(tR) into tA

repeat for each key element in tA["data"]
add 1 to tcount
put tA["data"][tcount]["birthday"] into tBirth
replace "\/" with "/" in tBirth
put tBirth into tBirth1

set the itemDel to " "
if the number of chars in item 1 of tBirth1 is 10 then
delete char 6 to 11 of tBirth1
end if

## This will check whether birthday is available to us or not.
if tA["data"][tcount]["birthday"] is not empty then
if tBirth1 = tShort then
put tA["data"][tcount]["name"] & " " & tBirth into theDataA[tcount]["Name"]
end if
end if

end repeat

set the dgData of group "DataGrid 2" to theDataA

## Hilite first row
set the dgHilitedLines of group "DataGrid 2" to 1

end mouseUp

 

Note: You will have to modify the DataGrid Row template accordingly. Please see this lesson on creating list of people using Datagrid.

http://lessons.runrev.com/s/lessons/m/datagrid/l/7305-Example-Creating-a-List-of-People

Step 11: Fetch results

As you can see i am able to list my friends, get my profile picture and get current birthday details.

Aditional Info

Zoom

For more on Facebook Graphs, please see this link

http://developers.facebook.com/docs/reference/api/

With the above example and the Graph Api you can bring in more details from Facebook. This example is just to get you started with the basics of using the Facebook Api with LiveCode.

We'd like to express our sincere thanks to Andre Garzia for his great plugin!

  • Prev: How do I implement in-app purchases in LiveCode?
  • Next: Mobile Orientations

Comments (0)

Add your comment

E-Mail me when someone replies to this comment
Brought to you by RunRev Ltd, Registered in Scotland, No. SC200728