integrating Facebook SDK 4.xx with codeigniter, error with facebooksession class - php

I am trying to integrate facebook SDK with a codeigniter website.
I have used composer to install the files with the composer.json file with the code provided in the facebook documentation. I have looked at multiple other potentially similar problems on stackoverflow and around and came across examples like this:
http://metah.ch/blog/2014/05/facebook-sdk-4-0-0-for-php-a-working-sample-to-get-started/#post-937
CodeIgniter Facebook SDK 4 with Composer
Error: Class 'Facebook\FacebookSession' not found with the facebook PHP SDK
The error I recieve is:
Fatal error: Uncaught exception 'Facebook\FacebookSDKException' with message 'You must provide or set a default application id.' in ....
I have the following code in my controller:
public function login()
{
$fb_config = array(
'appId' => 'xxxxx',
'secret' => 'xxxx'
);
$this->load->library('facebook', $fb_config);
$user = $this->facebook->getUser();
if ($user) {
try {
$data['user_profile'] = $this->facebook
->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
if ($user) {
$data['logout_url'] = $this->facebook
->getLogoutUrl();
} else {
$data['login_url'] = $this->facebook
->getLoginUrl();
}
$this->load->view('templates/header');
$this->load->view('user/login',$data);
}
I have tried adding require and use and session start at the top of the class but the issue persists.

Related

Cant post on users timeline

how to post on users timeline using php sdk
i search about that issue on stackoverflow i got nothing plx help
i need to do this for my client
<?php
namespace App;
use Facebook\Facebook;
class FacebookClass{
private $fb;
private $appiD = 3073019182770120;
private $appSecret = "6360e1540e1add2b6fc93fce07fcb61a";
public function __construct()
{
$this->fb = new Facebook([
'app_id' => $this->appiD, // Replace {app-id} with your app id
'app_secret' => $this->appSecret,
'default_graph_version' => 'v4.0',
]);
}
public function getLoginUrl() {
$helper = $this->fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions
$loginUrl = $helper-
>getLoginUrl('http://localhost/social_platform/',$permissions);
return $loginUrl;
}
public function loginWithFacebook() {
$helper = $this->fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
if(isset($accessToken))
{
$_SESSION['facebook']['access_token'] = (string) $accessToken;
header("index.php");
}
} catch (\Throwable $th) {
// var_dump($th);
}
}
public function postOnUserTimeLine() {
// posting on user timeline using publish_actins permission
try {
// message must come from the user-end
$data = ['message' => 'testing...'];
$request = $this->fb->post('/me/feed', $data,
$_SESSION['facebook']['access_token']);
$response = $request->getGraphNode()->asArray;
} catch (Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
echo $response['id'];
}
}
?>
Fatal error: Uncaught Facebook\Exceptions\FacebookAuthorizationException: (#200) If posting to a group, requires app being installed in the group, and \ either publish_to_groups permission with user token, or both manage_pages \ and publish_pages permission with page token; If posting to a page, \ requires both manage_pages and publish_pages as an admin with \ sufficient administrative permission in E:\xampp\htdocs\Social_Platform\vendor\facebook\graph-sdk\src\Facebook\Exceptions\FacebookResponseException.php:137 Stack trace: #0 E:\xampp\htdocs\Social_Platform\vendor\facebook\graph-sdk\src\Facebook\FacebookResponse.php(210): Facebook\Exceptions\FacebookResponseException::create(Object(Facebook\FacebookResponse)) #1 E:\xampp\htdocs\Social_Platform\vendor\facebook\graph-sdk\src\Facebook\FacebookResponse.php(255): Facebook\FacebookResponse->makeException() #2 E:\xampp\htdocs\Social_Platform\vendor\facebook\graph-sdk\src\Facebook\FacebookResponse.php(82): Facebook\FacebookResponse->deco in E:\xampp\htdocs\Social_Platform\vendor\facebook\graph-sdk\src\Facebook\Exceptions\FacebookResponseException.php on line 137
Posting to the user wall is not possible anymore. The required permission "publish_actions" got deprecated a while ago:
https://developers.facebook.com/docs/graph-api/changelog/breaking-changes#login-4-24
Use the Share Dialog instead: https://developers.facebook.com/docs/sharing/reference/share-dialog

Keep on getting "Invalid appsecret_proof provided in the API argument" error

I'm using v5.0 of Facebook's PHP SDK and have already set up an app.
I would like to get a list of friends via the Graph API using the SDK. To illustrate my implementation, this is the code in which I am trying to get lists of friends on Facebook:
public static function getFriends($access_token)
{
$facebook_app_id = env('FACEBOOK_APP_ID');
$facebook_app_secret = env('FACEBOOK_APP_SECRET');
$facebook = new Facebook([
'app_id' => $facebook_app_id,
'app_secret' => $facebook_app_secret,
'default_access_token' => $access_token,
'default_graph_version' => env('FACEBOOK_API_VERSION') // v2.7
]);
try {
$facebook_request = $facebook->get('me/taggable_friends');
$friends = $facebook_request->getGraphEdge();
$all_friends = $friends->asArray();
if ($facebook->next($friends)) {
while ($friends = $facebook->next($friends)) {
$all_friends = array_merge($friends->asArray(), $all_friends);
}
}
return $all_friends;
} catch (\Exception $e) {
throw $e;
}
}
When the above method is called, it shows a Invalid appsecret_proof provided in the API argument exception message. Digging around, I read that passing a separate parameter of appsecret_proof will get rid of this, however, I've already tried using the App Token of my app taken from the Access Token Tool page to get the value for the appsecret_proof and I still get the error.
And to add, the Require App Secret in my app settings is set to No and yet I still get the same error. Am I missing anything or am I passing the wrong values in getting appsecret_proof?

Connect with facebook using PHP SDK in codeigniter

Im implementing facebook's PHP SDK for authentication here. I've searched over net and found lots of tutorial on that. like Using Facebook PHP SDK 3 with CodeIgniter
i've modified base_facebook.php to return www.skillpaper.com/auth/fb_register in
$this->facebook->getLoginUrl()
when i click "Connect with Facebook" in login modal and facebook auth popup appears its successfully redirect to my given url. i.e www.skillpaper.com/auth/fb_register
and my code is like in auth controller:
public function fb_register(){
$userId = $this->facebook->getUser();
if($userId != 0){
// Get user's data and print it
$user = $this->facebook->api('/me');
var_dump($user);
}else{ echo "No user";}
}
i'm getting "No user" every time!
What am i missing? I don't want to use any JS API for this. any suggestion?
Thanks in advance.
Please Download the Facebook PHP SDK From https://developers.facebook.com/docs/reference/php/
Now Put src folder into application/controller folder.
Call into controller any function like this :-
require 'src/facebook.php';
$appId = FBAPPID;
$secretkey = FBSECRETKEY;
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $secretkey,
));
$fb['facebook']=$facebook;
$facebook_user = $facebook->getUser();
$fb['facebook_user']=$facebook_user;
if ($facebook_user) {
try {
$user_profile = $facebook->api('/me');
//$fb['logoutUrl'] = $facebook->getLogoutUrl();
} catch (FacebookApiException $e) {
error_log($e);
$facebook_user = null;
$this->load->view('Your Page');
}
}else {
$data['loginUrl'] = $facebook->getLoginUrl(array(
'scope' => 'user_about_me,email,user_birthday,friends_birthday,publish_stream,manage_pages,offline_access'
));
$this->load->view('Your Page');
}

Continue the loop if a Fatal Error is found in a Facebook publish stream script

I have the following PHP code:
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXX',
'secret' => 'XXXX',
));
$ids = array('1234','5678','1348','1476');
foreach ($ids as $id) {
$USER_ID = $id;
$args = array(
'message' => 'Hello from XXXXX',
'link' => 'http://www.xxxxx.com/',
'caption' => 'Visit XXXXXXX.com for Facebook API Integration.',
'picture' => 'http://www.xxxxxxx.com/busman.png'
);
$post_id = $facebook->api("/$USER_ID/feed", "post", $args);
}
It works great, it happens that once in a while some users can uninstall or revoke access to the facebook application to publish on his/her behalf, and if that userid is in a loop the entire process stops there and the next users are not touched.
What can I do in order to continue the loop even when I get this fatal error:
Fatal error: Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action thrown in /home/xxxxx/public_html/xxxxx/base_facebook.php on line 1254 <
BTW I am using the PHP SDK for Facebook. Thanks in advance.
Try catching the exception. Something like:
try {
$post_id = $facebook->api("/$USER_ID/feed", "post", $args);
} catch (FacebookApiException $e) {
// Do something with the error if you want.
// For example, remove it from your list so that your
// script doesn't try to use this ID again.
} catch (Exception $e) {
// Any other exception
}
More info on:
Facebook API errors & exceptions (and codes):https://developers.facebook.com/docs/reference/api/errors/
Facebook API error handling with PHP:https://developers.facebook.com/docs/reference/php/facebook-api/
Anatomy on a PHP exception:http://www.php.net/manual/en/class.exception.php
Catching PHP Exceptions:http://php.net/manual/en/language.exceptions.php

Unable to post message to my facebook using php sdk

Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in C:\xampp\htdocs\fyp\plugin\facebook-sdk\src\base_facebook.php on line 1106
The problem is like mentioned above. I have spend sometime on searching the solution but that still not work after i modified the code. I notice this there is website to get one
https://graph.facebook.com/oauth/authorize?
type=user_agent&
client_id=116122545078207&
redirect_uri=http%3A%2F%2Fxyz.com&
scope=user_photos,email,user_birthday,user_online_presence
Can i include it into my script? so that i don't have to going this url everytime
.And how to include it into my phpscript so that the problem can be fixed?
Thank you for any kind of help.
<?require 'plugin/facebook-sdk/src/facebook.php';
$session='123456';
$facebook = new Facebook(array(
'appId' => '1234567',
'secret' => '123456789',
'cookie' => true,
));
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
if ($session) {
// We have a valid FB session, so we can use 'me'
$result = $facebook->api('/me/feed','post',array('message' => 'testmessage'));
} elseif( isset($_SESSION['user_id']) ) {
$result = $facebook->api("/{$_SESSION['user_id']}/feed",'post',array('message' => 'testmessage'));
}
?>
Edit Scope ,
scope=user_photos,email,user_birthday,user_online_presence,publish_stream
& visit this link again
https://graph.facebook.com/oauth/authorize?
type=user_agent&
client_id=116122545078207&
redirect_uri=http%3A%2F%2Fxyz.com&
scope=user_photos,email,user_birthday,user_online_presence,publish_stream

Categories