我正在努力将 Facebook 连接实现到我的 codeigniter 应用程序中——我现在有一个问题。从这里下载 PHP SDK 后:https ://github.com/facebook/php-sdk/我将 /src 文件复制到我的 CI 库中(每个人都这样做: ))。
现在我想使用这种方法对我的用户进行身份验证:
https://developers.facebook.com/docs/authentication/server-side/
我的控制器在这里(我认为问题出在最后,但粘贴了完整文件,因为我不确定):
<?php
class Fb25 extends CI_Controller {
function index()
{
$app_id = "MY APP ID";
$app_secret = "MY SECRET KEY";
$site_url = "http://devlocalhost.com/";
$this->load->library('facebook',array('appId' => $app_id, 'secret' => $app_secret));
$user = $this->facebook->getUser();
$data['user'] = $user;
if($user){
$data['user_profile'] = $this->facebook->api('/me');
$user_profile = $data['user_profile'];
} else{
$user = NULL;
}
if($user){
// Get logout URL
$data['logoutUrl'] = $this->facebook->getLogoutUrl(array(
'next' => 'http://devlocalhost.com/', // URL to which to redirect the user after logging out
));
}else{
// Get login URL
$data['loginUrl'] = $this->facebook->getLoginUrl(array(
'scope' => 'email', // Permissions to request from the user
'redirect_uri' => 'http://devlocalhost.com/fb25/good', // URL to redirect the user to once the login/authorization process is complete.
));
}
if($user){
// Save your method calls into an array
$data['queries'] = array(
array('method' => 'GET', 'relative_url' => '/'.$user),
array('method' => 'GET', 'relative_url' => '/'.$user.'/home?limit=50'),
array('method' => 'GET', 'relative_url' => '/'.$user.'/friends'),
array('method' => 'GET', 'relative_url' => '/'.$user.'/photos?limit=6'),
);
// POST your queries to the batch endpoint on the graph.
$batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
//Return values are indexed in order of the original array, content is in ['body'] as a JSON
//string. Decode for use as a PHP array.
$data['user_info'] = json_decode($batchResponse[0]['body'], TRUE);
$data['feed'] = json_decode($batchResponse[1]['body'], TRUE);
$data['friends_list'] = json_decode($batchResponse[2]['body'], TRUE);
$data['photos'] = json_decode($batchResponse[3]['body'], TRUE);
}
echo 'user_info: '.$data['user_info'];
print_r($data);
$this->load->view('fb25_view', $data);
}
public function good(){
$app_id = "MY APP ID";
$app_secret = "MY SECRET KEY";
$site_url = "http://devlocalhost.com/";
echo '<br/><br/>SESJA:<br/>';
print_r($_SESSION);
echo '<br/><br/>GET:<br/>';
print_r($_GET);
echo '<br/><br/>REQUEST:<br/>';
print_r($_REQUEST);
echo '<br/><br/>najs<br/><br/>';
session_start();
$code = $_GET["code"];
//@@@@@@IMO PROBLEM IS SOMEWHERE HERE:
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
}
}
?>
我的问题是:我正在按下登录按钮并进入 facebook 以验证自己,单击“转到应用程序”并想返回我的网站 - 它说:
Message: file_get_contents(https://graph.facebook.com/oauth/access_token? client_id=487560264592073&redirect_uri=http%3A%2F%2Fdevlocalhost.com%2F&client_secret=mysecretcode&code=AQAgnmNWwDub9yaRB6Vf73gg8Xvwo8KhIM077lB67_bu1Z3rAvyk3Ckl54qK7hh9o3VkG0rFIBTfRXwtrSBFVWEpqYfm1o7e5CQg3jVctq-EE1ZxWrgWrfesLpQ2oF3wlmEMb5o6ORobGmibT06kqe5f2N0ch4kSYBJ4SiTcdV-612fGOJHGcipeyU_GJJ0Jvsg)
[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
更新
请看这里:
我在您的控制器中添加了功能:
public function link(){
$fb_usr = $this->fb_connect->user;
$firstname = $fb_usr['first_name'];
$my_url="http://devlocal.pl";
echo 'user_id';
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=myclientid&redirect_uri=" . urlencode($my_url)
. "&client_secret=mysecretcode&code=" . $_GET['code'];
redirect($token_url);
}
要获取我的访问令牌 - 没有访问令牌,我无法获取我的用户名。我将您的 loginbyfacebook 函数重定向到链接函数。而且它不显示 fb 用户名。
当我重定向到我的 token_url 它显示:
{
"error": {
"message": "Error validating verification code.",
"type": "OAuthException",
"code": 100
}
}