Stuck on 'Ads API Access Level:development' - facebook-graph-api

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();

Related

Accessing USI Webservice using a Token from Vanguard returns an Error

So I was trying to connect our SMS (Student Management System) to a government Service or Site. The process requires an authentication token coming from Vanguard. Successfully, I am able to obtain the token... but due to lack of documentation and sample codes in PHP I am having problem communicating to the said service. I was wondering if is it my code that causes the problem or is it my calls that has conflict interfacing to the webservice: Please see code below:
<?php
require_once 'VanguardClient.php';
$endpoint = 'https://3pt.portal.usi.gov.au/Service/v2/UsiService.svc';
function get_sts($endpoint){
$test = true;
$auskey_path = 'Keystore.xml';
$auskey_password = 'Password1!';
$v = new VanguardClient($test);
$v->loadAuskey($auskey_path, $auskey_password);
try {
return $v->requestToken($endpoint);
} catch (SoapFault $e) {
echo "Error1:";
echo $e;
}
}
//get token from Vanguard
$token = get_sts($endpoint);
//create soap client
try{
$wsdl = 'https://3pt.portal.usi.gov.au/service/V2/UsiService.wsdl';
$client = new SoapClient($wsdl,
array(
'trace' =>1,
//'soap_version' => SOAP_1_2,
'keep_alive' => false,
//'connection_timeout' => 500000,
'cache_wsdl' => WSDL_CACHE_NONE
)
);
} catch (SoapFault $e) {
echo "SoapClient Error:<br />";
var_dump($e);
}
try {
$result=$client->__setSoapHeaders( $token );
} catch (SoapFault $e) {
echo "__setSoapHeaders:";
var_dump($e);
}
$data = array(
'OrgCode' => '970003',
'USI' => 'U6Q8JN6UD9',
'FirstName' => 'Myrna',
'FamilyName' => 'Macknight',
'DateOfBirth' => '1971-04-19'
);
try{
$response=$client->__soapCall('VerifyUSI',$data);
} catch (SoapFault $e) {
echo "__soapCall Error:<br />";
echo $e;
}
var_dump($response);
The result on the browser that I am seeing is this:
SoapFault exception: [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. in /var/www/html/usitest/example1.php:73 Stack trace: #0 [internal function]: SoapClient->__doRequest('__soapCall('VerifyUSI', Array) #2 {main}NULL
Thanks in advance guys!!!
Your content type is probably caused by the SOAP version.
Try setting the SOAP version to 1.2:
'soap_version' => SOAP_1_2
See SoapClient connection to SoapServer
However, I think there are other issues in your code - particularly with the Vanguard token.
We managed to solve this however it took many classes, templates, external packages and months of work to solve and is not something we can put up online. However there are some things I'd suggest you do to solve it in your situation.
This does not work like a normal SOAP service. Use XML templates for all steps of the various process (Vanguard, USI, components of these sections etc).
Reverse engineer the .Net example code, we had major issues with the Java code.
We made major headway by using a proxy and capturing the content sent and received.
Unless you are using composer to manage your security dependencies you're going to have a bad time, even with composer it was a pain.
There are about 10 sections to do with security that have to be reverse engineered, don't forget to canonicalise the content to get the encryption correct.
Use Guzzle for the requests, it's easier
Most of the stuff in the PHP example is wrong, or at least impossible to follow and debug to fix. At the end we couldn't see a way that it would work.
Expect to spend at least a couple of weeks on it and you need to know a lot about security, hashing and ssl certificates.
Sorry I can't give you a full working solution but knowing these steps above would have definitely helped us and so I hope they'll help you.

facebook-php-sdk serverside graph api call with app token

I'm trying to show the feed and events of a Facebook fanpage on a website. I'm using the php sdk to call the Facebook graph api. I also created an app on my Facebook account and have an app ID, app secret and app token.
I don't want to log in the user of the website. I just want to integrate the events and news data of the Facebook fanpage on the website.
My question is: Can I use the static app token to make graph api calls with php from my server and build my website after that, so that no one will receive the app token? Or is this bad because of security issues?
Here is the code:
$app_ID = '{appID}';
$app_secret = '{appSecret}';
$app_token = "{staticAppTokenFromFacebookDeveloperSettings}";
$facebook_page_ID = '{facebookPageID}';
$fb = new Facebook\Facebook([
'app_id' => $app_ID,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.7',
]);
$fb->setDefaultAccessToken($app_token);
try {
$response = $fb->get('/' . $facebook_page_ID . '/feed');
//$response = $fb->get('/' . $facebook_page_ID . '/events');
// ...
} 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;
}
Thank you very much!
You have to put the App Secret somewhere in order to use an App Access Token, and the server is the only place where it is safe. Just keep in mind that there are limits to API calls, you should cache results in your own database. Do not call the feed and events endpoints for every single user hit.

Facebook API - App using Oauth to update status of page

I have built an app using the PHP Facebook SDK. It allows users to authenticate my app with facebook OAth so that they can update their status' via my App. This works great, however a lot of my users have business pages and they want to update the status on their business page not their main personal feed. How is this possible? Below is what I have so far.
if ($status != ''){
try {
$parameters = array(
'message' => "$status"/*,
'picture' => $_POST['picture'],
'link' => $_POST['link'],
'name' => $_POST['name'],
'caption' => $_POST['caption'],
'description' => $_POST['description']*/
);
//add the access token to it
$parameters['access_token'] = $access_token;
//build and call our Graph API request
$newpost = $facebook->api(
'/me/feed',
'POST',
$parameters
);
$success['status'] = "$xml->status";
$xml2 = new XMLGenerator($success, 'facebook','status');
$returnData = $xml2->output;
$returnData = APIResponse::successResponse('200', "$xml->status");
} catch (Exception $e) {
$returnData = APIResponse::errorResponse('400', 'Facebook Error: '.$e);
}
I assume I would have to change '/me/feed'? but to what? What is they have multiple pages how would my app know which page to post to?
Any help with this would be much appreciated.
You can substitute me with the PAGE_ID to post to a page e.g., /013857894/feed. Make sure that you have completed the OAuth process with the manage_pages and publish_stream permissions. You can learn more at the link below:
https://developers.facebook.com/docs/reference/api/page/#statuses
If the user has multiple Pages then you will first need to give them some way of selecting which page they want to post to. You can find out which Facebook Pages a given user is the administrator of by calling /me/accounts for that user. You can find out more about this approach in the Connections section of this page:
https://developers.facebook.com/docs/reference/api/user/

Error OAuthException: (#200) when inviting to Event

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
}
}

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.