Facebook page tab changing - facebook-page

I want to change tab name of a facebook page.
Here is the code I am using
<?php
session_start();
$pageId=$_SESSION['pageid'];
require('sdk/facebook.php');
$appId = 'My App Id';
$secret = 'My App Secret';
$pageId = $pageId;
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $secret,
));
$access_token=$facebook->getAccessToken();
if($facebook->setAccessToken($access_token))
{
$page_tabs=$facebook->api($pageId . '/tabs');
$name=$page_tabs['data']['0']['name'];
$tabid=$page_tabs['data']['0']['id'];
if($name=="MyTab")
{
$facebook->setAccessToken($access_token);
$facebook->api($tabid, 'POST', array(
'custom_name' => 'MyTab New Name',
'access_token'=>$_SESSION['token']
));
}
echo "OK";
}
?>
But I am getting error for Oauth Exception , it says you need to supply the accesstoken for this
Please Help

you need few changes
Instead of this
'access_token'=>$_SESSION['token']
use this
//get the user access token
$token = $facebook->getAccessToken();
echo "</br>" . 'User Access_Token:' . $atoken;

Related

WordPress: wp_create_nonce() not creating a value inside custom plugin

I am creating a custom plugin for user registration with the following steps:
A function to create my HTML form fields
function render_registration_form() {
ob_start();
...
<input type="hidden" id="register_nonce" name="register_nonce" value="<?php wp_create_nonce('generate-nonce'); ?>" />
//Above line does not create a nonce at all
...
return ob_get_clean();
}
A function to use as shortcode to display the above form
function custom_user_registration_form() {
//Render registration form only if user is not logged in already!
if(!is_user_logged_in()) {
$registration_enabled = get_option('users_can_register');
if($registration_enabled) {
$output = render_registration_form(); (written above)
} else {
$output = __('User registration is not enabled');
}
return $output;
} else {
return _e('You are already logged in!');
}
}
add_shortcode('register-user', 'custom_user_registration_form');
A function to validate and add a new user along with meta to the database:
function validate_and_create_user() {
if(isset($_POST['txt_username']) && wp_verify_nonce($_POST['register_nonce'], 'register-nonce')) {
$user_data = array(
'user_login' => $loginid,
'user_pass' => $password,
'user_nicename' => $first_name,
'display_name' => $first_name . ' ' . $last_name,
'user_email' => $email,
'first_name' => $first_name,
'last_name' => $last_name,
'user_registered' => date('Y-m-d H:i:s'),
'role' => 'subscriber'
);
$new_user_id = wp_insert_user($user_data);
...
}
add_action('init', 'validate_and_create_user');
Since no nonce value is getting created inside the form the check wp_verify_nonce($_POST['register_nonce'], 'register-nonce') always fails and no user data is getting saved in the database.
According to https://codex.wordpress.org/Pluggable_Functions
Pluggable functions are no longer being added to WordPress core. All new functions instead use filters on their output to allow for similar overriding of their functionality.
I am new to WordPress. Don't have any idea about how to solve this! Any help would greatly help me.
Try This:
wp_nonce_field('register_nonce');
in the place of input.
And for retrieving nonce use following
$retrieved_nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($retrieved_nonce, 'register_nonce')) die( 'Failed security check' );

post from php to my fan page facebook with facebook sdk 5 api 2.4

I read dozens of articles, the guidelines, I read everything but I do not understand anything. I'm going crazy. Are three days that I'm trying to post on my facebook fan page through the last 4 API 2.4 SDK.
1. I created the app on facebook but the permissions are almost impossible to enforce
2. I have created the appropriate PHP code with the various authentication codes
the result is always the same: NOTHING
Then the questions:
1. What do you need the app to publish on my fan page?
2. What permissions are needed?
3. If I do not have screenshots to be indicated in the permit to push them through whatever I do (I do the screen shot of the source code?).
4. as you get the access token to the fan page?
A desperate help.
$APP_ID = 'XXXXXXXXXXXXXXXXX'; //app id
$APP_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; //app secret
$TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; //access token
$page_id = "XXXXXXXXXXXXXXXXXXXXX"; // facebook page id ottenuto da
$message = "Stiamo testando la pubblicazione delle inserzioni anche su Facebook";
$link = "http://qualcosa";
$name = "Me";
/*$fb = new Facebook\Facebook([
'app_id' => $APP_ID,
'app_secret' => $APP_SECRET,
'default_graph_version' => 'v2.4',
]);
$linkData = [
'link' => 'http://qualcosa/altro',
'message' => $message,
];
var_dump($linkData);
$helper = $fb->getPageTabHelper();
$accessToken = $helper->getAccessToken();
var_dump($accessToken);
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/feed', $linkData,$TOKEN);//
} catch(Facebook\Exceptions\FacebookResponseException $e) {
$msg = 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
$msg = 'Facebook SDK returned an error: ' . $e->getMessage();
}
var_dump("MSG: ".$msg);
$graphNode = $response->getGraphNode();
var_dump("Graph: ".$graphNode);
$msg = 'Posted with id: ' . $graphNode['id'];
var_dump($msg);
$msg="Nulla";
// I tryed but nothing
try {
FacebookSession::setDefaultApplication($APP_ID, $APP_SECRET);
$session = new FacebookSession($TOKEN);
var_dump($session);
$page_post = (new FacebookRequest( $session, 'POST', '/'. $page_id .'/feed', array(
'access_token' => $TOKEN,
'name' => $name,
'link' => $link,
'picture' => '',
'caption' => 'Test da Cip!',
'message' => $message,
) ))->execute()->getGraphObject()->asArray();
} catch (Facebook\Exceptions\FacebookResponseException $e)
{$msg = 'Graph returned an error: ' . $e->getMessage();}
catch (Facebook\Exceptions\FacebookSDKException $e)
{$msg = 'Facebook SDK returned an error: ' . $e->getMessage();}
// return post_id, optional
var_dump( $page_post );
var_dump($msg);
echo "<br />Finito";
After many attempts I have solved the problem. In graph explorer serves select the App, then the page on publish, assign publishing rights and withdraw the access token created. In the bottom of the page then you can extend the time validity of the token and you will have to use the latter.

PHP-SDK publish link not available for public

When publishing a new link, using php sdk, the link is only available for logged user, it's not publish as public.
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $secret,
'cookie' => true
));
$user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.
if($user == 0) {
setcookie('cod_eventos', $_POST['cod_eventos']);
$login_url = $facebook->getLoginUrl($params = array('scope' => "publish_stream"));
setcookie('userlogin', 1);
echo ("<script>window.open('".$login_url."')</script>");
} else {
$page_id = $page_id;
try {
$access_token = $facebook->getAccessToken();
$attachment2 = array(
'access_token' => $access_token
);
$page = $facebook->api('/me/accounts', 'get', $attachment2);
} catch (FacebookApiException $e) {
echo 'Unable to get page access token';
}
$privacy = array(
'value' => 'EVERYONE',
);
$attachment = array(
'access_token' => $page['data'][0]['access_token'],
'name' => 'Test',
'link' => 'http://www.facebook.com/',
'description' =>'Facebook',
'privacy' => json_encode($privacy),
);
try {
$facebook->api('/' . $page_id . '/feed', 'POST', $attachment);
echo '<div class="gestor_ficheiros_tipo">Event publish Facebook</div>';
}
catch (FacebookApiException $e)
{
echo '<div class="gestor_ficheiros_tipo">'.$e->getMessage().'</div>';
}
After running this the content is publish, but not view-able by un-registered facebook users
Apparently, despite what the API documentation says, the privacy setting is 'privacy_type', not 'privacy'. I found that out here on SO I think, but closed the tab so I can't immediately share the link.
Check the settings on your app as well - I think it's the 'Visibility of app and posts' setting defaults to 'Friends' instead of 'Public'. Maybe that'll do it?
I found out what was the problem, I was using facebook app in sandbox mode... :(. Once I've changed it started working perfectly

Send request to other server in wordpress

I have two website in wordpress
Main site
1.)http://www.abc.com
Other Site
2.)http://www.xyz.com
From mainsite I want to send username and password to othersite for this i am using this code
$creds = array();
echo $creds['user_login'] = "sachin";
echo $creds['user_password'] = "pass";
$user = wp_signon( $creds, false );
// echo ABSPATH . WPINC;
if ( is_wp_error($user) ){
echo $user->get_error_message();
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
// You would edit the following:
$username = $creds['user_login']; // Twitter login
$password = $creds['user_password']; // Twitter password
$message = "Hey neat, I'm posting with the API";
// Now, the HTTP request:
$api_url = 'http://www.xyz.com';
$body = array( 'status' => $message );
$headers = array( 'Authorization' => 'Basic '.base64_encode("$username:$password") );
$request = new WP_Http;
$result = $request->request( $api_url , array( 'method' => 'POST', 'body' => $body, 'headers' => $headers ) );
// echo"<pre>";print_r($result['body']);
I dont know i am sending wp http request in right way or not but my problem is i want this username and password(i am sending from main site) to other server (http://www.xyz.com)
How I can do that please suggest me.please provide me your valuable suggestion how I can send username and password from one server to other server.
Thanks
To share 2 wordpress installs so that users can access both then this page will let you do that.
http://www.remicorson.com/share-users-database-on-different-wordpress-installs/
I think this is what your trying to do?

Get page_access_token

As I knew there is a way to get access token via https://developers.facebook.com/tools/explorer.
Is there other way to get page_access_token by API?
I want to post request to add app to facebook page but it require a page_access_token as below:
https://graph.facebook.com/myPageId/tabs/?app_id=myAppId&access_token=myPageAccessToken
Thanks.
Yes you can. The current user must be an administrator of this page;
extended permission needed - manage_pages
refer url: https://developers.facebook.com/docs/reference/api/page/
Try this:
$config = array(
'appId' => FB_APP_ID,
'secret' => FB_SECRET_KEY,
'cookie' => true // enable optional cookie support
);
$facebook = new Facebook($config);
// See if there is a user from a cookie
$user = $facebook->getUser();
$params = array(
'scope' => 'manage_pages',
'redirect_uri' => FB_APP_URL,
);
$loginUrl = $facebook->getLoginUrl($params);
<script> window.top.location.href = '<?php echo $loginUrl ?>'; </script>

Resources