0

我想在 Facebook graph api 的帮助下通过 PHP 更新 FAN-PAGE 的状态。谷歌说:不起作用。

现在我想通过 PHP 更新我自己的用户状态。我的主要问题是如何让我自己的用户登录到图形 api(使用 PHP),而不使用浏览器,也没有有趣的 PHP 变通方法。

4

3 回答 3

1

在这两种情况下,您都需要获得publish_stream许可http://developers.facebook.com/docs/authentication/permissions

这可以通过FB.login()来完成

更多信息:http: //developers.facebook.com/docs/authentication

之后,您可以使用图形 api 帖子更新状态:http: //developers.facebook.com/docs/reference/api/post

于 2011-02-01T09:37:18.590 回答
1

我的主要问题是如何在不使用浏览器且没有有趣的 php 变通方法的情况下将我自己的用户登录到图形 api(使用 php)。

如果通过浏览器与他交互至少一次以获取offline_access.

这个答案解释了如何获得offline_access许可以及如何从那里开始使用它。

编辑:
请阅读评论!谢谢@zerkms!

于 2011-02-01T10:01:50.637 回答
1

您需要几件事情来更新您的 facebook 个人资料或页面的提要:facebook 应用程序(client_idclient_secret)、profile_idaccess_token(publish_stream、manage_pages、offline_access 权限)

您需要 offline_access 因为如果没有,那么访问令牌将过期。如果您已经阅读过,如果您已经指定了 publish_stream,则不需要offline_access,那么它们只是意味着您并不总是需要它。

发布帖子很容易:

$data = array(
    'access_token' => $access_token,
    'message' => 'status message',
    );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/{$profile_id}/feed");

现在如何获取profile_idaccess_token,你可以使用我的 app post panda,或者制作你自己的脚本。我将它包括在这里:

# arvin castro
# http://codecri.me/
# January 16, 2011

$client_id     = ''; # application id
$client_secret = ''; # application secret
$callbackURL   = 'http://'; # the URL of this script
$extendedPermissions = 'publish_stream,manage_pages,offline_access';

session_name('facebookoauth');
session_start();

if(isset($_GET['logout']) and $_SESSION['loggedin']) {
    $_SESSION = array();
    session_destroy();
}

if(isset($_GET['signin'])) {

    # STEP 1: Redirect user to Facebook, to grant permission for our application
    $url = 'https://graph.facebook.com/oauth/authorize?' . xhttp::toQueryString(array(
        'client_id'    => $client_id,
        'redirect_uri' => $callbackURL,
        'scope'        => $extendedPermissions,
    ));
    header("Location: $url", 303);
    die();
}

if(isset($_GET['code'])) {

    # STEP 2: Exchange the code that we have for an access token
    $data = array();
    $data['get'] = array(
        'client_id'     => $client_id,
        'client_secret' => $client_secret,
        'code'      => $_GET['code'],
        'redirect_uri'  => $callbackURL,
        );

    $response = xhttp::fetch('https://graph.facebook.com/oauth/access_token', $data);

    if($response['successful']) {

        $var = xhttp::toQueryArray($response['body']);
        $_SESSION['access_token'] = $var['access_token'];
        $_SESSION['loggedin']     = true;

    } else {
        print_r($response['body']);
    }
}

if($_SESSION['loggedin']) {
    // Get Profile ID
    $data = array();
    $data['get'] = array(
            'access_token'  => $_SESSION['access_token'],
            'fields' => 'id,name,accounts',
            );  
    $response = xhttp::fetch('https://graph.facebook.com/me', $data);
    echo '<pre>';
    print_r(json_decode($response['body'], true));
    echo '</pre>';

} else {
    echo '<a href="?signin">Sign in with Facebook</a>';
}

?>

我正在使用我的 cURL 包装类xhttp

于 2011-02-01T15:04:26.413 回答