开发者

Using Facebook Graph API from a mobile application

开发者 https://www.devze.com 2023-02-02 04:30 出处:网络
I have a small card game at Facebook (and few Russian social networks), which gets user\'s id, first name and avatar through the old REST API.

I have a small card game at Facebook (and few Russian social networks), which gets user's id, first name and avatar through the old REST API.

Now I'm trying to develop the same game as a mobile app with Flex Hero SDK for Android and iPhone. Which 开发者_开发百科means I can't use native SDKs for those platforms, but have to use OAuth as descibed at Facebook page.

Using Facebook Graph API from a mobile application

I'm trying to write a short PHP script, which would return the user information as XML to my mobile app. My script can get the token already:

<?php
define('FB_API_ID', 'XXX');
define('FB_AUTH_SECRET', 'XXX');
$code = @$_GET['code'];

# this is just a link for me, for development puposes
if (!isset($code)) {
        $str = 'https://graph.facebook.com/oauth/authorize?client_id=' . FB_API_ID .
                '&redirect_uri=http://preferans.de/facebook/mobile.php&display=touch';
        print "<html><body><a href=\"$str\">$str</a></body></html>";
        exit();
}

$req = 'https://graph.facebook.com/oauth/access_token?client_id=' . FB_API_ID .
'&redirect_uri=http://preferans.de/facebook/mobile.php&client_secret=' . FB_AUTH_SECRET .
'&code=' . $code;

$ch = curl_init($req);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$page = curl_exec($ch);
if (curl_errno($ch))
        exit('Download failed');
curl_close($ch);

parse_str($page, $data);

#header('Content-Type: text/xml; charset=utf-8');
#print('<?xml version="1.0"? ><app>');
print_r($data);
#print('</app>');
?>

This works well and I get back the token:

Array
(
    [access_token] => 262578703638|2.OwBuoa2fT5Zp_yo2hFUadA__.3600.1294904800-587287941|ycUNaHVxa_8mvenB9JB1FH3DcAA
    [expires] => 6697
)

But how can I use this token now to find the user's name and avatar and especially I'm confused by how will I get the current user id?

While using REST API I've always known the current user id by calling $userid=$fb->require_login()


See docs at: http://developers.facebook.com/docs/api

Use curl to request: https://graph.facebook.com/me/?access_token=XXXXXXX

The "/me" will get you all of the info you need.

$req = 'https://graph.facebook.com/me/?access_token=' . $access_token;
$ch = curl_init($req);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$page = curl_exec($ch);
if (curl_errno($ch))
        exit('Download failed');
curl_close($ch);

$user_object = json_decode($page);

var_dump($user_object);

Will return something like:

object(stdClass)#1 (20) {
  ["id"]=>
  string(10) "1017193642"
  ["name"]=>
  string(19) "Lance Scott Rushing"
  ["first_name"]=>
  string(5) "Lance"
  ["middle_name"]=>
  string(5) "Scott"
  ["last_name"]=>
  string(7) "Rushing"
  ["link"]=>
  string(36) "http://www.facebook.com/LanceRushing"
  .....


Since I cannot add comments I'll answer here.

In order to get the profile picture you need to request https://graph.facebook.com/ID/picture. If you want only specific fields you can specify it this way: https://graph.facebook.com/ID?fields=uid,name,etc&access_token=token

Alternatively you can use the PHP SDK to authorise and log in the user so that you don't have to get the token manually - it would simplify your code. For instance, instead of every cURL request you could just do $facebook->api(request); A good description and example are here http://apps.facebook.com/graphapidemo/.

0

精彩评论

暂无评论...
验证码 换一张
取 消