I am getting the Error:
Fatal error: Uncaught OAuthException: (#200)
when i try to invite people via an App to an event.
My Steps:
1. Getting long lived token with Permissions: create_event create_note publish_actions publish_stream rsvp_event user_events
2. Getting Userids who are friends and not already invited to the event.
3. Split into 50 Persons per Invite and send it.
Code for Inviting:
$friends = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT uid,name FROM user WHERE uid IN
(SELECT uid1 FROM friend WHERE uid2=me()) AND NOT
(uid IN (SELECT uid FROM event_member where eid = '$eventid'))"));
foreach ($friends as $value)
{
if (!isset($allids))
{
$allids = $value['uid'];
}
else
{
$allids .= ',' . $value['uid'];
}
$count++;
$invitedusers++;
if ($count > 49)
{
//$splitinvite = $facebook->api($eventid . '/invited?users=' . $allids, 'POST');
unset($allids);
$count = 0;
}
}
$facebook->api($eventid . '/invited?users=' . $allids, 'POST');
I already seperated the Userids to make sure that they are setted correctly in the format Userid_1,Userid_2,Userid_3
I checked the token with the graph token debugger and all permissions are setted correctly.
Anyone an idea?
One posibility is the following Code. But this works realy slow because it sends a query for each person.
It takes up 30 Seconds for checking 37 people. Querying 50 people at once as shown above invites over 1000 people in 30 seconds.
I would appreacheate it if anyone can try to find a better way!!!
The best way would be filtering the FQL query (if is posible).
Slow working Code:
foreach ($friends as $value)
{
try
{
$splitinvite = $facebook->api($eventid . '/invited?user=' . $value['uid'],'POST');
$count++;
}
catch (Exception $e)
{
// Users privacy setting blocked inviting him, has blocked you or has blocked you from inviting
}
}
Related
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();
I am creating a REACT app that uses wordpress as a backend through the rest API. I am using the Simple-JWT-Login plugin to handle the register, login, authentication, and it works perfectly.
I want to block non-logged in users from accessing the /wp/v2/posts endpoint. I know I can do this by doing something like:
add_filter( 'rest_authentication_errors', function( $result ) {
if ( ! empty( $result ) ) {
return $result;
}
if ( ! is_user_logged_in() ) {
return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) );
}
if ( ! current_user_can( 'edit_posts' ) ) {
return new WP_Error( 'rest_not_admin', 'You are not entitled to view that.', array( 'status' => 401 ) );
}
return $result;
});
However that blocks all endpoints, including ?rest_route=/simple-jwt-login/v1/auth and ?rest_route=/simple-jwt-login/v1/users which are the ones from the Simple-JWT-Login plugin used for registering and authenticating users. Therefore users end up in a catch 22 whereby they can't login unless they are already logged in!
Is there a way of modifying the above code to block just the wp/v2/posts/ endpoint, or, alternatively, of effectively whitelisting the registering and authenticating endpoints?
Many thanks for any ideas!
Have searched on here and elsewhere but can't find a solution.
You could always check for the url before showing the error with $_SERVER['REQUEST_URI'] but that might become a pain to add all your allowed urls in a if check.
I like to use apache_request_headers with preg_match to check for a Bearer Token and if the current request has the correct bearer token, show the results. Otherwise you would throw a error. I can't guarantee its 100% safe or the correct way to do it but it works pretty well for a super basic simple system.
Something like the below code might work for someone:
add_filter("rest_authentication_errors", function ($result) {
if (!empty($result)) {
return $result;
}
$headers = apache_request_headers();
$matches = array();
$currentToken = "PUT YOUR BEARER TOKEN HERE";
$pattern = $currentToken."/i";
preg_match('/Bearer '. $pattern, $headers['Authorization'], $matches);
if(isset($matches)){
$token = $matches[0];
if($token == 'Bearer ' . $currentToken){
return $result;
} else {
return new WP_Error( 'no_auth_found', 'You are not currently logged in.' , array( 'status' => 401));
}
}
});
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
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.
I am attempting to access a user's friends work and education history using the Graph API. I have included both permissions in the auth dialogue. I have tried accessing them using FQL but have had no luck.
try{
$fql = "select friends_education_history from permissions where uid=" . $user;
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
}
catch(Exception $o){
d($o);
}
}
I'd prefer to access the info without using FQL but at this point, I need to figure out ANY way. Is there a way to do something like this:
if($user_id) {
try {
$friends = $facebook->api("/me/friends");
}
catch(Exception $o){
d($o);
}
}
?>
And then call the respective variables?
Your FQL query would look something like this:
SELECT education,work from user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
Above query will fetch all education and work objects from friends of the logged in user.
SELECT education,work from user WHERE uid = <YOUR FRIENDS USER ID>
Above query will get education and work for just the one user id you provide
Hope this helps!
The User graph api object has fields education and work.
Permissions user_education_history & friends_education_history are required for the first, user_work_history & friends_work_history for the second.
These fields are optional & a user can restrict their visibility via her profile page, so even having granted the permissions the api may return nothing.
Use the Graph api explorer to have a play.