如何使用新的Facebook PHP SDK 3.0.0登录OFFLINE_ACCESS? [英] How to login with OFFLINE_ACCESS using the new Facebook PHP SDK 3.0.0?

查看:112
本文介绍了如何使用新的Facebook PHP SDK 3.0.0登录OFFLINE_ACCESS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ session = array 

'uid'=> $ userdata ['fb_uid'],
'sig'=> $ userdata ['fb_sig'],
'access_token'=> $ userdata ['fb_access_token']
);

$ facebook-> setSession($ session);

在新的SDK中,此函数不再存在。我想我需要登录:


setPersistentData($ key,$ value)


但是这个函数是受保护的,我不知道'代码'是什么?我需要这个来登录用户吗? sig发生了什么?我不需要这个了吗?



希望有人已经弄清楚了,因为文档真的没有帮助!

解决方案

使用Facebook PHP SDK v3(在github上查看),它很简单要使用 offline_access 权限登录某人,请在生成登录URL时询问。



获取离线访问令牌



首先检查用户是否被记录in / not:

  requirefacebook.php; 
$ facebook = new Facebook(array(
'appId'=> YOUR_APP_ID,
'secret'=> YOUR_APP_SECRET,
));

$ user = $ facebook-> getUser();

if($ user){
try {
$ user_profile = $ facebook-> api('/ me');
} catch(FacebookApiException $ e){
//我们拥有的访问令牌无效
$ user = null;
}
}

如果他不是,你会生成 Facebook的URL要求 offline_access 权限:

  if(!$ user){
$ args ['scope'] ='offline_access';
$ loginUrl = $ facebook-> getLoginUrl($ args);
}

然后在模板中显示链接:

 <?php if(!$ user):?> 
< a href =<?php echo $ loginUrl?>>使用Facebook登录< / a>
<?php endif?>

然后,您可以检索脱机访问令牌并将其存储。要获得它,请致电:

  $ facebook-> getAccessToken()
/ pre>

使用离线访问令牌



在用户未登录时使用脱机访问令牌:

  requirefacebook.php; 
$ facebook = new Facebook(array(
'appId'=> YOUR_APP_ID,
'secret'=> YOUR_APP_SECRET,
));

$ facebook-> setAccessToken(...);

现在,您可以为此用户拨打API:

  $ user_profile = $ facebook-> api('/ me'); 

希望有帮助!


with the old (2.x) SDK I used this to log someone with offline_access:

$session = array
(
    'uid' => $userdata['fb_uid'],
    'sig' => $userdata['fb_sig'],
    'access_token' => $userdata['fb_access_token']
);

$facebook->setSession($session);

In the new SDK this function doesnt exist anymore. I think I need to login using:

setPersistentData($key, $value)

but this function is protected and I dont know what 'code' is? Do I need this to log the user in or not? And what's going on with 'sig'? Don't I need this anymore?

Hope someone already figured this out because the documentation really doesn't help!

解决方案

With the Facebook PHP SDK v3 (see on github), it is pretty simple. To log someone with the offline_access permission, you ask it when your generate the login URL. Here is how you do that.

Get the offline access token

First you check if the user is logged in or not :

require "facebook.php";
$facebook = new Facebook(array(
    'appId'  => YOUR_APP_ID,
    'secret' => YOUR_APP_SECRET,
));

$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    // The access token we have is not valid
    $user = null;
  }
}

If he is not, you generate the "Login with Facebook" URL asking for the offline_access permission :

if (!$user) {
    $args['scope'] = 'offline_access';
    $loginUrl = $facebook->getLoginUrl($args);
}

And then display the link in your template :

<?php if (!$user): ?>
    <a href="<?php echo $loginUrl ?>">Login with Facebook</a>
<?php endif ?>

Then you can retrieve the offline access token and store it. To get it, call :

$facebook->getAccessToken()

Use the offline access token

To use the offline access token when the user is not logged in :

require "facebook.php";
$facebook = new Facebook(array(
    'appId'  => YOUR_APP_ID,
    'secret' => YOUR_APP_SECRET,
));

$facebook->setAccessToken("...");

And now you can make API calls for this user :

$user_profile = $facebook->api('/me');

Hope that helps !

这篇关于如何使用新的Facebook PHP SDK 3.0.0登录OFFLINE_ACCESS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆