how to create a score in facebook application? - php

from facebook docs it says:
"You can post a score or a user by issuing an HTTP POST request to /USER_ID/scores with the app access_token as long as you have the publish_actions permission."
well i have publish_actions permission and i'm trying the following code but it doesn't work...
require('php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => '123546321654654654654',
'secret' => 'd9132156afasdfadsfsdfewfg79f5f16d0',
'cookie' => true
));
$attachment = array('score' => 1005);
$postscore = $facebook->api('/12345612/score/','POST',$attachment);

You need to send your app access token in the attachment array. So simply update your array attachment to
$attachment = array( 'score' => 1005, 'access_token' => $facebook->getAppId().'|'.$facebook->getApiSecret() )
$postscore = $facebook->api('/12345612/score/','POST',$attachment);

I'm not familiar with php but link for posting the score is "/USER_ID/scores/". You have forgotten one 's' in the end of the scores.

Related

Why can't I add an event as a page?

I'm trying to add an event to a page in the name of the page. I'm the admin of the page and I found a lot of stuff to the topic, but, no matter what I do, it ends in the PHP Exception:
Uncaught Exception: You must be an admin of the specified page to perform the requested action.
I can add events with my app in my own profile. So, I think the problem is the Access Token. I tried several ways, but to me the best way seems to be:
Open the Graph API Explorer
Select my Application
Request a new access code with permissions manage_pages, publish_actions, publish_stream, create_event
Request /me/accounts
Get the access_token from the page I want to post to and use it in my script
My test script, that works in my own profile:
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => $APPLICATION_ID,
'secret' => $APPLICATION_SECRET,
'cookie' => false,
'fileUpload' => true
));
$event_id = 0;
$event_name = "New Event API Test Event";
$event_start = '2013-12-04';
$event_privacy = "OPEN";
$ret_obj = $facebook->api('/'.$page_id.'/events?access_token='.$access_token, 'POST',
array(
'name' => $event_name,
'start_time' => $event_start,
'privacy_type' => $event_privacy,
'location_id' => $location_id
));
if(isset($ret_obj['id'])) {
echo 'Event with added. ID: ' . $ret_obj['id'];
} else {
echo 'Couldn\'t create event';
}
What I did wrong?
You need a page access token.
Page Access Token – These access tokens are similar to user access
tokens, except that they provide permission to APIs that read, write
or modify the data belonging to a Facebook Page. To obtain a page
access token you need to start by obtaining a user access token and
asking for the manage_pages permission. Once you have the user access
token you then get the page access token via the Graph API.
Doc: https://developers.facebook.com/docs/facebook-login/access-tokens/
Then use GET /me/accounts to get the access token associated to your page.
I found the solution in How to programmatically add an event to a page using Graph API?. I had to add the access_token direct into the event array and everything works as expected:
$ret_obj = $facebook->api('/'.$page_id.'/events', 'POST',
array(
'access_token' => $access_token,
'name' => $event_name,
'start_time' => $event_start,
'privacy_type' => $event_privacy,
'location_id' => $location_id
));
Thanks for the help!

Facebook sdk using php: how to get theuserid of the connected user

how can i get the current logged in userid ?
im pretty sure my appId is correct
$facebook = new Facebook(array(
'appId' => '...',
'secret' => '...',
));
im using this method but i returns 0;
$facebook->getUser();
If you want to get facebook user id with this code
$facebook = new Facebook(array(
'appId' => 'your_app_id',
'secret' => 'your_app_secret',
));
$user = $facebook->getUser();
You have condition to should have done before, the first condition is you put that code in the page that redirected after the user login via fb provide by this code
$params = array(
'display'=>'popup',
'redirect_uri' => 'http://example.com'
);
$loginUrl = $facebook->getLoginUrl($params);
OR the second condition is you already have the user 'Access Token', so the code will be like this
$facebook = new Facebook(array(
'appId' => 'your_app_id',
'secret' => 'your_app_secret',
));
$facebook->setAccessToken('user_access_token');
$user = $facebook->getUser();
If you don't clear about the Facebook Login flow using PHP SDK, you can visit this page
$myData=$facebook->api('/me');
$myid=myData['id'];
$myName=myData['name'];
$myUsername=myData['username'];
[id] => your id
[name] => your full name
[first_name] => your firs name
[last_name] => lastname
[link] => profil link
[username] => username
[gender] => your gender (male,female)
[email] => your mail (if have email permission)
[timezone] => 2
[locale] => your locale
[verified] => 1
Follow this guide: Login for Server-side Apps. Basically, you have to generate a login URL, receive a code generated by Facebook, and with this code, request for an access token.

Facebook Graph Api - Posting to page without logging

I'm trying to create a script that post status updates on facebook pages without logging-in the user. I've got access_token for my application and for the pages as the user granted the application once; but I always get the error
Fatal error: Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action
despite I've granted manage_pages and publish_stream permission.
Also if I use the access_token for the page I get the error
Fatal error: Uncaught OAuthException: Error validating access token: The session is invalid because the user logged out.
Here is the code I use.
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXXXXXXXXXX'
));
$post = array(
'message' => 'Message to user'/*,
access_token = "XXXXXXXX"*/
);
$feed = '/[PAGE_ID]/feed';
$post_id = $facebook->api($feed, "post", $post);
Thanks.
Edit :
I finally managed to post status updates on a user's page while this user was disconnected, I had to generate the access_token through the server-side auth method.
Below is a small code smaple on how to do a FB post to a page:
yOU NEED:
Your_authToken_for_User
List item
saved_fb_Page_Token
$facebook = $facebook = new Facebook(array(
'appId' => $fbAppId,
'secret' => $fbAppSecret,
'cookie' => true,
));
$facebook->setAccessToken("Your_authToken_for_User");
//Now your FB instance is authenticated.
$pages= $facebook->api('/me/accounts'); //use the current authenticated FB instance to grab Pages. If you already know your page ID , skip this.
//Assuming you have a pageid in $page_id,(the $page_id can be grabbed from the $pages array).
$attachment = array(
'access_token' => $saved_fb_Page_Token,
'message' => "foo",
'name' => "Bar",
'link' => "http://foo.bar",
'description' => "barfoo",
'picture' => "an image url here",
);
$facebook->api('/' . $page_id . '/feed', 'POST', $attachment);//post to fb page

Update Facebook wall with PHP

I have a blog system where I want the user to be able to post on there Facebook wall without them having to login to Facebook. I would like it to happen through the PHP SDK delivered by Facebook.
This is the permissions I have given the Facebook application i created:
https://www.facebook.com/dialog/oauth?client_id=xxxxxxxx&redirect_uri=http://www.mydomain.com/facebook/index/&scope=offline_access,publish_stream,read_stream,manage_pages
Just to clarify the permissions: offline_access,publish_stream,read_stream,manage_pages
Below codes only works if the user is logged in. else I get this error:
FacebookApiException [ 0 ]: An active access token must be used to query information about the current user.
public function facebook()
{
require_once('facebook/php-sdk-3.0.0/src/facebook');
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
$attachment = array(
'access_token' => $facebook->getAccessToken(),
'message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'http://www.google.com',
'description' => 'Test of application PHP',
'picture' => 'http://www.lalibre.be/img/logoLaLibre.gif',
'actions' => array(array(
'name' => 'Get Search',
'link' => 'http://www.google.com')
)
);
$result = $facebook->api('/me/feed/','post',$attachment);
print_r($facebook->getAccessToken());
}
In order to post to someone's wall your application must have wall post permissions for that user. In order to get those permissions the user must login to facebook and grant you access.
For more info: http://developers.facebook.com/docs/authentication/
Also, you need "offline_access" if you want to post when's the user is not logged in.

Post to Facebook Page Wall

I'm just trying to make PHP Connector to Facebook for posting wall posts on a Page.
So i'm not trying to post wall post to profile wall or anything else.
I've read some tutorials and manuals and i decided to use Facebook PHP-SDK (from Naitik Shah)
https://github.com/facebook/php-sdk/
I've created Facebook App to post wallposts through it. I received appId and api secret. I've added application permissions to my Page and tried example code
$facebook = new Facebook(array(
'appId' => 'my app id',
'secret' => 'my api secret',
'cookie' => false,
'domain' => 'domain.com'
));
domain.com => domain from which i'm sending api requests
next ->
$facebook->getSession();
$token = $facebook->getAccessToken();
$facebook->api('/123456789/feed', array(
'access_token' => $token,
'link' => 'http://www.example.com'
));
So i'm trying to post link on wall of page with id 123456789
The request goes through without warnings/errors but nothing is posted in right place and nothing is returned.
Thanks for any idea about this problem.
Used tutorials:
How do you post to the wall on a facebook page (not profile)
http://blog.theunical.com/facebook-integration/5-steps-to-publish-on-a-facebook-wall-using-php/
http://www.moskjis.com/other-platforms/publish-facebook-page-wall-from-your-site
http://tips4php.net/2010/12/automatic-post-to-facebook-from-php-script/
$facebook->api('/123456789/feed', 'post', array(
'access_token' => $token,
'link' => 'http://www.example.com'
));
Note the 'post' part.
If you look at the source for the API via the link you provided, you'll see:
protected function _graph($path, $method='GET', $params=array()) {
if (is_array($method) && empty($params)) {
$params = $method;
$method = 'GET';
}
When you don't have 'post' as the second argument and your array as the third, it goes a get
If you are getting authorisation errors, make sure you have included the following permission:
publish_stream
https://developers.facebook.com/docs/reference/api/post/

Categories