can we send Facebook Notifications using app scoped user ids? - php

I am struggling with one problem.
I have to send notification to Facebook Users. According to >=2.0 version of Graph API, we get app scoped ids of user instead of original UIDs.
My question is: can we send notification to user using these new app scoped ids?
As i tried to send notification with app scoped ids, code failed. But When I tried same code with original user id, it worked.
Please let me know if there is any way to solve this problem.
Thanks in Advance.
Here is my code:
require_once "/facebook_api/facebook.php";
$facebook = new Facebook();
$app_id = '<app_id>';
$app_secret = '<secret_id>';
$app_access_token = $app_id . '|' . $app_secret;
$id = '<app-scoped-id>'
$response = $facebook->api( '/'. $id .'/notifications', 'POST', array(
'template' => 'You have received a new message.',
'href' => 'http://test.com',
'access_token' => $app_access_token
));
print_r($response);
die;
RESPONSE:
If is use app-scoped user id, response is:
PHP Warning: Missing argument 1 for Facebook::__construct(), called in /facebook/message.php on line 4 and defined in /facebook/facebook_api/facebook.php on line 47
PHP Notice: Undefined variable: config in /facebook_api/facebook.php on line 51
PHP Fatal error: Uncaught GraphMethodException: Unsupported post request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api
thrown in /facebook/facebook_api/base_facebook.php on line 1283
if i use original user id, result is: Array(['success']=>1);

Currently you can use both the global ID and app scoped ID in your app. Facebook seems to have relaxed this restriction.
If your app scoped ID isn't working, file a bug.

I'm doing the same thing and it also works with real userID, but when I use app scoped id, I get this error :
(#803) Some of the aliases you requested do not exist: ...

Related

Spotify Web API - Client Credentials - Accessing a users playlists

I'm trying to access users playlist tracks by using the client credentials flow.
Spotify getting playlist documentation: https://developer.spotify.com/web-api/get-playlists-tracks/
Spotify getting client credentials documentation: https://developer.spotify.com/web-api/authorization-guide/
First question, is it possible to get a users playlist tracks using client credentials flow? I'm using this flow since I'm unable to pop up a login box for the user to login.
Secondly, I've tried using https://github.com/jwilsson/spotify-web-api-php client credentials flow (Docs: http://jwilsson.github.io/spotify-web-api-php/authorization.html) by practically copying the code at the bottom of that page:
<?php
include('vendor/autoload.php');
$session = new SpotifyWebAPI\Session('Tobys client id', 'Tobys secret', 'http://localhost/callback');
// Request a access token with optional scopes
$scopes = array(
'playlist-read-private',
'user-read-private'
);
$session->requestCredentialsToken($scopes);
$accessToken = $session->getAccessToken(); // We're good to go!
// Set the code on the API wrapper
$api->setAccessToken($accessToken);
$playlists = $api->getUserPlaylists('USER_ID', array(
'limit' => 5
));
foreach ($playlists->items as $playlist) {
echo '' . $playlist->name . ' <br>';
}
This gives me Notice: Undefined variable: api in /var/www/html/dev/testing.php on line 16
I've also tried creating the API variable using $api = new SpotifyWebAPI\SpotifyWebAPI(); but this says I need user information/ tokens.
Thanks.
First question, is it possible to get a users playlist tracks using
client credentials flow?
Yes, retrieving tracks for a playlist doesn't require user authentication as part of the access token.
I've also tried creating the API variable using $api = new
SpotifyWebAPI\SpotifyWebAPI(); but this says I need user information/
tokens.
Looking at the code (Session class, SpotifyWebapi class), it does look like you should set this up by doing
$api = new SpotifyWebAPI\SpotifyWebAPI();
$session = new SpotifyWebAPI\Session($clientId, $clientSecret, $redirectUri);
$api->setAccessToken($session->getAccessToken());
When that's set up you should be good to use the getUserPlaylists method like you're doing in your example code.

How to use facebook access_token

I´m building a facebook app with php,
everything works perfect, I do successful dialog auth
I have the short_live token
I generate the long_live_token and save it to some directory
what I want to do is that in canvas app the user selects some stuff and activates a mechanism that regularly posts stuff, this is why I save the token.
but what can I do with it?!
I find a lot about generating the access_token but nothing about how to use it!?
Where can I add it as parameter? What is the key?
example:
I´m using facebook sdk for php for post sth. to a wall like
$msg_body = array(
'message' => "wassup yo"
);
$facebook->api($uri, 'post', $msg_body );
but this only works if
$facebook->getUser();
is returning a user
how can I use my stored access_token to do the same?
I believe there is a function called "setAccessToken" in the Facebook PHP SDK. You would just need to set it with that function and it gets added to every call automatically.
Manual way:
$params = array(
'message' => 'wassup yo',
'access_token' => '[your-token]'
);
$facebook->api($uri, 'post', $params);
You could also do this with CURL, this would be an example URL;
$url = 'https://graph.facebook.com/' . $userId .
'/feed' .
'&access_token=' . $accessToken .
'&message=' . $userMessage;
Basically you just add the Access Token as a parameter like the message.
Just make sure you are using secure calls, see this article for an example of using CURL with the Facebook API and usage of "appsecrect_proof": http://www.devils-heaven.com/extended-page-access-tokens-curl/
IMPORTANT: Be sure that the message parameter is always 100% user generated without any prefilling (see Platform Policy) and keep in mind that you need to go through a review process with pulish_actions to make it available for other Users: https://developers.facebook.com/docs/apps/changelog

Retrieving messages from status table in FQL using PHP SDK

I am developing a php script for retrieving messages from a page (not user) in facebook. I have access to that page (it is mine) so if I must to modify something in config, I can.
I tried the FQL query in graph api webpage from Facebook and it works. I get the file with all messages.
But whenever I try it in my php script, it fails. I am trying it in a localhost server and calling the script : "localhost/testServer/phpScript.php" I don't know if I have to add a parameter.
I have checked many examples and tutorials and all are similar but in my case, they don't work.
I always get an exception with this message:
Fatal error: Uncaught Exception: 100: Requires user session thrown in C:\xampp\htdocs\testServer\base_facebook.php on line 1271
If I surround it in try/catch and print what I get, it tells it is an exception with getType() and an array with getResult().
My code is quite simple:
<?php
require_once("facebook.php");
//Get Facebook SDK Object
$config = array(
'appId' => '*******',
'secret' => '*******',
'cookie' => true,
);
$facebook = new Facebook($config);
//$fql = "SELECT message FROM status WHERE uid = 169070991963 ORDER BY time DESC";
$fql = "SELECT message FROM status WHERE uid = 169070991963";
//Create Query
$param = array(
'method' => 'fql.query',
'query' => $fql,
'access_token'=>'*******|******-*******',
'ext_perm' => "read_stream",
);
$result = $facebook->api($param);
print_r($result);
?>
I just deleted my appId, secret and access_token.
I read something about read_stream permissions using status table. But adding access_token and ext_perm tags has no effect. I leave the uid of the webpage because it is public so you can test the code on your own.
If I can't do this query with php fql, I have no problem in using normal api if I can get the same result.
I am expending many days doing this script and I am completely lost with the problem.

Facebook - Retrieving recent posts from FB API in PHP

I am working on a script to pull recent Facebook posts/status updates for a specific using the Facebook API. I downloaded the FB SDK and searched all over the FB developers site, SO, and other places and it seems like the code below should accomplish what I need. However, although I am not getting an error, I am getting a NULL result.
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
));
$fbApiGetPosts = $facebook->api('***My Name***/feed?limit=5');
var_dump($fbApiGetPosts["data"]);
I have tried using my two FB accounts,(my personal acct and a test account that I created just for this project) as well as one for my company and each time the response was NULL. When I misspelled the account name or used a name that did not exist I got an error, which seems to suggest the request is going somewhere.
In the code above, I also tried var_dump-ing $fbApiGetPosts and that result was also null
Can anybody see what the missing piece of the puzzle is?
I've not used the Facebook SDK, but here is how I retrieve Facebook posts:
$appId = 'XXXXXXXXXXXX';
$token = 'XXXXXXXXXXXXXXXXXXXXXXXX';
$posts = json_decode(
file_get_contents('https://graph.facebook.com/' . $appId . '/feed?
access_token=' . $token
)
);
var_dump($posts->data);
Hopefully this helps!
EDIT:
The token is just called an "Access Token". Here are the instructions for obtaining one. You might find the Access Token Tool helpful, but it's more for debugging existing tokens then creating new ones.
Just use the PHP SDK :
download link : https://developers.facebook.com/docs/php/gettingstarted/
Log your app :
$facebook = new Facebook(array('appId' => 'XXXXX','secret' => 'XXXXX'));
$facebook->setAccessToken('stored access token');
then :
$fbApiGetPosts = $facebook->api('/me/feed');
print_r($fbApiGetPosts);
Here is the Graph doc : https://developers.facebook.com/docs/graph-api/reference/
and the /feed ref : https://developers.facebook.com/docs/graph-api/reference/user/feed/

Facebook OAuthException: "user hasn't authorized the application to perform this action"

Using the Facebook PHP SDK, I'm getting the following error when I try to post a status update:
Fatal error: Uncaught OAuthException:
(#200) The user hasn't authorized the
application to perform this action
These are the steps I've taken:
Get code:
https://graph.facebook.com/oauth/authorize?client_id=FB_APP_ID&redirect_uri=REDIRECT_URI
Get access token:
https://graph.facebook.com/oauth/access_token?client_id=FB_APP_ID&code=CODE&client_secret= FB_SECRET&redirect_uri=REDIRECT_URI
Attempt the status update:
require_once(facebook.php);
$fb = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_SECRET
));
$post = $fb->api('me/feed', 'POST', array(
'access_token' => ACCESS_TOKEN,
'message' => 'hello world!'
));
I don't see any settings in my application that would authorize the application to do this, but maybe I'm missing something. Any suggestions?
Make sure you ask for extended publish_stream permission when you're requesting code (added as the third parameter):
https://graph.facebook.com/oauth/authorize?client_id=' . FB_APP_ID . '&redirect_uri=' . REDIRECT_URI . '&scope=publish_stream'
Hope this helps.
Cheers!
I had the same problem and this post really helped me out http://facebook.stackoverflow.com/a/8502709/965536
The only difference with my problem was that I was using the PHP SDK but essentially it works the same. I used the api call
$permissions = $facebook->api('/me/permissions');
You can then run your checks
if(isset($permissions['data'][0]['publish_stream']) && $permissions['data'][0]['publish_stream'])
This works for me but someone may have a better answer. Also you should wrap your publish post stream in a try catch
Hope this helps.
Thanks
I put here some more information:
Mark's replied above has lead me to the right direction. But it took me another 5 hours to figure out the solution.
I'm using omniauth-facebook for ruby on rails.
I need to set the scope for omniauth (Please refer to https://github.com/mkdynamic/omniauth-facebook)
I also ready this post from "Lu Chen" to set the right scope request
http://developers.facebook.com/blog/post/2012/04/25/streamlining-publish_stream-and-publish_actions-permissions/
So, here is the result for omniauth.rb:
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
:scope => 'email,user_birthday,publish_actions'
Have you taken the other steps required to connect the user with your app and get them to authorize your app to perform these actions? You need to register the users then call showPermissionDialog to let them log in and authorize your app. That's what this error is telling you.

Categories