How can I post in Facebook wall programmatically? - facebook

I'm very new to Facebook (yeah, I'm the only one) but I'd like to publish messages in the wall programmatically using Python.
What do I need to do to achieve that? I'm very confused. I have seen that most question about this are focused on how to let an app interact in some way with Facebook, but I'd like to post as if I were a simple user (a user which happens to "own" that particular Facebook page). Do I really need to go and create an app? (By the way, I'm having problems even with that... it tells me that my account seems fake or invalid even after I validated it via SMS). I imagine that there is a simpler way, introducing my credentials, using a POST request but I haven't read something similar.
I would have thought this is what I needed, but it leads to a page to create an app.
Some pointers?
By the way, I got a token via a publish_stream permission in the Graph API Explorer (using the extended permissions tab). Seems like this permission doesn't have expiration date but it's not recommended. Is there a better choice for a token?.
Thanks

Facebook is a bit trickier than other sites it would appear.
While watching the POST tab under NET in Firebug, while logging in, I see the following POST parameters:
charset_test €,´,€,´,水,Д,Є
default_persistent 1
email myusername
lgnjs 1340241652
lgnrnd 182050_CuPx
locale en_US
lsd AVqwALVx
pass mypassword
persistent 1
timezone 240
I don't yet know what lsd paramater is, but I did find out what the lgnrnd was by viewing the source of the login page before I logged in. It appears to be there to make it more difficult to automate authentication.
You first want your script to scrape that login page for that value, store it in a variable. Then, a cURL example would be something like:
curl -L "https://www.facebook.com/login.php?login_attempt=1" \
--cookie-jar /tmp/cookies.txt \
--data-urlencode "email=myusername" \
--data-urlencode "lgnrnd=$lgnrnd" \
--data-urlencode "pass=mypassword"
and so forth, with all the parameters...
This is untested of course, and I don't know what some of those parameter values are yet, but you should get the idea from that. You could probably use a python module like Beautiful Soup or something to apply the above basic concept.
Then you would do the same thing, watching the NET tab to find the parameters sent when you post to your facebook wall, and put it all together.
Note that I'm storing and using cookies with curl's --cookie-jar option. You will need a way to save and use cookies with the request as well, which is why I showed that.
Hope that helps you get started anyway..

What about this?.
import urllib2, urllib
parameters = {}
parameters['access_token'] = MY_TOKEN
parameters['message'] = 'Hello world'
target = 'https://graph.facebook.com/MY_ID/feed'
parameters = urllib.urlencode(parameters)
handler = urllib2.urlopen(target, parameters)
while True:
if handler.code < 400:
print handler.read() # Gets post_id
break
elif handler.code >= 400:
print 'Error' # :-(
break
Are there problems with this? Maybe a better way? Comments?
Thanks

Related

I can't send a Facebook game request using Graph API v2.8

I've spent a lot of time trying to send a game request from the (latest) JS API, using the Graph API v2.8, but nothing seems to work.
I've used only common OG, since custom OG are deprecated. When I use "turn" instead of "send" as the action type, my request works, since the "turn" requests don't require an object id, so I guess the problem comes from my OG.
I first created an action like this:
curl -X POST \
-F "access_token=*a user access token*" \
-F "game=*my game id*" \
https://graph.facebook.com/v2.8/me/games.plays
And then sent a game request using it the returned id in the "object_id" field. And this is the result I got:
Facebook response after calling FB.ui
Which doesn't provide any information...
So I tried to make a game object instead of an action. I figured "maybe you can't use actions in this context", and since game.achievement is the ONLY game object available, I made an achievement object, like this:
curl -X POST \
-F "access_token=*my app access token*" \
-F "object={\"fb:app_id\":\"*my app id*\",\"og:type\":\"game.achievement\",\"og:url\":\"*object's URL*\",\"og:title\":\"*A title*\",\"og:description\":\"*A description*\",\"og:image\":\"*A link to an image*\",\"game:points\":\"1\"}" \
https://graph.facebook.com/v2.8/app/objects/game.achievement
This is the result that I got:
Facebook response after calling FB.ui
Which makes no sense, because the object was created using an app access token, and that my app IS a game. So this statement makes no sense.
And now, I'm lost. I have no solution left, no new idea to try. I don't know what to do... The documentation is obviously missing information. They didn't even bother making a documentation for the games.plays action...
I need someone who knows how to use this damned API to help me figure this out, because right now, I'm just completely stuck.
Thanks in advance, community.

How do I get the campaigns list through the ads API on 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.

How to have users 'reconnect' with soundcloud on each page reload?

I'm using the Javascript SDK inside a node.js (Express) App.
Connecting Users works fine, but the connection does not persist between page reloads.
Is this the default behaviour, or is the User supposed to stay connected in new requests?
Do I have to use OAuth token authentication and if so, how can this be done with the JS-SDK?
Inside the "Permission"-Popup, Users are already logged in with soundlcoud, though.
(just have to click the "connect" button each time)
Figured I'd share my answer for those who are unsatisfied with the current answers for automated oauth:
Retrieving access_token:
I had to define get and set cookie functions and then I use the functions to set and retrieve a function holding the access token. I'm not going to give these functions for conciseness but you can easily find them with a google search. I then use this line of code to get the SC access token (once the user has authenticated for the first time)
SC.accessToken()
Setting token:
So this is kind of just an elephant in the room in my opinion that for some reason no one has mentioned. How in the **** do you connect w/ SC using the access token? Do you set it as oauth param? On each call pass it? Well, after experimenting with putting the parameter in every single place I could think, I found out you have to do something like this:
SC.initialize({
client_id: '[removed for security reasons]',
client_secret: '[removed for security reasons]',
redirect_uri: '[removed for security reasons]',
access_token: getCookie("sc_lm2"),
scope: 'non-expiring'
});
//Where "sc_lm2" is the name of my cookie
Hope the helps! Took me a while to figure this out for such a simple thing
EDIT
Using PHP and Wordpress:
$json = wp_remote_get("http://api.soundcloud.com/users/[user_id]/tracks.json?client_id=[client_id]");
$soundcloudData = json_decode($json['body'],true);
(substitue cURL functionality if you're not using Wordpress). #krafty I assume you would just change the endpoint from "/tracks" to "/users" but I can't say I have ever really needed to grab anything but tracks using the Soundcloud API. Hope this helps, though I'm not sure I fully understand what it is that you are trying to accomplish (or rather, how exactly you're going about it) - are you trying to allow user logins? If you want to explain fully what you're trying to accomplish and the steps you're taking I'd be happy to take a crack at it.
Yep, this is the way to do it, officially. :)
For the Next SoundCloud site, we store the token in localStorage (until the user logs out, of course). In each AJAX request to the API from the front end, we put the oauth token in a request header:
jqXHR.setRequestHeader('Authorization', 'OAuth ' + the_oauth_token);

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]