Textus
Textus

Reputation: 109

facebook storing access token for offline access with php sdk - really confused

I want our web app to be able to update a users wall using the offline access permission. I'm going around in circles at the moment because I don't seem to be able to store the access token. I've tried using example.php but am not retaining the session (I think, please forgive me I'm a newbie)

The way I want it to work is as follows:

User Clicks add Facebook - i.e. our app is approved by user (I can do this using the graph api)

Token is returned and we save it in database (this bit I'm struggling to get my head around) to enable a post later.

If anyone can give me a step by step guide I'd really appreciate it. Please don't just redirect me to a the developers page at facebook.

Upvotes: 0

Views: 11566

Answers (2)

Damodaran
Damodaran

Reputation: 11057

You use the javascript for the authentication as

FB.login(function(response) {
    // As of Facebook's transition to OAuth 2.0 session has been replaced with authResponse
    // See https://developers.facebook.com/blog/post/525/
    // var access_token = response.session.access_token;
    var access_token = null;
    if (response.authResponse) {
        access_token = response.authResponse.accessToken;    

        if (response.perms) {
            // user is logged in, everithig is ok                     
        } else {
            // user is logged in, but did not grant any permissions
        }
    } else {
        // user is not logged in
    }
}, {perms:'read_stream,publish_stream,offline_access'});

You will get the access token in javascript variable access_token.You can save this access token in database and then publish to wall using the following code

function graphStreamPublish(){
           var body = document.getElementById("txtTextToPublish").value;
            FB.api('/me/feed', 'post', { message: body }, function(response) {
                if (!response || response.error) {
                     alert('Error occured');
                } else {
                     alert('Post ID: ' + response.id);
                }
           });
     }

Or if you want to use php sdk then create authentication.php as follows.

<?php 
$app_id = "YOUR_APP_ID";
    $app_sec = "APP_SEC";
$canvas_page = "APP_CANVAS_PAGE_URL";
$scope = "&scope=user_photos,email,publish_stream";           $auth_url"http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" .       urlencode($canvas_page).$scope; 
$signed_request = $_REQUEST["signed_request"];   
list($encoded_sig, $payload) = explode(".", $signed_request, 2); 
$data = json_decode(base64_decode(strtr($payload, "-_", "+/")), true); 
     if (empty($data["user_id"])) {
 echo(""); } 
                     $access_token = $data["oauth_token"]; 
$user_id = $data["user_id"]; 
$user = json_decode(file_get_contents( "https://graph.facebook.com/me?access_token=" .                 $access_token)); 
function get_facebook_cookie($app_id, $application_secret) { 
$args = array();
parse_str(trim($COOKIE["fbs" . $app_id], "\""), $args); 
ksort($args);
$payload = ""; 
foreach ($args as $key => $value) { 
if ($key != "sig") { 
$payload .= $key . "=" . $value; 
} 
} 
    if (md5($payload . $application_secret) != $args["sig"]) {
return null; 
} 
return $args; 
} 
$cookie = get_facebook_cookie($app_id, $app_sec); 
?>

In the page where you need to publish to wall include this page and facebook api page(facebook.php) then the code to publish to wall

$attachment = array('message' => 'some meesgae',
        'name' => 'This is my demo Facebook application!',
        'caption' => "Caption of the Post",
        'link' => 'mylink.com',
        'description' => 'this is a description',
        'actions' => array(array('name' => 'Get Search', 'link' => 'google.com')) );
    $result = $facebook->api('/me/feed?access_token='.$access_token, 'post', $attachment);

I think this is helpful..

Upvotes: 1

Janar J&#252;risson
Janar J&#252;risson

Reputation: 510

I have created a simple demo iframe application which uses facebook php sdk to do exactly what needed.

http://eagerfish.eu/using-facebook-off-line-access-to-post-on-users-wall/

Hope it helps out someone.

Upvotes: 0

Related Questions