location_id for Facebook event creation - facebook-graph-api

I am trying to use location_id , which I retrieved from my current location, as a parameter for Facebook event creation, here is my code:
I have set up all the authorization token and extended permission , and it indeed returned a valid location id; however, after I plug the location_id into the event creation parameter array, it did not create an event. (all the other fields are set through a form).Could anyone help?
Here is my code:
try{
//default location
$event_url = $my_url . 'create_group.php';
$event_location = $facebook->api('/me?fields=location');
$event_location_id = $event_location['id'];
echo $event_location_id;
}
catch(Exception $e)
{
echo $e;
}
$params= array(
"name" => "[Study Group]".$_POST['name'],
"description" => $_POST['description'],
"location_id" => $event_location_id,
"start_time" => $_POST['start_time'].$_POST['time_zone'],
"privacy_type" => $_POST['privacy_type'],
"end_time" => $_POST['end_time'].$_POST['time_zone']
);
Here is the exception I have:
Exception: The place or event you attempted to plan to attend is invalid. Please try a different place or event.
The exception is solved in the following way:
change:
$event_location_id = $event_location['id'];
to
$event_location_id= $event_location['location']['id'];

The exception tells itself that the error is the wrong value of Location Id.
And as you have debugged it by yourself that this was because of you retrieved it as
$event_location_id = $event_location['id'];
Which should instead be retrieved as
$event_location_id= $event_location['location']['id'];

Related

Stuck on 'Ads API Access Level:development'

I try to have an app that can pause/resume my adset (accounts owned by myself).
I get no error message(or anything) output when requesting this
$adset = new AdSet($adsetid);
$adset->campaign_status = AdSet::STATUS_ACTIVE;
try{
$adset->updateSelf();
} catch (RequestException $e) {
$response = json_decode($e->getResponse()->getBody(), true);
var_dump($response);
}
But I see that the adset status did not change.
Now, I do see that the Marketing API, Settings section shows me that the API access Level is development and the app doesn't have Ads management standard access.
When I check permissions at App review > Permissions and features it shows 'Standard access' and 'ready to use'. (however not 'Active')
And at the same time my request count and error rate in the past 30 days are acceptable.
I don't understand what is missing to make it work. Can anyone help me out?
The code I have shown in my question was based on the code I copied from the Facebook marketing API documentation.
Strangely when I simulated my request using the Graph API Explorer and hit the "Get code" button it will suggest you a different code.
When I used that code instead of the code of the marketing API docs it did seem to work just as expected.
This code worked as opposed to the code from the docs:
$adsetid = "YOUR ADSET ID";
$access_token = "YOUR ACCESS TOKEN";
try {
$response = $fb->post(
'/'.$adsetid,
array (
'fields' => 'status',
'status' => 'ACTIVE'
),
$access_token
);
} catch(FacebookExceptionsFacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();

Handling errors in AWS PHP SDK 2

If I try to get an object from my S3 bucket that doesn't exist, the Amazon PHP SDK 2 gives me a pretty ugly error. Handy for me but means nothing to the end user...
E.g:
$s3 = $aws->get('s3');
$result = $s3->getObject(array(
'Bucket' => 'my bucket',
'Key' => 'path/to/file'
));
The error:
Fatal error: Uncaught Aws\S3\Exception\NoSuchKeyException: AWS Error Code: NoSuchKey, Status Code: 404, AWS Request ID: xxxxxxxxxxxxx, AWS Error Type: client, AWS Error Message: The specified key does not exist. thrown in AWS/vendor/aws/aws-sdk-php/src/Aws/Common/Exception/NamespaceExceptionFactory.php on line 89
Is there a way that I can determine if there is an error and print a message that makes sense rather than the above?
It suddenly occurred to me to try this:
try {
$result = $s3->getObject(array(
'Bucket' => 'my bucket',
'Key' => 'path/to/file'
));
} catch (Exception $e) {
// I can put a nicer error message here
}
All errors that occur in calls to methods of the AWS SDK are indicated by throwing exceptions. You can catch those exceptions if you want to handle the errors.
In the simplest case, you might just want to catch Exception:
try {
$result = $s3->getObject(array(
'Bucket' => 'my bucket',
'Key' => 'path/to/file'
));
}
catch (Exception $e) {
echo 'Oops, something went wrong';
}
If you only want to handle certain expected exceptions, though, while letting others bubble up and crash your application, then things get a little more subtle.
Firstly, each of the few dozen namespaces within the AWS namespace contains an Exception namespace in which it defines exception classes. One of these classes in each namespace is what Amazon calls the default service exception class for the namespace, from which all other exceptions inherit.
For example, S3 has the Aws\S3\Exception namespace and the S3Exception class. EC2 has the Aws\Ec2\Exception namespace and the Ec2Exception class.
Note that catching one of these exceptions instead of the base Exception class immediately stops us catching certain errors! The service-specific exceptions are thrown as a result of error responses from the server; connection failure exceptions do not inherit from them. For example, if you try running the following code without an internet connection...
try {
$result = $s3->getObject(array(
'Bucket' => 'my bucket',
'Key' => 'path/to/file'
));
}
catch (S3Exception $e) {
echo 'Oops, something went wrong';
}
... then the exception will not be caught (since it will be a Guzzle\Http\Exception\CurlException, not an S3Exception) and the program will crash. For this reason, if you're catching these exceptions just to provide generic failure messages to the user, you should probably catch Exception.
Let's return to the question of how to handle a specific error. For most of the namespaces, the answer is that there will be an exception class defined for that error, and you should catch that. For example, let's say we're again using the S3 getObject method and want to do something when the bucket we ask for doesn't exist. Looking in the S3 Exception namespace docs, we see that there is a NoSuchBucketException we can catch:
try {
$result = $s3->getObject(array(
'Bucket' => 'my bucket',
'Key' => 'path/to/file'
));
}
catch (NoSuchBucketException $e) {
echo 'There is no such bucket.';
}
(In practice, it may well be easier to figure out which exceptions can be thrown by what operations through trial and error than through carefully reading the docs.)
Finally, it is worth mentioning the EC2 API. Unlike all the other services, the EC2 namespace includes only a single exception class, the Ec2Exception. If you want to catch and handle a specific error, you need to inspect the exception object to figure out what kind of error you're dealing with. You can do this by checking the value returned by the getExceptionCode() method of the exception.
For example, a (modified) snippet from a script I recently wrote that grants specified IPs access to our MySQL server:
try {
$result = $ec2->authorizeSecurityGroupIngress([
'GroupName' => 'mygroup',
'IpProtocol' => 'tcp',
'ToPort' => 3306,
'CidrIp' => $ip . "/32",
]);
}
catch (Ec2Exception $e) {
if ($e->getExceptionCode() == 'InvalidPermission.Duplicate') {
echo "IP already has requested permission.";
}
else {
// Don't know how to deal with this error; let's crash
throw $e;
}
}
Note that the possible exception codes - like InvalidPermission.Duplicate in this case - are not listed in the AWS PHP SDK documentation, but you can find them by trial and error or from the documentation for the EC2 API itself, in which each API action's page contains an 'Errors' section listing the error codes it can return.
You can also use this method: $response = $s3->doesObjectExist( $bucket, $key );
It will return a boolean true response if the object exists.
AWS Docs for doesObjectExist

events fql return empty array

I'm trying to retrieve users events using the facebook php sdk, but i'm stuck, the api return an empty array
$user = $me['id'];
$fql = "SELECT eid, name, start_time, end_time
FROM event
WHERE eid IN (SELECT eid
FROM event_member
WHERE uid = 1552544515)
ORDER BY start_time LIMIT 5";
$params = array(
'method' => 'fql.query',
'query' => $fql,
);
try {
$result = $facebook->api($params);
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
thanks in advance.
If this is returning nothing, the most likely reason is that your user hasn't granted the user_events Permission during the Authentication flow.
Looking at your comments above, you may have added user_events to the permissions granted when a user goes through the Authenticated Referrals flow or via App Center, but regular users accessing the app directly need to go through one of the authentication flows from the document above, you're probably not doing this

Facebook OAuthException: (#1)

I have a few applications which upload image to user profile. A few hours ago all applications were working fine but now when uploading is requested, it gives this error
Fatal error: Uncaught OAuthException: (#1) An unknown error occurred thrown in applications/fb-sdk/facebook.php on line 543
I'm using the following code to publish image.
$FILE = "images/$image";
$args = array('message' => 'My msg ');
$args['image'] = '#' . realpath($FILE);
$data = $facebook->api('/'.$uid.'/photos', 'post', $args);
Is it because of some policy change or some new feature?
I have all the permissions like upload is set to true and application takes permission to upload file.
P.s: when the application is used 2nd time, it works fine.
You need to verify if the user is logged in AND has the permissions to post on wall. We're going to do that with a TRY/CATCH with a call to the user.
$userId = $facebook -> getUser();
if ($userId) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$userId = NULL;
error_log($e);
}
}
$app_permissions = array(
'scope' => 'publish_stream'
);
$logoutUrl = $facebook->getLogoutUrl();
$loginUrl = $facebook->getLoginUrl($app_permissions);
If the user is not logged in OR has authorized the app, you'll need to redirect him via header redirect or with a link.
if ($userId){
//Then you can call the facebook api
$data = $facebook->api('/'.$uid.'/photos', 'post', $args);
//... ...
}
That's the easiest way i've found.
EDIT : This question on stack has helped me : Facebook PHP SDK Upload Photos
No, the error is caused by the system cannot get the image file. Facebook will not allow the empty image field appear in the api. So it return Fatal error: Uncaught OAuthException: (#1) --- although it does not relate to the OAuth and OAuthException.

Adding a tab to a fan page does not work... error: (#210) Subject must be a page

I'm trying to add a tab to a fanpage using the graph api/PHP SDK and I'm receiving an error :
(#210) Subject must be a page I've tried using both the user access_token AND the page access_token but neither work. I've tried using the page id of numerous accounts and still no go. Here is my code:
<?php
$path="/PAGE_ID/tabs/";
$access_token="ACCESS_TOKEN";
$params = array(
'app_id' => "APP_ID",
'access_token' => $access_token
);
try{
$install = $facebook->api($path, "POST", $params);
}catch (FacebookApiException $o){
print_r($o);
}
?>
And here is the error I get:
FacebookApiException Object
(
[result:protected] => Array
(
[error] => Array
(
[message] => (#210) Subject must be a page.
[type] => OAuthException
)
)
[message:protected] => (#210) Subject must be a page.
[string:Exception:private] =>
[code:protected] => 0
Thanks for any help you can provide!
If you are not limited to using the API to add your application to your page then you can follow the instructions provided by Facebook at this link :
https://developers.facebook.com/docs/reference/dialogs/add_to_page/
Essentially you can use a dialog ( see the link above ) or this direct URL to add tab apps to your page :
https://www.facebook.com/dialog/pagetab?app_id=YOUR_APP_ID&display=popup&next=YOUR_URL
Dont forget to substitute APP_ID for your app id and next for a different URL
API Call is atm bugged: https://developers.connect.facebook.com/bugs/149252845187252?browse=search_4f31da351c4870e34879109
But here is a solution for JS: OAuthException "(#210) Subject must be a page." - just do not use the library and do your own call.
I did it with PHP:
<?php
$url = 'https://graph.facebook.com/<PAGE ID>/tabs?app_id=<APP ID>&method=POST&access_token=<PAGE ACCESS TOKEN>&callback=test';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Echo value should be something like "test(true)".