在多个 PHP 文件中使用 Facebook 对象

发布于 2025-01-05 07:39:56 字数 1099 浏览 1 评论 0原文

我正在创建一个包含多个 PHP 页面的 Facebook 应用程序。如果我在所有文件中重新实例化 Facebook 对象,我会收到错误 OAuth 访问令牌签名无效。

我尝试了多种替代方法,而且 $facebook->getSession() 函数在新框架中不起作用并且已被弃用。

因此,我尝试将访问令牌保留在会话中,并在下一个 PHP 文件的 Facebook 对象中使用相同的访问令牌。

在我的第一页上,我已经使用以下方法实例化了我的应用程序:

$app_id = "107684706025330";
$app_secret = "__SECRET__";
$my_url = "http://www.sentimentalcalligraphy.in/wp-content/then_n_now/";
$config = array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
);
session_start();
$facebook = new Facebook($config);
$access_token = $facebook->getAccessToken();
echo $access_token;
$_SESSION['fob'] = $access_token;

在下一个 PHP 文件中,我需要第二次使用 Facebook 对象:

$config = array(
'appId' => '107684706025330',
'secret' => '__SECRET__',
);
$facebook = new Facebook($config);
$access_tocken = $_SESSION['fob'];
echo $access_token;
$facebook->setAccessToken($access_token);

我仍然收到访问令牌无效的错误。我该怎么用这个呢。我需要在我的第二个 PHP 文件中使用以下代码:

$friends = $facebook->api('/me/friends','GET');

提前感谢您, 纳西尔

I am creating a Facebook Application containing multiple PHP pages. If I reinstantiate Facebook objects in all the files I get an Error Invalid OAuth access token signature.

I have tried a number of alternatives, also the $facebook->getSession() function doesn't work in the new framework and is deprecated.

So I tried to keep the access token in the session and use the same access token in my next PHP file's Facebook object.

On my first page i Have instantiated my App using:

$app_id = "107684706025330";
$app_secret = "__SECRET__";
$my_url = "http://www.sentimentalcalligraphy.in/wp-content/then_n_now/";
$config = array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
);
session_start();
$facebook = new Facebook($config);
$access_token = $facebook->getAccessToken();
echo $access_token;
$_SESSION['fob'] = $access_token;

On the next PHP file were I need to use the Facebook object for the second time:

$config = array(
'appId' => '107684706025330',
'secret' => '__SECRET__',
);
$facebook = new Facebook($config);
$access_tocken = $_SESSION['fob'];
echo $access_token;
$facebook->setAccessToken($access_token);

I still get an error that the access token is not valid. How am I supposed to use this. I need to use the following code in my second PHP file:

$friends = $facebook->api('/me/friends','GET');

Thanking you in Advance,
Nasir

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港巷口少年归 2025-01-12 07:39:56

我有一些 Facebook API 的代码。我想和你分享。

// App user account and password
$app_id = 'blabla';
$app_secret = 'blabla';
// 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();
// Login or logout url will be needed depending on current user state.
if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
} else {
    $loginUrl = $facebook->getLoginUrl();
}
$access_token = $facebook->getAccessToken();
if(isset($_GET["code"])){ 
    // Get our app user feed
    $user_profile_feed = $facebook->api('/me');

    // Insert user id into users table while user login our app
    // We have to control user who inserted our users table after before
    $query_is_user = mysql_query("SELECT * FROM `users` WHERE `user_id` = ".$user_profile_feed['id']) or die("hata 1");
    $count = mysql_num_rows($query_is_user);
    if($count == 0) {
        mysql_query("INSERT INTO `users`(`user_id`) VALUES (".$user_profile_feed['id'].")") or die("hata 2");
        echo '<script language=Javascript>alert("Uygulamamiza Basariyla Giris Yaptiniz...");</script>';
    }

    $token_url = "https://graph.facebook.com/oauth/access_token?"
                         . "client_id=" . $app_id . "&redirect_uri=" . "http://apps.facebook.com/machine_learning/"
                         . "&client_secret=" . $app_secret . "&code=" . $_GET["code"];
    $response = @file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    mysql_query("UPDATE users SET access_token='$params[access_token]' WHERE user_id='$user_profile_feed[id]'") or die("hata 4");
}

$url = "https://graph.facebook.com/oauth/authorize?"
        ."client_id=262609310449368&"
        ."redirect_uri=http://apps.facebook.com/machine_learning/&scope=read_stream";

I have a some code for Facebook API. I want to share with you.

// App user account and password
$app_id = 'blabla';
$app_secret = 'blabla';
// 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();
// Login or logout url will be needed depending on current user state.
if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
} else {
    $loginUrl = $facebook->getLoginUrl();
}
$access_token = $facebook->getAccessToken();
if(isset($_GET["code"])){ 
    // Get our app user feed
    $user_profile_feed = $facebook->api('/me');

    // Insert user id into users table while user login our app
    // We have to control user who inserted our users table after before
    $query_is_user = mysql_query("SELECT * FROM `users` WHERE `user_id` = ".$user_profile_feed['id']) or die("hata 1");
    $count = mysql_num_rows($query_is_user);
    if($count == 0) {
        mysql_query("INSERT INTO `users`(`user_id`) VALUES (".$user_profile_feed['id'].")") or die("hata 2");
        echo '<script language=Javascript>alert("Uygulamamiza Basariyla Giris Yaptiniz...");</script>';
    }

    $token_url = "https://graph.facebook.com/oauth/access_token?"
                         . "client_id=" . $app_id . "&redirect_uri=" . "http://apps.facebook.com/machine_learning/"
                         . "&client_secret=" . $app_secret . "&code=" . $_GET["code"];
    $response = @file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    mysql_query("UPDATE users SET access_token='$params[access_token]' WHERE user_id='$user_profile_feed[id]'") or die("hata 4");
}

$url = "https://graph.facebook.com/oauth/authorize?"
        ."client_id=262609310449368&"
        ."redirect_uri=http://apps.facebook.com/machine_learning/&scope=read_stream";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文