How to validate Facebook App ID - php

I need to check if the given Facebook app id is valid. Also, I need to check which domain and site configurations are set for this app id. It doesn't matter if it's done through PHP or Javascript.
I checked everywhere but couldn't find any information about this. Any ideas?

You can validate the ID by going to http://graph.facebook.com/<APP_ID> and seeing if it loads what you expect. For the app information, try using admin.getAppProperties, using properties from this list.

Use the Graph API. Simply request:
https://graph.facebook.com/<appid>
It should return you a JSON object that looks like this:
{
id: "<appid>",
name: "<appname>",
category: "<app category>",
subcategory: "<app subcategory>",
link: "<applink>",
type: "application",
}
So, to validate if the specified app_id is indeed the id of an application, look for the type property and check if it says application. If the id is not found at all, it will just return false.
More info: https://developers.facebook.com/docs/reference/api/application/
For example:
<?php
$app_id = 246554168145;
$object = json_decode(file_get_contents('https://graph.facebook.com/'.$app_id));
// the object is supposed to have a type property (according to the FB docs)
// but doesn't, so checking on the link as well. If that gets fixed
// then check on isset($object->type) && $object->type == 'application'
if ($object && isset($object->link) && strstr($object->link, 'http://www.facebook.com/apps/application.php')) {
print "The name of this app is: {$object->name}";
} else {
throw new InvalidArgumentException('This is not the id of an application');
}
?>

Use the Graph API:
$fb = new Facebook\Facebook(/* . . . */);
// Send the request to Graph
try {
$response = $fb->get('/me');
} 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;
}
var_dump($response);
// class Facebook\FacebookResponse . . .
More info:FacebookResponse for the Facebook SDK for PHP

Related

Parse PHP current user update failed but session updated

In Parse PHP SDK If the current user want to change his Email/username normally Parse check if username used by other user and if it is used it will return error, now this is good and work perfectly so far but the issue is the session is automatically updates to the new value where it failed.
so basically the session for the current user updated even if it wasn't for the backend
Steps to reproduce
$currentUser = Parse\ParseUser::getCurrentUser();
echo "Current Username is : ". $currentUser->get("username");
if ($currentUser) {
$currentUser->set("username", "ww");
try {
$currentUser->save();
echo "UPDATED";
} catch (Parse\ParseException $er) {
$ex = $er->getMessage();
echo "<br> Error: ". $ex;
}
}
here is a video that explains more:
https://youtu.be/KWS9fW5MReA
Since you have updated the object in your PHP application, it will keep updated locally unless you reset the action. So you can either:
save the old username and reverse the action in your catch method; or
use $currentUser->fetch() in your catch method; or
instantiate a new user object, do the change attempt in this new object, and finally $currentUser->fetch() only in case of success.
See below one of the possible solutions:
$currentUser = Parse\ParseUser::getCurrentUser();
echo "Current Username is : ". $currentUser->get("username");
if ($currentUser) {
$currentUser->set("username", "ww");
try {
$currentUser->save();
echo "UPDATED";
} catch (Parse\ParseException $er) {
$currentUser->fetch();
$ex = $er->getMessage();
echo "<br> Error: ". $ex;
}
}

Facebook SDK not catching exceptions

I'm using the Facebook Graph API SDK in my Laravel 5.4 app ("facebook/graph-sdk": "~5.0" in composer)
I'm trying to fetch some fields from a page and when the page is invalid it throws an error, however it doesn't seem to catch the error properly:
Code below:
use Facebook\Facebook;
class FacebookUser extends Controller
{
try {
echo 'Trying ' . $venue->id;
$response = $fb->get('/'.$page.'/locations?fields=hours', $access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}
This isn't ideal as it stops every time it hits a snag with the following error:
In FacebookResponseException.php line 106:
(#100) Tried accessing nonexisting field (hours) on node type (URL)
The exception is most likely named Facebook\Exceptions\FacebookResponseException.
Because you have:
use Facebook\Facebook;
which is:
use Facebook\Facebook as Facebook;
you are ending up trying to catch this class:
Facebook\Facebook\Exceptions\FacebookResponseException
You probably want to adjust your catch like so:
} catch (\Facebook\Exceptions\FacebookResponseException $e) {

Laravel 5 Parse

I have the following problem i'm using laravel 5 and laraparse package.Login with parse works without a problem,also things like insert categories works.The problem is for sign up i'm using ParseUser().I use the following code from parse docs for sign up:
$user = new ParseUser();
$user->set("username", $request->username);
$user->set("email", $request->email);
$user->set('isArtist', $isArtist);
$user->set("password", $request->password);
try {
$user->signUp();
return redirect('profile');
} catch (ParseException $ex) {
// Show the error message somewhere and let the user try again.
echo "Error: " . $ex->getCode() . " " . $ex->getMessage();
}
but it returns the following error:
You must specify a Parse class name or register the appropriate subclass when creating a new Object. Use ParseObject::create to create a subclass object.
The keys in config are ok because login and everything else works so the keys are not the problem.

Soundcloud API Check if a user is following another user

I'm trying to figure out if a user is following another user on Soundcloud using the Soundcloud API and php.
So far I came across a solution which would either return an object (user) or a 404 error:
$test = json_decode($client->get('/users/{id1}/followers/{id2}'));
I've tried it multiple times with different user IDs but I always receive a the following error message:
'Services_Soundcloud_Invalid_Http_Response_Code_Exception' with message 'The requested URL responded with HTTP code 404.'
I know that this is supposed to be the error message which informs me that user2 is not following user1. However I've tried this snippet with ids where I know a reciprocal following exists for sure.
Any suggestions on how this can be solved?
Update (21.05.15):
I've read through some of the Soundcloud documentation and cam across a code snippet:
<?php
require_once 'Services/Soundcloud.php';
// create a client object with access token
$client = new Services_Soundcloud('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET');
$client->setAccessToken('YOUR_ACCESS_TOKEN');
// Follow user with ID 3207
$client->put('/me/followings/3207');
// Unfollow the same user
$client->delete('/me/followings/3207');
// check the status of the relationship
try {
$client->get('/me/followings/3207');
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
if ($e->getHttpCode() == '404')
print "You are not following user 3207\n";
}
?>
This is pretty much what I was referring to. However if I open a php page with this script the result is always one of three cases:
You are not following user 3207 (expected output)
No output (I'm following the user)
Uncaught exception 'Services_Soundcloud_Invalid_Http_Response_Code_Exception' with message 'The requested URL responded with HTTP code 404.'
The third option is either referring to $client->put or $client->delete
Here is how i would do this:
<?php
require_once 'Services/Soundcloud.php';
$client = new Services_Soundcloud(
'xxxxxxxxxxxxxxxxxxx160', 'xxxxxxxxxxxxxxxxxx34dd1 ');
$userid = 1672444;
$followerid = 383228;
$yesno = '';
try {
$response = json_decode($client->get('users/'.$userid.'/followers'), true);
$yesno = IdInArray($response, $followerid);
echo $yesno;
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
exit($e->getMessage());
}
function IdInArray($response, $followerid){
echo $followerid.'<br/>';
for($i = 0; $i < count($response); ++$i) {
if($response[$i]['id'] == $followerid){
return 'yolo';
}
else{
return 'nolo';
}
}
}
?>

Recieving "instance of __PHP_Incomplete_Class given" when storing the Facebook session in the PHP $_SESSION array

I am attempting to implement the server side PHP facebook authentication code, to go with my front end Javascript Facebook authentication code.
For this, I am grabbing the current facebook session, storing it into the PGP $_SESSION array, and then using it to make a call to get the user's username.
On the 2nd time into the side, i check to see if we have the stored facebook session in the PHP $_SESSION array, and if so, I use that again to make the call to get the user name. This is where i recieve the "instance of __PHP_Incomplete_Class given" error message, when trying to use the stored session.
The reason i want to store the facebook session between page refreshes is that whenever i call $session = $helper->getSession(); a second time after the user logs in, i receive a message "this authorization code has already been used". I read somewhere that you need to store the facebook session, instead of calling for a new one each refresh, as the facebook token was already consumed in getting you the first session object.
Can anyone help me figure out how to store the facebook session in the PHP $_SESSION array correctly, and be able to pull it back out, and use it to make calls to facebook?
I am using the latest facebook JS sdk and the latest facebook PHP sdk.
CODE:
<?php
session_start();
require_once ('facebookphpsdkv4/src/Facebook/GraphObject.php');
require_once ('facebookphpsdkv4/src/Facebook/GraphUser.php');
require_once ('facebookphpsdkv4/src/Facebook/GraphSessionInfo.php');
require_once ('facebookphpsdkv4/src/Facebook/Entities/AccessToken.php');
require_once ('facebookphpsdkv4/src/Facebook/FacebookSession.php');
require_once ('facebookphpsdkv4/src/Facebook/Entities/SignedRequest.php');
require_once ('facebookphpsdkv4/src/Facebook/FacebookSignedRequestFromInputHelper.php');
require_once ('facebookphpsdkv4/src/Facebook/FacebookJavaScriptLoginHelper.php');
require_once ('facebookphpsdkv4/src/Facebook/HttpClients/FacebookHttpable.php');
require_once ('facebookphpsdkv4/src/Facebook/HttpClients/FacebookCurl.php');
require_once ('facebookphpsdkv4/src/Facebook/HttpClients/FacebookCurlHttpClient.php');
require_once ('facebookphpsdkv4/src/Facebook/FacebookResponse.php');
require_once ('facebookphpsdkv4/src/Facebook/FacebookRequest.php');
require_once ('facebookphpsdkv4/src/Facebook/FacebookSDKException.php');
require_once ('facebookphpsdkv4/src/Facebook/FacebookRequestException.php');
require_once ('facebookphpsdkv4/src/Facebook/FacebookAuthorizationException.php');
useFacebookGraphObject;
useFacebookGraphUser;
useFacebookGraphSessionInfo;
useFacebookEntitiesAccessToken;
useFacebookFacebookSession;
useFacebookEntitiesSignedRequest;
useFacebookFacebookSignedRequestFromInputHelper;
useFacebookFacebookJavaScriptLoginHelper;
useFacebookHttpClientsFacebookHttpable;
useFacebookHttpClientsFacebookCurl;
useFacebookHttpClientsFacebookCurlHttpClient;
useFacebookFacebookResponse;
useFacebookFacebookRequest;
useFacebookFacebookSDKException;
useFacebookFacebookRequestException;
useFacebookFacebookAuthorizationException;
FacebookSession::setDefaultApplication('AppID', 'AppSecret');
if (isset($_SESSION['session']))
{
echo 'session set';
$session = $_SESSION['session'];
}
else
{
$helper = new FacebookJavaScriptLoginHelper();
try
{
$session = $helper->getSession();
$_SESSION['session'] = $session;
}
catch(FacebookRequestException $e)
{
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
catch(Exception $e)
{
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
if ($session)
{
try
{
$user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
echo "Name: " . $user_profile->getName();
}
catch(FacebookRequestException $e)
{
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
?>
You have an __PHP_Incomplete_Class error because you get your sessions (with session_start() call) before including you class files.
There are objects of a type unknown by PHP (as your requires are after your session_start()) in your session variables, so PHP don't know how to "rebuild" theses objects.

Categories