I've been going through FB docs but I think I am totally lost now. My task is quite simple: I need to have an "import" link, user click开发者_如何学运维s it and receives FB popup where he authorizes the site, user is redirected back to my site, I access user's FB profile and retrieve some data. Also: I need to access and retrieve profile in PHP.
The first part goes well. I created mysite.com?page=import link which opens in popup and then redirects to https://graph.facebook.com/oauth/authorize?... User then allows access and the popup is redirected back to mysite.com?...#access_token=...&expires_in=4031 Then I am going to close the popup and instead refresh the parent window that opened this popup by redirecting it to something like this mysite.com?page=register&access_token=...&expires_in=4031 Then I was going to use their PHP SDK hoping that it can take this access token and allow me to get user's data. However I have no luck so far. I've tried lots of things and went through facebook.php but can't see a solution. Please let me know how to do this: authorize user in a popup and then gather the profile data in php.
here is your working example. it has a redirect and works entirely in php, i didnt do javascript beacuse this is easier and faster to write. the main difference is that the returned code after the authorization page is only a code which allows you to fetch the actual access token incombination with your client secret. otherwise anyone could get an access token for yuor application or you would have to pass your secret token in the url. thats the reason for the second step. in javascript we dont need that because facebook only redirects back to a whitelisted domain and as the access token is in the url fragment after the # tag the server cant access it, only the client. and this is ensured to be yours erver as your domain must be whitelisted. but it needs more client interaction... well anyway. you can use the code i wrote for yuo and you can also do it in a popup. you just have to pass your variables to the form or whatever you are doing but this shouldnt be a problem. i did it this way because you said you needed it in php. a good javascript example can be found on the facebook connect page here: http://developers.facebook.com/docs/guides/web#login
any questions? comment!
ps: i put the @ sign before the file_get_contents function because you might geht ssl errors, you should actually use curl for that and do error handling!
save this file as whatever.php to your server, check the 4 config variables in the top and hit it
<?
// your app id
$app_id = "123";
// your secret token
$mysecret = '***';
// your website correctly set up in facebook app config
$redirecturl = "http://www.yourdomain.com/whatever/thisfile.php?action=authorized";
// what you want to fetch
$scopes = array('email','user_interests'); // whatever you want
if($_GET['action'] == 'authorize') {
$url = "https://graph.facebook.com/oauth/authorize?client_id=".$app_id."&redirect_uri=";
$url .= urlencode($redirecturl);
$url .= "&display=popup";
$url .= "&scope=".implode(',',$scopes);
header("Location: $url");
exit();
} else if($_GET['action'] == 'authorized') {
$code = $_GET['code'];
$tokenurl = 'https://graph.facebook.com/oauth/access_token'.
'?client_id='.$app_id .
'&redirect_uri='.urlencode($redirecturl).
'&client_secret='.$mysecret.
'&code='.$code;
$token = @file_get_contents($tokenurl);
$token = preg_match('/access_token=(.*)&/',$token,$extracted_token);
$token = $extracted_token[1];
$dataurl = 'https://graph.facebook.com/me?access_token='.$token;
$result = @file_get_contents($dataurl);
$data = json_decode($result);
echo "<pre>".print_r($data,true)."</pre>";
} else {
?><a href="?action=authorize">click here to immport your data</a><?
}
?>
精彩评论