How do I get the campaigns list through the ads API on Facebook? - facebook

I am trying to get the campaigns list from the Graph API Explorer. I'm using the request found at https://developers.facebook.com/docs/marketing-api/reference/ad-account/campaigns/.
GET /v2.8/{ad-account-id}/campaigns HTTP/1.1
Host: graph.facebook.com
I'm' business manager and admin of the page used for ads. I found the AD_ACCOUNT_IDs using the following request on the Graph API Explorer.
GET /v2.8/me/businesses HTTP/1.1
Host: graph.facebook.com
I choosed the one which had the higher privileges.
The error reported when submitting the first request for campaigns is:
{
"error": {
"message": "Unsupported get request. Object with ID '1015359838XXXXXX' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api",
"type": "GraphMethodException",
"code": 100,
"fbtrace_id": "DLyfwGbM8fi"
}
}
What's wrong?
Thanks in advance,
Mattia

#Ryan's answer saved my day. It may sound stupid/silly but I was doing this mistake and I was just stuck at it.
Going through the FB docs for using the Marketing Apis.
I was trying to build the custom audience, this is how the curl request looks like -
One of the basic things to keep in mind while using the Marketing API is, almost all the APIs ask for these two things -
1) ACCESS_TOKEN
2) AD_ACCOUNT_ID
For people who are just starting with the FB Marketing API or somehow stuck in finding these values, I will just tell you how to get these values, so that you don't have to waste your time as I did.
So for getting the ACCESS_TOKEN, go to your app dashboard then click Add Product and then select Marketing API. Once you added the product, this is how the screen should look like -
Now just check the permissions and click on the Get Token button, a token would be generated. Just copy the token in some file and save it.
Now for getting the AD_ACCOUNT_ID value, just go to the Adverts Manager page -
The number that is written inside the red box in you Adverts Manager page is your AD_ACCOUNT_ID.
The final step would be to go back to you app dashboard again, Settings > Advanced
Click on Ads API and then enter the AD_ACCOUNT_ID here. Thats it, you have finished the Access and Authentication process for using the marketing API via your App.
Now comes the part where I was doing the silly mistake. The curls request looks like this -
curl \
-F 'name=My new CA' \
-F 'subtype=CUSTOM' \
-F 'description=People who bought from my website' \
-F 'access_token=<ACCESS_TOKEN>' \
https://graph.facebook.com/v2.8/act_<AD_ACCOUNT_ID>/customaudiences
First I replaced ACCESS_TOKEN with its value.
Then instead of replacing only the <AD_ACCOUNT_ID> I replaced the whole string act_<AD_ACCOUNT_ID>, with the AD_ACCOUNT_ID value.
So I was getting this error while making request to the API which was -
{"error":{"message":"Unsupported post request. Object with ID '120574219' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https:\/\/developers.facebook.com\/docs\/graph-api","type":"GraphMethodException","code":100,"fbtrace_id":"YsSvKKwgLMQ"}}
I don't know why I made this mistake but I am sure this is one of the common mistake others are doing too. Since the response from FB was misleading, I wasted a lot of my time in understanding and reading on the API permissions and authentications.
Hope this article was helpful and save your time.

I similarly was having problems trying to retrieve a list of all campaign IDs within an ad account.
I already am able to retrieve campaign data by ID, so I obviously have permissions already.
But the Facebook error message is misleading.
When retrieving an ad account (in order to then display its campaigns or whatever you want within it), you need to retrieve by its ID prepended with 'act_'.
E.g you could request /act_123456789000001234/campaigns instead of /123456789000001234/campaigns.
Thanks so much to #Jan Sommer at https://stackoverflow.com/a/39974857/470749.

Related

LinkedIn API: fetching our company's posts & profile info

So, I'm making this application, and it's required that it has an embed thingy containing the recent LinkedIn posts as well as basic profile info of the company...
Since I like the Law of Minimum effort, the first thing I saw that made my eyes sparkle was the RSS feed... But it seems like it's been phased out. It just redirects to the company page, period.
Then I realized that we have 2 APIs, v1 being superdead since May.
So, I succesfully generated a v2 access token, keeping in mind that it will have to be renewed every 2 months, but hey, nothing's perfect.
Anyway, I know there's a v1 endpoint that would seem to put me on the right track:
https://api.linkedin.com/v1/companies/{id}:
(id,name,ticker,description)?format=json
However, when I go to the v2 docs (https://learn.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context), there's this entire, very detailed section about Authentication, and a section titled "API guide" which talks about "API Concepts" as well as "Best Practices"... but no section detailing just where the endpoints are and how to use them?????
Please, help me, SO, how do I get a company's recent posts as well as basic info like name and pfp?
Thank you in advance.
UPDATE: I have made progress, I think.
I found this: https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/company-pages-migration
And this: https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/organizations/organization-lookup-api
I am making the call and after basically having to guess how to input my token (it must be included as the oauth2_access_token parameter) I get a 403 error DESPITE me being an administrator for the organization I'm looking up.
Call: https://api.linkedin.com/v2/organizations/XXXXXXXX?oauth2_access_token=my-token
Response:
{"serviceErrorCode":100,"message":"Not enough permissions to access: GET /organizations/00000000","status":403}
This makes me think maybe I need to request extra permissions on authentication... but THAT I do not find anywhere (i.e. what the permissions are. I only find really vague stuff with NO details. eg: https://learn.microsoft.com/en-us/linkedin/shared/authentication/permissions?context=linkedin/context)
UPDATE 2: As suggested by #ManvinderSingh I removed the oauth2_access_token param and instead included my token in the Authorization header. This works awesome for the /v2/me endpoint, for instance, BUT still 403's me on the v2/organizations/XXXXXXXX endpoint for an organization that I am an admin of.
As per the documentation https://learn.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context#step-4-make-authenticated-requests.
You have to send the token in the Authorization header like this.
Authorization: Bearer {access_token}

Empty response to API call to Facebook Graph

I have been hammering away at this problem for the last day. I really hope someone can help me out. I would be very grateful.
What I am trying to do is fetch event data from a Facebook Page. I looked over the documentation, which says this about reading Pages data:
Reading
A Facebook page
Permissions
For pages that are published, you need:
An app or user access token to view fields from fully public pages.
So what I did was I obtained an app access token via:
GET https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID
&client_secret=YOUR_APP_SECRET
&grant_type=client_credentials
which then returned
access_token=MY_APP_ID|MY_APP_ACCESS_TOKEN
After this I followed the documentation and made the following call
https://graph.facebook.com/v2.4/129511477069092/events?fields=id,cover,name,venue,description&access_token=MY_APP_ID|MY_APP_ACCESS_TOKEN
This returned:
{
"data": [
]
}
So after some fiddling around I was able to make the call by switching to version V2.2 and by creating an access key with Facebook API Graph explorer tool(no permissions granted).
GET https://graph.facebook.com/v2.2/129511477069092/events?
fields=id,cover,name,venue,description&
access_token=ACCESS_TOKEN_FROM_FACEBOOK_API_EXPLORER_TOOL
The page I am trying to retrieve data from is public and so are the events, so I can't figure out what is going on. I know I am super close since I am able to retrieve the data with the token provided by the Facebook API Graph Explorer Tool. The thing with using this token is that it only last a few hours and I need something that is going to last longer such as an app access token. I have done some research on Stackoverflow and found similar questions, but none of them solve my problem. Any help will be appreciated to the max! Thanks.
Looks like a bug of v2.4 to me. It works with v2.3 if you remove the deprecated venue field:
https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=129511477069092%2Fevents%3Ffields%3Did%2Ccover%2Cname%2Cdescription&version=v2.3
If the version is changed to v2.4, it returns an empty result.
https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=129511477069092%2Fevents%3Ffields%3Did%2Ccover%2Cname%2Cdescription&version=v2.4
There's no documentation about this in the changelog at
https://developers.facebook.com/docs/apps/changelog#v2_4
so I guess it's a bug. There's already a bug report at
https://developers.facebook.com/bugs/443054055873667/
with status assigned. If you have an app that supports v2.3, I'd suggest you prefix your request like this:
GET /v2.3/129511477069092/events?fields=id,cover,name,description

Facebook graph api - Unsupported get request

I'm creating a custom module in Drupal, that for part of its functionality must fetch posts from a business page. So for simplicity, I'm using fbapp module as a dependency (drupal.org/project/fbapp), so that I can use it's authentication and request functions (fbapp_app_authenticate() and fbapp_graph_request()) without having to worry about the constant facebook graph updates making my own code obsolete.
I've created a facebook app, so authentication should be app token, using appid and app secret. This seems fine and I'm getting back access_token. However, when I try to read posts from a publicly available page (the clients), I get the response:
"Unsupported get request. Please read the Graph API documentation at https:\/\/developers.facebook.com\/docs\/graph-api"
Here's the queries and responses my code produces:
graph.facebook.com/oauth/access_token?client_id=<redacted>&client_secret=<redacted>&grant_type=client_credentials
array(1) {
["access_token"]=>
string(43) "<redacted>|<redacted>"
}
graph.facebook.com/<page_id>/posts?access_token=<redacted>|<redacted>"
string(183) "{"error":{"message":"Unsupported get request. Please read the Graph API documentation at https:\/\/developers.facebook.com\/docs\/graph-api","type":"GraphMethodException","code":100}}"
Can anyone verify a correct way to query a Facebook page programmatically, perhaps there's a setting in the page I'm querying that I need to set (although I can't find anything)?
If page restrictions apply, the page's feed can only be retrieved with an user access token as far as I know (because FB needs to evaluate the visibility criteria, and setting your app to the same restrictions doesn't help here):
See
https://developers.facebook.com/docs/graph-api/reference/v2.4/page/feed#readperms
It looks like everything you have done is correct. The response you got can be (i am not sure) because you got your clients pageid wrong.

Is there a trick to editing the status of an Ad Group?

According to the Facebook documentation, you can update the run status of an individual ad group via the ads api.
To do this, you submit a POST request to /<AD_GROUP_ID>/ with adgroup_status=X where X is your status (1 for active, 9 for paused, etc). However, making this request results in the status NOT changing, and the only response you get back from Facebook is a JSON representation of the Ad Group.
Has anyone been able to successfully submit ad group status updates to the facebook ads api? If so, are you using the method outlined above, or is there another trick to it?
thanks!
EDIT
I cross posted this into the Facebook Bug tracker in hopes to create a trail/awareness/find more people who were having this problem. https://developers.facebook.com/bugs/354657724569051
EDIT
An example of the request I'm making. This request is being tested from the Facebook Graph Explorer
https://graph.facebook.com/6003521999629?adgroup_status=9&method=POST&access_token=<access_token>
We have not been experiencing problems with changing adgroup_status. Try submitting the status number as an integer and then as a string (I don't remember which type they expect). Note that if the campaign is paused, setting the adgroup_status to 1 will actually change it to 8 (campaign_paused). The fact that you're getting the adgroup to redownload (I presume you have the redownload=1 parameter) tells me that your call is mostly correct. I just confirmed that this works:
curl -F "adgroup_status=9" \
-F "ids=..." \
-F "access_token=..." \
https://graph.facebook.com/
If you're making that API call and receiving the adgroup's details back instead of a 'true' response, it means you're making a GET request, not a POST request.
Update your code to make a HTTP POST request, and this will resolve your issue, John Pickard's answer above is an example of making a POST request in curl, but it will change depending on your application's language and/or which Facebook SDK you're using.

Open Graph custom action that links to other user

I have question about the beta Open Graph stuffs.
The documentation I am looking at is this https://developers.facebook.com/docs/beta/opengraph/
I successfully define custom objects and actions. However, I cannot figure out how to link current user with other users.
For example, say I define a custom action 'kick'. I want the current user to be able to 'kick' one of his/her friends.
The closest object I can think of is 'profile', but when I pass user_id, 'http://www.facebook.com/profile.php?id=', or http://graph.facebook.com/, it does not work.
This is the requests I tried
POST https://graph.facebook.com/me/myapp:kick?access_token=abc&profile=http%3A%2F%2Fwww.facebook.com%2F123
POST https://graph.facebook.com/me/myapp:kick?access_token=&profile=123
POST https://graph.facebook.com/me/myapp:kick?access_token=abc&profile=http%3A%2F%2Fwww.facebook.com%2Fprofile.php%3Fid%3D123
POST https://graph.facebook.com/me/myapp:kick?access_token=abc&profile=http%3A%2F%2Fgraph.facebook.com%2F123
This is the response I get
{"error":{"message":"An unexpected error has occurred. Please retry your request later.","type":"OAuthException"}}
The profile given is for an external website. They call it external profile. Pretty misleading terms.
As you can see I used the following
curl -F 'access_token=TOKEN' \
-F 'profile=http://graph.facebook.com/zuck' \
'https://graph.facebook.com/me/MYAPP:kick'
And it gave the above.
I guess you can fill the external profile with meta info from the Facebook users and it will show alright (for example I can send the profile URL facebook.com/zuck instead), but it seems backwards, inefficient and not the intended usage.
For example feeding it my link.
curl -F 'access_token=TOKEN' \
-F 'profile=http://facebook.com/username' \
'https://graph.facebook.com/me/MYAPP:kick'
But the thing is ... I am not a musician.
It does though seem to look alright in aggregation view.
Which is interesting/weird since one of objects shown in this picture is a Facebook profile, so you would think they would give an example with Facebook profiles as objects.
Though they did say
We are now extending the Open Graph to include arbitrary actions and objects created by 3rd party apps and enabling these apps to integrate deeply into the Facebook experience
Which most likely means they want you to create arbitrary objects outside of Facebook.
Can you still link to other users?
Yes, but through tagging using tags=FacebookID1,FacebookID2 but it will be in the form of
phwd kicked a musician with Friend1 and Friend2 on [APP NAME]