Index: openacs-4/packages/facebook-api/tcl/facebook-api-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/facebook-api/tcl/facebook-api-procs.tcl,v diff -u -N -r1.2 -r1.3 --- openacs-4/packages/facebook-api/tcl/facebook-api-procs.tcl 16 Dec 2007 14:10:18 -0000 1.2 +++ openacs-4/packages/facebook-api/tcl/facebook-api-procs.tcl 3 Nov 2009 06:22:06 -0000 1.3 @@ -48,7 +48,7 @@ ad_proc facebook_api::secret { -package_key } { - Get the Facebook API key given the package_key of + Get the Facebook API secret given the package_key of the openacs package. The openacs package that you will use as a facebook app must have a "secret" parameter defined in acs-admin/apm. @@ -125,7 +125,7 @@ } { @return Login Status } { - ad_returnredirect http://www.facebook.com/login.php?api_key=[api_key -package_key $package_key]&v=1.0, + ad_returnredirect http://www.facebook.com/login.php?api_key=[api_key -package_key $package_key]&v=1.0 } ad_proc facebook_api::format_post_vars { @@ -160,13 +160,33 @@ ad_proc facebook_api::get_session_from_token { -package_key -auth_token - -url + {-url ""} } { - Returns a new session_id from facebook using the given token and redirects the user to the specified url. + Returns a new session_id from facebook using the given token. If a url is specified, this proc will redirect the user to the specified url. } { - facebook_api::do_request -package_key $package_key -method auth.getSession -params [list auth_token $auth_token format json] - facebook_api::redirect $url - ad_script_abort + # fetch session info + set json [facebook_api::do_request -package_key $package_key -method auth.getSession -params [list auth_token $auth_token format json]] + # record session info for the fb user + set session_data [json::json2dict $json] + set session_key [lindex $session_data 1] + set uid [lindex $session_data 3] + set session_expires [lindex $session_data 5] + if { [db_0or1row "check_fb_user" "select uid from fb_users where uid=:uid"] } { + db_dml "update_session_info" "update fb_users set auth_token=:auth_token, session_key=:session_key, session_expires=:session_expires where uid=:uid" + } else { + db_dml "record_session_info" "insert into fb_users (uid,auth_token,session_key,session_expires) values (:uid,:auth_token,:session_key,:session_expires)" + } + # check if the user is logged in to this oacs site + # if yes, then create a map between the user_id and the fb_uid + set user_id [ad_conn user_id] + if { $user_id != 0 && ![db_0or1row "checkmap" "select uid from oacs_fb_user_map where user_id=:user_id"]} { + db_dml "map_fb_uid" "insert into oacs_fb_user_map (user_id,uid) values (:user_id,:uid)" + } + if { [exists_and_not_null url] } { + facebook_api::redirect $url + } else { + return [list $session_key $uid $session_expires] + } } ad_proc facebook_api::json_to_multirow { @@ -179,16 +199,16 @@ template::multirow create $multirow set i 1 foreach elm $list_data { - array set arr_data $elm - template::multirow append $multirow - foreach name [array names arr_data] { - if {[lsearch [template::multirow columns $multirow] $name] < 0} { - template::multirow extend $multirow $name + array set arr_data $elm + template::multirow append $multirow + foreach name [array names arr_data] { + if {[lsearch [template::multirow columns $multirow] $name] < 0} { + template::multirow extend $multirow $name + } + template::multirow set $multirow $i $name $arr_data($name) } - template::multirow set $multirow $i $name $arr_data($name) + incr i } - incr i - } } ad_proc facebook_api::redirect { @@ -219,11 +239,15 @@ if they are not a user } { set user [facebook_api::get_current_user_info -package_key $package_key -session_key $session_key -uid $uid] - array set user_array [lindex [json::json2dict $user] 0] - if {!$user_array(has_added_app)} { - redirect "http://www.facebook.com/add.php?api_key=[api_key -package_key $package_key]" + + set user_info_list [json::json2dict $user] + + if { [llength $user_info_list]==0 || [lindex $user_info_list 0] == "error_code" } { + facebook_api::redirect "http://www.facebook.com/add.php?api_key=[api_key -package_key $package_key]" + } else { + array set user_array [lindex $user_info_list 0] + return $user } - return $user } # *************************** @@ -234,7 +258,7 @@ ad_proc facebook_api::get_current_user_info { -package_key -session_key - {-fields "uid,first_name,last_name,status,pic_square,pic,about_me,sex,hometown_location,hs_info,interests,movies,music,political,quotes,religion,has_added_app"} + {-fields "uid,name,first_name,last_name,status,pic_square,pic,about_me,sex,hometown_location,hs_info,interests,movies,music,political,quotes,religion,has_added_app"} -uid } { Get the user information of the current user. @@ -314,6 +338,51 @@ } # *************************** +# Photo procs +# - procs to retrieve facebook photos +# *************************** + +ad_proc facebook_api::photo_getalbums { + -package_key + -session_key + -uid + {-format "json"} +} { + Returns a list of facebook photo albums from a user with the give uid + http://developer.facebook.com/documentation.php?v=1.0&method=photos.getAlbums +} { + return [facebook_api::do_request -package_key $package_key -method "photos.getAlbums" -params [list session_key $session_key uid $uid format $format]] +} + +ad_proc facebook_api::photo_getphotos { + -package_key + -session_key + {-subj_id ""} + {-aid ""} + {-pids ""} + {-format "json"} +} { + Returns a list of photos + http://developer.facebook.com/documentation.php?v=1.0&method=photos.get +} { + set params [list session_key $session_key format $format] + if { [exists_and_not_null subj_id] } { + lappend params "subj_id" + lappend params $subj_id + } + if { [exists_and_not_null aid] } { + lappend params "aid" + lappend params $aid + } + if { [exists_and_not_null pids] } { + lappend params "pids" + lappend params $pids + } + return [facebook_api::do_request -package_key $package_key -method "photos.get" -params $params] +} + + +# *************************** # Feed procs # - procs related to publishing feeds to user's profile page # *************************** @@ -366,7 +435,7 @@ # Custom procs # - we're going to add some useful features to # this api, e.g. scoring, caching user info -# - note some of this are net yet fully functional +# - note some of this are not yet fully functional # *************************** ad_proc facebook_api::score_friends { @@ -432,7 +501,7 @@ -session_key -package_key } { - Update the list of this users friends in out database + Update the list of this users friends in our database } { if {![facebook_api::update_friends_p -uid $uid]} { return Index: openacs-4/packages/facebook-api/www/doc/index.html =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/facebook-api/www/doc/index.html,v diff -u -N -r1.1 -r1.2 --- openacs-4/packages/facebook-api/www/doc/index.html 15 Dec 2007 11:27:07 -0000 1.1 +++ openacs-4/packages/facebook-api/www/doc/index.html 3 Nov 2009 06:22:06 -0000 1.2 @@ -14,8 +14,8 @@

Authors :
Dave Bauer (dave@solutiongrove.com)
Hamilton Chua (ham@solutiongrove.com) -
Last Updated : 10/27/07 -
Version : 0.1d +
Last Updated : 1/29/08 +
Version : 0.2d

Overview

@@ -66,20 +66,21 @@ } # check that we are passed an auth_token -# if we don't have an auth_token, redirect to -# http://apps.facebook.com/your_application_name -# to get one. -# if there's no auth_token, it's most likely that -# the user got here without going thru facebook +# an auth_token allows us to get a session_key from facebook +# if auth_token is empty, it is most likely that we already have a +# session_key in fb_sig_session_key if {$auth_token ne ""} { - facebook_api::get_session_from_token -package_key "your_package_key" -auth_token $auth_token -url "http://apps.facebook.com/canvas_page_url" + set session_info [facebook_api::get_session_from_token -package_key $package_key -auth_token $auth_token -url $app_url] + set fb_sig_session_key [lindex $session_info 0] + set fb_sig_user [lindex $session_info 1] } -# check that we are passed fb_sig_session_key +# check that we have an fb_sig_session_key # if we don't have this parameter it means that -# the user doesn't have this app installed -# you'll need to redirect to the facebook add page +# the user didn't get here from facebook +# but rather directly +# ask the user to login or add the app if { [exists_and_not_null fb_sig_session_key] } { @@ -105,13 +106,26 @@
  • Make sure that you choose Use iframe.
  • Click submit -
  • Your new application shoud now be listed under My Applications.
  • +
  • Your new application should now be listed under My Applications.
  • Click on it to add your new application to your list of Facebook Applications.
  • Click on the name of your application from your list of applications to launch your app
  • The page should return JSON with your friends data. -

    +

    Authenticating with Facebook

    +

    As of version 0.2d, the facebook api package has procs that allow you to do the following .... +

    +

    For more information about authenticating with facebook refer to this page. +

    The facebook api package now has two new tables : +

    +

    Authenticating with facebook is useful if you want to run a web application outside of the facebook user interface but still have access to data from facebook. \ No newline at end of file