Using PHP-SDK to get the album details - php

I'm tried get the details of an album using the PHP-SDK and facebook class, using the following code:
$data = $facebook->api('me/albums');
the code above return an empty array.
But if I try get the friends:
$friends = $facebook->api('/me/friends');
works fine. Why? the permissions are same.
in 'pure' mode:
$url = "https://graph.facebook.com/me/albums/?access_token={$_SESSION['access_token']}";
$res = curl($url);
$json = json_decode($res);
too works fine.

to get the alubms details of the currently logged-in user, you need to request for the "user_photos" permission too.

You have to have "user_photos" permission to access this resource.
Make sure the access_token you are using takes care of that.

Related

Using PHP and want to get the Facebook access token in PHP (without SDK)

My code as below :
//Tie it all together to construct the URL
$url = "https://graph.facebook.com/oauth/access_token?client_id=[ID]&client_secret=[key]&grant_type=client_credentials";
//Make the API call
$result = file_get_contents($url);
//Decode the JSON.
$decoded = json_decode($result, true);
//Dump it out.
var_dump($decoded);
var_dump($result);
I am not using PHP SDK (Facebook) and it doesn't work....
I run the same link in browser and it can return the access token in JSON.
I assume you found the URL here: https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
DonĀ“t forget about the code parameter. You get it when the user authorizes your App. This is what you need: https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#login

graph api notifications returns empty data

im building a facebook app and i want to notify the user
https://developers.facebook.com/docs/games/notifications
im using facebook php sdk
what i do:
user auths the app and accepts permission
i get the accesstoken like:
$facebook->getAccessToken()
and then i generate a long-time token like:
public function generateLongTimeToken($token){
$long_time_token_req_body = array(
"grant_type"=>"fb_exchange_token",
"client_id"=>$this->facebookOptions["appId"],
"client_secret"=>$this->facebookOptions["secret"],
"fb_exchange_token"=>$token
);
$query = http_build_query($long_time_token_req_body);
$lttreq = file_get_contents("https://graph.facebook.com/oauth/access_token?".$query);
$lttresp = parse_str($lttreq, $output);
if ( array_key_exists("access_token", $output)){
$this->logger->info("Facebook-app: Successfuly generated long_time_token");
return $output["access_token"];
}else {
$this->logger->err("Facebook-app: generating oauth long_time_token failed \n".$lttreq);
return false;
}
}
some later i use this token for background processes to post on the users wall and them all work fine
now i also want to notificate the user like that :
public function notifyUser($message,$facebookClientId,$token){
$appsecret_proof= hash_hmac('sha256', $token, $this->facebookOptions["secret"]);
$req_body = array(
"access_token"=>$token,
"appsecret_proof"=>$appsecret_proof,
"href"=>"/index",
"template"=>$message,
"ref"=>"post"
);
$query = http_build_query($req_body);
$url = "https://graph.facebook.com/".$facebookClientId."/notifications?".$query;
$lttreq = file_get_contents($url);
return $lttreq;
}
but when i try to notify the user i always get empty data back
when i open the url in browser with all parameters facebook returns the same
{
data: [ ]
}
so i have no idea whats going on,when i look on SO i only find about people posting to sites but i want to notify the user itself
thanks for any help
First, from the Facebook docs:
Currently, only apps on Facebook.com can use App Notifications.
Notifications are only surfaced on the desktop version of
Facebook.com.
Also, an App Token is needed, not a User Token.
Btw, file_get_contents is very bad, use CURL for Facebook. May be another reason why it does not work. A basic example of using CURL with the Facebook API: http://www.devils-heaven.com/extended-page-access-tokens-curl/
Additional Info: I recently wrote a blogpost about App Notifications, it is in german but the small code part may be interesting for you: http://blog.limesoda.com/2014/08/app-notifications-facebook-apps/

Facebook Graph API - Issues with friends that have installed the app

I have an android client, that initiate log-in to facebook, receives access token and other details about the profile.
The android client passes the access_token and details to the Server (PHP).
Both have facebook sdk installed.
When I initiate a FacebookRequest from the PHP, for example '/me/' It's working.
BUT when I initiate a (friends who have installed the APP) FacebookRequest from the PHP
'/me/friends'. I get "null".
When I use the graph explorer provided by facebook the result is :
{
"data": [
]
}
Additional information for the helpers:
The app contains only two users at the moment, which are friends on facebook.
Those users are both administrators of the app.
Currently the app is not live\published.
We haven't "Start a Submission" in Stats and Reviews as suggested somewhere.
We asked for the permission 'user_friends'.
Since everything in stack overflow requires reputation,
This is how the permissions look like:
http://i.stack.imgur.com/kURwB.png
Thanks
EDIT:
OK I added two test users, Made them friends of each other, and /me/friends through graph explorer is working for them.
BUT why doesn't it work for non-test-users?
Okay,
So apparently Facebook had a problem providing new Access Tokens.
Don't know why, but it provided the same access token, for new Log-Ins.
Now that they fixed that here is a Handy function that I wrote in PHP that will let users
Parse Facebook responses, this might come in hand since there is NOTHING explained over the Docs, Google, StackOverFlow.
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
function find_my_facebook_friends($access_token){
require_once 'facebook-php-sdk-v4-4.0-dev/autoload.php';
FacebookSession::setDefaultApplication('app_key','secret_key');
$session = new FacebookSession($access_token);
$request = new FacebookRequest(
$session,
'GET',
'/me/friends'
);
$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();
return $graphObject;
}
Usage:
include_once 'path/to/function.php';
//You wanna use it, just so the PHP file contains your app_key, secret_key Wont be exposed.
$access_token = isset($_POST['access_token']) ? $_POST['access_token'] : NULL;
$facebook_object = find_my_facebook_friends($access_token);
for($i = 0 ; $i < count($facebook_object['data']) ; $i++){
$id = get_object_vars($facebook_object['data'][$i]);
echo $id['id'];
echo $id['name'];
//You don't have to echo, you could handle those fields as you wanted.
}
This function refers to a Mobile Client passing his Log In User's access token to the server.
I hope this will save you lots of time!

Twitter Oauth Statuses/show in codeigniter returns error

I am using this library for implementing twitter Oauth in my project,
and codeigniter if that is relevant in this case.
I am trying to get a tweet based on the tweet id, but it's returning an error:
{"errors":[{"message":"Sorry, that page does not exist","code":34}]}
Here is the code I am using:
public function gettweetbyid() {
$this->config->load('twitter');
$consumer = $this->config->item('twitter_consumer_token');
$consumer_secret = $this->config->item('twitter_consumer_secret');
$access_token = $this->config->item('twitter_access_token');
$access_token_secret = $this->config->item('twitter_access_secret');
$connection = $this->twitteroauth->create($consumer, $consumer_secret, $access_token, $access_token_secret);
$content = $connection->get('account/verify_credentials');
$data = array(
'id' => $this->input->get('id')
);
$tweets = $this->twitteroauth->get('statuses/show', $data);
echo json_encode($tweets);
}
I know everything is right because I am using the plugin as well to get tweets based on a hashtag, and this is no problem at all.
The URl I am getting looks very similar to their docs url:
https://api.twitter.com/1.1/statuses/show.json?id=408541017676460000&....
I'm looking at this for a whole day now and my time is running out...
Any help would be appreciated!
Edit: Also, is there any way on twitter I can check if the ID actually exists? Altho I don't think that is the problem, I'd like to know for future reference
For those people stumbling on this:
I get the id from a previous request to twitter, and I found out you have to use the id_str from the json data twitter returns, and not the normal id. the string id and normal id are different. God may know why, but I'm just glad this got solved!

PHP: file_get_contents function does not work in graph api facebook

I am trying to retrieve facebook uesr ID via graph from below code
$post = "https://graph.facebook.com/me?access_token=".$access_token;
$response = file_get_contents($post);
I get the following error:
Warning: /me cannot be read [file_get_contents] https://graph.facebook.com/me?access_token=AAIvGrJsBAI3nLAbfaqyh8NExN5e21yKCvJAjVOGJ9ira1RdCDHRFszktHnDCY44urQtxdBHMKEsX1K7QAUWXSYF5ip3l68JI8xKeopjYdssds
But when i try to access the above graph URL via browser, its working correctly. So my access token is valid.
Note: I am using google app engine so I can't use cURL.
Use cURL or this class: http://ideone.com/Q1Zfl
I've used this class many times and it behaves well.
Usage:
$url = '...';
$req = new Request($url);
$data = $req->DownloadToString();
P.S I hope the link does not expire
You can use
$content = #file_get_contents($site);
To hide the error. If it's dont work. You go to the ini.php file and find the line
Allow_url_fopen = Off
Change "off" to "on"
Allow_url_fopen = On

Categories