Reputation: 5931
I am trying to log in a user to facebook using an offline token (I had it working for a long time), but it seems maybe facebook changed something?
Quick Run Down: I had the Facebook PHP SDK (2.1) Authentication through OAuth. I recall the return data after getting an access token was something like:
callbackurl?session={access_token:....,expires:....,session_key....}
etc.. Basically a JSON object in the URL.
But now it appears they have changed parameters and all I get back is something like this:
[fb_199972360033499_access_token] What are these digits?
[fb_199972360033499_user_id]
(I have tried with (PHP SDK 2.1 and 3.1.1)
Previously I was storing setting up saved token data with
$facebook->setSession($dataArray);
But now I no longer know what to do, if anyone has any clues on where to look Id appreciate it, as I have a hard time finding the right instruction in the actual FB documentation.
Upvotes: 2
Views: 709
Reputation: 38135
Let us use the new PHP-SDK to get you the access token:
<?php
/**
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
require './src/311/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'offline_access'));
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
</body>
</html>
Here are a couple of notes:
offline_access
permission when constructing the login URLNow log-out from Facebook and let's use the access_token
:
<?php
require './src/311/facebook.php';
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
$offline_access_token = "XXXXXX";
try {
$user_profile = $facebook->api('/me', 'GET', array('access_token'=>$offline_access_token));
echo "<pre>";
print_r($user_profile);
echo "</pre>";
} catch (FacebookApiException $e) {
error_log($e);
}
?>
Upvotes: 2
Reputation: 2981
New PHP SDK
For examples on using the new PHP SDK you should look:
https://github.com/facebook/php-sdk
http://developers.facebook.com/blog/post/534/
It's also a good idea to take a look at the source code.
Offline Access
http://developers.facebook.com/docs/authentication/
If you have an authorised user you can now get the user access_token
with:
$access_token = $facebook->getAccessToken();
If no authorised user is available it will return the application access token.
If your app needs an access token with an infinite expiry time (perhaps to take actions on the user's behalf after they are not using your app), you can request the offline_access permission.
To get the user id:
$user_id = $facebook->getUser();
Application Id
[fb_199972360033499_access_token] What are these digits?
Those digits should be your application id.
Persistant Data
$facebook->setSession($dataArray);
This should no longer be necessary as the PHP SDK takes care of that. Unless you want to store your own data - in which case you should create your own session data.
Upvotes: 2