Get newest post from a Facebook page as a string in JavaScript - javascript

I simply need to get the newest post from a specific Facebook page as a string in JavaScript for a website. I have never used the Facebook API and I'm still new to JavaScript, after many attempts I just can't figure out what the problem is...
So far this is everything I've done:
Created a Facebook app, chose 'website' for the platform
Inserted the JavaScript SDK code that I got from Facebook into my website code and replaced the placeholder 'appID' with the 'App ID' from my Facebook app
Became admin to the Facebook Page
On the Graph API Explorer page used the 'Get Token' dropdown to select 'Get Page Access Token', then selected the Facebook Page that shows up in the dropdown
Used the following request in the Graph API Explorer: 'https://www.facebook.com/pagenamehere/?fields=posts.limit(1)' (The output provides the post I need but with some extra information like "create_time", "id", etc. that I will most likely remove in JavaScript unless there is a more precise request I can use instead since I only need "message")
Copied the provided JavaScript code from 'Get Code' and inserted this under the JavaScript SDK code
replaced '//Insert your code here' with 'alert (response)', the alert message is: [object Object]
Either I don't know how to obtain "message" from '[object Object]', or I'm not getting the response I need. I've tried to do this with the webpage on a live server.
Please help, I'm out of ideas and I've read the Facebook API too many times.
Thanks in advance.

It's simple. Just have a look at the Graph API Explorer, and you'll get the idea how to access the message property:
Sample Graph API Explorer call
A call to /buzzfeed/feed?limit=1 will return
{
"data": [
{
"message": "science",
"created_time": "2015-11-23T07:01:00+0000",
"id": "21898300328_10154109572045329"
}
],
"paging": {
"previous": "https://graph.facebook.com/v2.5/21898300328/feed?limit=1&format=json&since=1448262060&access_token=&__paging_token=&__previous=1",
"next": "https://graph.facebook.com/v2.5/21898300328/feed?limit=1&format=json&access_token=&until=1448262060&__paging_token="
}
}
which means that
console.log(JSON.stringify(response.data.message));
should print you the message.

Thank you for your suggestions, they helped point me in the right direction and I was able to get this to work.
Here are the missing steps from my original post required to make it work:
Had to also add my domain to the 'Valid OAuth redirect URIs' field in 'My Apps' -> 'Settings' -> 'Advanced'
My access token had to be included in the code generated from the Graph API Explorer, like the answer in this post:
(your user access token should be extended to 60 days in the 'Access Token Tool' page by clicking 'debug' -> 'Extend Access Token' and using this token in the code)
The Facebook SDK needs to finish loading before the code generated from the Graph API Explorer is executed, I used Mohammed Arif's answer from this question to do this but I had to use a delay for it to work. I put his code in a function that runs after a 200ms setTimeout, if it is loaded the code generated from the Graph API Explorer executes, else I run the function after a 200ms setTimeout again.
Tobi's answer to part of the question (to get the message) was almost correct: response.data[0].message;

Related

How to allow my google script to be run by anyone off of a webpage

I am currently attempting to write a google script that will run once a submit button is clicked on a form in a webpage. When the button is pressed it takes the form data, logs it into googles Logger and then emails a JSON report from the google account the script was made on. I have just gotten this script to run and print how I want it to thanks to another user on this site and I have now run into the issue that the only reason it was running and working is because I was signed into google on the account I made to send the emails. I have turned my script to public access by anyone on the web and still all I am getting is that google is "unable to open the page at this time." Is there some other way to get around this without having the user use their own email client?
var TO_ADDRESS = "test#email.com";
var output = HtmlService.createHtmlOutputFromFile('Confirmation');
function doPost(e) {
try{
Logger.log(e);
MailApp.sendEmail(T0_ADDRESS, "RFQ: The following person has requested a quote", JSON.stringify(e.parameters, null, 4));
return output;
This is the code which as I said earlier does work as long as I'm logged in but I would like this to be able to be used by anyone that accesses the website
I have done something similar using "Deploy as web app" option you will get an url , you can give that to anyone and they too can execute this without any need of authorization.Below is a walkthrough
You can use this url in whatever way you like, as a curl request,ajax or just a browser url request
But note that you must have a doGet() otherwise it would give an error like in the final screenshot.But since i had put doGet() it ran perfectly (forgot to take its screen :P) .

How get Facebook token using Oauth2 with chrome.identity

I'm using chrome.identity in a packaged app to get a user token using Facebook. I call chrome.identity.launchWebAuthFlow, and I need to take the access token, send it to the server, and then access token is used to verify the user.
Works great with Google+. But for some reason, it doesn't work for Facebook. For some reason, Facebook's OAuth appears to be special. I've added the extensionid.chromiumapp.org to the list of redirect URLs in the API. Added
"https://extensionid.chromiumapp.org/*",
"https://facebook.com/*",
"https://www.facebook.com/dialog/",
"https://graph.facebook.com/*"
to manifest.json. But nothing changed.
In calling chrome.identity.launchWebAuthFlow I use URL like this
"https://www.facebook.com/dialog/oauth/access_token?" +
"client_id=myclientid&client_secret=myclientsecret&" +
"response_type=token&grant_type=client_credentials"
When I try to invoke it, I get the following error message:
«launchWebAuthFlow completed Object {message: "Authorization page could not be loaded."} »
And when I use URL like
«"https://www.facebook.com/dialog/oauth?client_id=myclientid&redirect_uri=https://myextensionid.chromiumapp.org/facebook.com=token&scope=user"»
I get the next error:
«launchWebAuthFlow completed Object {message: "User interaction required."} undefined »
I try to get facebook token in 4 days. I am tired. What I do wrong?
Why chrome.identity.getAuthToken work great for Google+ and chrome.identity.launchWebAuthFlow dont with Facebook?
I hope someone have done the same things and can help me.
I have solved the problem. Here is an example https://github.com/blackgirl/Facebook_Oauth2_sample_for_extensions
The "User interaction required." message means that the user needs to sign in to Facebook, and/or approve the requested OAuth scopes. The setting the { interactive: true } flag would allow identity.launchWebAuthFlow to display a window where the user would perform these steps. Since you are passing the { interactive: false } flag, identity.lauchWebAuthFlow fails.
https://www.facebook.com/dialog/oauth is most likely the URL you want to use to start your flow. The client_credentials grants are normally for cases where you are accessing resources owned by your app, rather than resources owned by a specific user.
However if you did want to debug that "Authorization page could not be loaded" case, the way to do it would be to open up chrome://net-internals and look for error responses coming back from Facebook.
chrome.identity.launchWebAuthFlow(
{'url': 'https://www.facebook.com/dialog/oauth?client_id=myclientid&redirect_uri=https://myextensionid.chromiumapp.org/facebook.com=token&scope=user, 'interactive': true},
function(redirect_url) {
/* Extract token from redirect_url */
console.log(redirect_url);
});
//'interactive': true
what will this flag do, will show a dialog box asking username and password
upon successful login it will ask for grant access just press that and
you will receive redirect url in above function body.

undefined response , facebook javascript SDK

I am trying to import some Photos from a Facebook page that I own.
I am following this answer on Stack Overflow, more specifically the Client-Side part.
In the answer 3 steps are suggested.
Add the javascript SDK , which i do.
Something about Authentication but the link is wrong...
A piece of code for rendering the photos.
I skipped the 2nd step , cause I am not sure what to do there and I implemented the code in 3rd step :
FB.api('593959083958735/photos', function(response) {
if(!response || response.error) {
// render error
alert("Noo!!");
} else {
// render photos
alert("Yeah! " + response.status);
}
From here I get the alert "Yeah! undefined". The response is always undefined. I think maybe because I should have done something in the authentication part.
All I am trying to do here , is to import some photos from a public Facebook page. Is this the correct way to do that? If yes why would I need any authentication for it. And what exactly should I do in the authentication part?
From here I get the alert "Yeah! undefined". The response is always undefined. I think maybe because I should have done something in the authentication part.
You are querying the photos from a page, so there is no status field in the response.
All I am trying to do here , is to import some photos from a public Facebook page. Is this the correct way to do that? If yes why would I need any authentication for it.
You don’t need authentication for that – as you can see here in the Graph API Explorer, you get results even without an access token (after clearing the field). And you can see the structure of the response there as well.

Google API Authorisation with JavaScript

Hi I hope someone can help with this because it's driving me crazy right now.
I am trying to replicate this Google API authorisation example https://code.google.com/p/google-api-javascript-client/source/browse/samples/authSample.html on my own web server http://james-gilmore.co.uk/test.html
I have duplicated the code but replaced the clientID and API key with the keys set up in the google api console and I have turned the Google+ api service on in the 'Services' section.
I have the following set up under 'Client ID for web applications'
Redirect URIs: http://james-gilmore.co.uk/oauth2callback
JavaScript origins: http://james-gilmore.co.uk
And I also have the following set up under 'Simple API Access'
Referers: .james-gilmore.co.uk/.
When I run the http://james-gilmore.co.uk/test.html script I can authorise my access but I can see a JS error generated in the console:
'Uncaught TypeError: Cannot read property 'url' of undefined'
Does anyone know what I could be doing incorrectly? I think it may have something to do with the setup of my client ID's and allowed Referers because when I run the following example https://code.google.com/apis/console/?api=plus it works fine
The OAuth authentication appears to be working, but I see an error Access Not Configured being returned from an AJAX call.
Set up your referers as james-gilmore.co.uk/* (with the trailing wildcard, so that it will allow anything starting with your domain name).

Facebook Feed Dialog: Post not showing up on friend's wall even though I get a response id

I am creating an app on facebook and I am trying to post to a friend's wall. I'm using the facebook javascript SDK and the FB.ui method to do this. In short here is my code:
function test() {
var obj = {
method: 'feed',
to: '######'
};
function callback(response) {
alert(response['post_id']);
}
FB.ui(obj, callback);
}
Note that I made this code very simple for testing purposes.
When I run it, a facebook feed dialog opens correctly and at the top says "Post Story to friendsname's Wall". I type in a message and press Share. My alert pops up with a response['post_id'] number. Because there is a response['post_id'] the story should've been posted successfully right? However when I navigate to the friend's wall there is no story. I've tried this multiple times in slightly different ways and haven't been able to get it to work. If I remove the 'to' parameter and simply post to my own wall it -does- work. So for some reason, posting to a friend's wall breaks it?
I know it's a rather broad question but I was wondering if anybody had any ideas why this doesn't work. Thanks
I suspect the problem may be because your application is in sandbox mode - https://developers.facebook.com/docs/ApplicationSecurity/
When testing your apps, place them into Sandbox Mode. This hides your
app entirely from all users who you have not authorized in the
developer app to see the app, for the roles described below. Please
note that when your app is in Sandbox Mode, you cannot call any API
calls on behalf of users who cannot see your app.
Basically, if your application is in sandbox mode, nothing your application does can be seen by users who have not authorized your application or by users that have not been specifically listed in the "roles" within your applications settings.
Remove you application from sandbox mode or add some users to specific "roles" within your application.

Categories

Resources