Update Facebook AdAccount via API - php

I'm trying to update the payment id on my ad accounts, Is there a way to do this via the API?
Code:
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "https://graph.facebook.com/v2.10/act_$account_id",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'funding_source' => $payment_id,
'access_token' => $access_token
)
));
Getting the following Error: Either the object you are trying to access is not visible to you or the action you are trying to take is restricted to certain account types.
Permissions Error: 200
I am using a system_admin token with all permission, and also tried creating a token with admin permissions, still get the same error.
Reading through the API I can't find any examples of updating an Ad Account.
Is there a way to update the payment (funding_source) id through the API?
Thanks,

Related

Getting news from sharepoint sites through microsoft graph api

I have been stuck at getting the news from sharepoint sites through the microsoft graph api for a while now, and this is my last resort.
I have been going through the microsoft graph api multiple times and I have found out how to use a work microsoft account to get the sites I am part of, which works fine through this: https://learn.microsoft.com/en-us/graph/api/resources/site?view=graph-rest-1.0
But I cannot figure out where to go from here to get the news that has been created on sharepoint sites, and maybe someone can guide me in the right direction on where to get these?
I have tried going over the microsoft graph documentation multiple times, I have tried everythingg that has to do with sites and only got as far as getting the list of sites the account is part of.
I have tried searching for a solution with no luck.
this is what I have used to get the sharepoint sites list:
/sites?search=*&$select=id,displayName,description,webUrl,sharepointIds
I think I have figured it out, but I am not sure why it is not working if I do it in the same flow, so what I had to do was call the microsoft graph api (I do that through league.
$oauthClient = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => $this->clientId,
'clientSecret' => $this->clientSecret,
'redirectUri' => "<redirect url>",
'urlAuthorize' => "https://login.microsoftonline.com/<tenant id>/oauth2/v2.0/authorize",
'urlAccessToken' => "https://login.microsoftonline.com/<tenant id>/oauth2/v2.0/token",
'urlResourceOwnerDetails' => '',
'scopes' => "openid profile User.Read offline_access Sites.Read.All",
]);
$accessToken = $oauthClient->getAccessToken("authorization_code", ['code' => $authorization_code]);
After I get the access_token and refresh_token I then use curl to get the sharepoint online access_token and refresh_token by using the frefresh_token from the previous step:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://login.microsoftonline.com/<tenant id>/oauth2/v2.0/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "client_id=<client id>&client_secret=<client secret>&refresh_token=<refresh token from the previous step>&grant_type=refresh_token&scope=https%3A%2F%<tenant name>.sharepoint.com%2FSites.Read.All",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded"
),
));
$SPOResponse = curl_exec($curl);
$SPOResponse = json_decode($SPOResponse);
$SPOHttpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
I tried making it all work in 1 step but I could not make it happen for some reason, if I find a way I will update this post.

Cannot set cookies dynamically in api request on a site with laravel and angular

Hello everyone I have an app where I need to make an API request and that app is made of laravel and angular. Laravel for the backend and angular is for the front end. The API is for subscribing a user to a specific task. On my app, I need to make an API request which needs cookies that are being generated from the backend ( most probably I'm not sure ). So those cookies enable my API to work properly. I have all the things ready but I can't set the cookies dynamically. The problem is I'm not sure where are those cookies are being generated from. I have created a custom function that calls the API with some specific parameter and then it just subscribes the user with that privilege that the user was subscribed to.
My app workflow is like below
on the angular, I submit the payment form
after the form submission my app goes to the payment sandbox provided by the third party ( this is a completely different server )
then after a successful payment transaction it redirects to my laravel app and calls the custom function that I made and tries to subscribe the user with the features user paid for.
on the last step, I can't get the cookie that was needed to make the user subscribe to the plan that they paid for. This time I need to get the cookie from the API which I can get by making the request manually. as it's a payment procedure I can not make it manual and the whole process needs to go automatically.
My API is okay no problem with that The problem is that I can not make the cookies dynamically and also I am not being able to use those dynamically generated cookies each time a request is made.
following is the main api function
/**
* Create a new subscription.
*
* #return JsonResponse
*/
public function store()
{
$this->authorize('update', Subscription::class);
$this->validate($this->request, [
'user_id' => 'required|exists:users,id|unique:subscriptions',
'renews_at' => 'required_without:ends_at|date|nullable',
'ends_at' => 'required_without:renews_at|date|nullable',
'plan_id' => 'required|integer|exists:billing_plans,id',
'description' => 'string|nullable',
]);
$subscription = $this->subscription->create($this->request->all());
return $this->success(['subscription' => $subscription]);
console.log('running');
}
following is my custom function
public function apicall(Request $request){
$curl = curl_init();
$xsrf_token = request()->Cookie("X-XSRF-TOKEN");
$cookie = request()->Cookie("Cookie");
curl_setopt_array($curl, array(
CURLOPT_URL => 'localhost:4200/secure/billing/subscriptions',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"user_id": 3,
"renews_at": "2021-01-29 18:00:00",
"plan_id": 1,
"description": "null"
}',
CURLOPT_HTTPHEADER => array(
$xsrf_token,
$cookie,
'Host: localhost:4200',
'Referer: http://localhost:4200/admin/subscriptions?page=1&per_page=15',
'Origin: http://localhost:4200',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
return redirect('http://localhost:4200/dashboard')->with('status', 'Success');
}
On web.php I have configured routes and everything else. My API and payment redirection all are working well but The users are not being subscribed or I can say may API is not being able to verify the request's owner or the request's validation so it's not being able to give me any 200 response. It just gives me 302 with the found tag. So how should I set the cookie dynamically so that the request cookie can be matched by the system's subscription cookie?
Can anyone help me with this? I need to solve this as soon as possible.

"Invalid platform app" error using Instagram Basic Display API

I am trying to use Instagram Basic display API but when I post the authorization code to get the access token I keep getting the following error
{"error_type": "OAuthException", "code": 400, "error_message":
"Invalid platform app"}
I am following all the steps mentioned here -> https://developers.facebook.com/docs/instagram-basic-display-api/getting-started and Yes I am using the Instagram app ID and It's client secret which is in Products -> Instagram -> Display and following is the URL I am sending the request
"https://api.instagram.com/oauth/access_token?client_id=".$app_id."&client_secret=".$app_secret."&grant_type=authorization_code&redirect_uri=".$redirecturi."&code=".$code,
I ran into this same issue. Problem was I was using the Facebook App ID and App Secret instead of the Instagram App ID & App Secret. You must go to the "Instagram Basic Display" section on the Facebook developers site then scroll down until you find the Instagram App ID & Secret.
If you are using Postman, do remember it's a POST request. Use form data
When you exchange the code you need to use a POST request.
From the looks of your url, you've formed it as a GET request with all the parameters as part of the url rather than as form data. Try sending the parameters as part of the post body instead
Working example code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.instagram.com/oauth/access_token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('client_id' => '{client_id}','client_secret' => '{client_secret}','grant_type' => 'authorization_code','redirect_uri' => '{redirect_uri}','code' => '{code}'),
CURLOPT_HTTPHEADER => array(
"Content-Type: multipart/form-data; boundary=--------------------------780367731654051340650991"
),
));
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
I have got this error for the below reason.
If any case, App Id and Secret Key are blank then this type of error is generated. so we can first test first that that app id and secret key must be the correct one. I know that this is a very normal thing that we can notice easily. But sometimes, we can not notice some simple things.

Nest Token revoked but api does not returns "401 unauthorized" response

I'm building an app using the nest api. We can successfully authorize a user with the API, however, if the users revokes the app authorization from within it's account, the API does not return a 401 unauthorized response but the full response as if the user had not revoked the token access.
Here's a step by step of how I tested :
First the user comes to our website, we request the auth url to nest
https://home.nest.com/login/oauth2?client_id=%s&state=%s
Where both string are replaced by, respectively, the product key and a random state string.
From there, the user can authorize the app, it will then redirect to the configured redirect url of the product.
We then query for a long access token, which we also get properly.
Then the user can ask to see a list of devices within his account (cameras only), so we query the users' devices list with
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://developer-api.nest.com/devices",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_FRESH_CONNECT => true,
CURLOPT_FORBID_REUSE => true,
CURLOPT_HTTPHEADER => array(
"authorization: Bearer ".$token,
"content-type: application/json",
"Cache-Control: no-cache"
),
));
Where $token is the users' long access oauth token.
This works just as expected. Until the user revokes the app authorization from within his nest account. If we follow the API documentation, this call would return a 401 Unauthorized response.
However, the response is still the list of users' devices, even if the token is now invalid. Which mean we still have access to the user's informations even if he technically revoked our access. The user is therefore never prompted to re-authorize the application and since this is taking place directly in the nest website and that there are no event callback, we can't detect if the user wants to revoke the rights of our application.
This is causing us quite a few headaches as we can't test the functionality of our website properly. Ideally, we would then have to request another token from the user.
Thanks

Login to Access Facebook data on my Server with FB PHP SDK and Cordova App

I can't figure out the logic flow for a cordova app I am creating. Here is what I want to do.
User is using a Cordova app I am creating and they login to Facebook with a plugin I have (wizcorp Cordova plugin). Nothing in the login process communicates with my own server.
After login my server needs to access some data on my web server in a database that I may have previously saved about this user. (Like posts they have made to their friends through the app).
I need to verify in some PHP code that the user is truly this fb user and not an imposter so I imagine I need to use the access token returned to the user in the app.
I can easily send the token to my PHP code but I can't figure out how to use the PHP Facebook SDK to set this token and get the verified user's Facebook ID.
Here is my PHP code.
require_once("fb/autoload.php");
//Create the Facebook service
$facebook = new Facebook\Facebook ([
'app_id' => '12345',
'app_secret' => '67890',
'default_graph_version' => 'v2.4'
]);
$facebook->setAccessToken($_REQUEST['access_token']);
if (($userId = $facebook->getUser())) {
echo "userId=$userId<br/>";
}
I am getting this error: Call to undefined method Facebook\Facebook::setAccessToken()
curl_setopt_array($curl, array(
CURLOPT_URL => "https://graph.facebook.com/$fb_user_id?fields=id%2Cname&access_token=$access_token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 3602fbe6-2faf-9c7f-05b0-bcaea8548000"
),
));

Categories