2

i have de following .gs code

function updateStatus(){
var myId="100006898720726";
var myAppAccess_token="APP-ID|APP-SECRET";
var graphUrl="https://graph.facebook.com/"+myId+"/feed";
var theDate=new Date().toString();
UrlFetchApp.fetch(graphUrl,{method:"post",payload:{
message:"foo\n"+theDate,
access_token:myAppAccess_token
}});
}

AND y have a JSON URL http://pipes.yahoo.com/pipes/pipe.run?_id=a6c5eeff220e55a4fe9607065384817a&_render=json

i want to publish to facebook every new item with description and link

i've tried several ways but the google console always shows something wrong

thanks in advance

andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • So, you would like to iterate through each property in the JSON object, and make a different post for each property? Is there something specific you are trying to get out of that JSON object? So, I guess this isn't about the Facebook API, but about how to get information out of the JSON object? – Alan Wells Dec 07 '14 at 00:07
  • i want to post every item with description and link not for each property – Sergio Quintero Dec 07 '14 at 02:01
  • You need to figure out how that object is structured, and whether it's always the same structure or not. There's a lot of data in there. That object has 232 thousand characters. When I pasted it into a text file, it's 44 pages long. It's a multidimensional object. There are objects inside of objects inside of other objects. You can take a look at this SO question: [How to list the properties of a JavaScript object](http://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object?rq=1) – Alan Wells Dec 07 '14 at 05:16
  • the object is structured by data.value.items and its always the same for each item. i have develop a way to display it on a web page using jquery and it runs ok but i dont know how to fuse the code to assign variables for each item and the code to post it to FB – Sergio Quintero Dec 07 '14 at 15:18
  • I'm assuming that you can get the value you want into a variable. Pass the variable you want to the `updateStatus()` function. I don't know what you know or don't know. I don't know how much programming experience you have. Put an argument to receive in your function: `function updateStatus(argToReceive){ the code };` Then you can use `argToReceive` within the `updateStatus` function. Put something together, even if it's terrible, and post what you have tried. – Alan Wells Dec 07 '14 at 16:11
  • look at source code of this page http://herbalista.hol.es/Eventos.html there i use jquery to assign variables and it displays to web page and de code is ok. the code to post to facebook it is ok too but i dont know how to fusion two codes to post to facebook. – Sergio Quintero Dec 07 '14 at 17:34
  • So, you want to call a Google Apps Script code from your website? You don't need to use Google Apps Script to post to Facebook. You can add the Facebook SDK into your website, similar to how jQuery is referenced. [Facebook Developers Page](https://developers.facebook.com/docs/javascript/quickstart/v2.2) Why do you want to use Apps Script to post to Facebook? – Alan Wells Dec 07 '14 at 18:53
  • because in Google Apps it is an option to run the code automaticly...and in my web page i dont know how to. – Sergio Quintero Dec 07 '14 at 19:25
  • Okay, so you want a certain event to run the code, or the code to execute at a certain time or interval? You can have code run when the webpage loads. Do some research for `onload`. Do an internet search on: `run jquery when page loads`. Do a search on JavaScript `setInterval`. – Alan Wells Dec 07 '14 at 21:08
  • i dont want my code depends of a page load, i have the solution and it is google apps because it is an option to...my ask is how to fusion the code of http://herbalista.hol.es/Eventos.html with the facebook post code. i have to codes and runs ok but i dont know how to use both to autopost when there is a new item – Sergio Quintero Dec 07 '14 at 21:51
  • What is the basic flow of events? 1) How does a new item get put into the website? 2) What do you want to trigger the code to run? You can use Content Service: [Google Documentation](https://developers.google.com/apps-script/reference/content/content-service) Also, you need to pass info to the Google apps script in the URL. Read the doGet Parameters section in this documentation: [Google Documentation](https://developers.google.com/apps-script/guides/ui-service#ServerHandlers) – Alan Wells Dec 07 '14 at 23:11
  • You can run an AJAX request from an HTML Script tag to run the Apps Script macro. Apps Script is considered a macro. You need to use the URL of the Apps Script that has `exec` on the end. You get that URL when you publish the Apps Script as a stand alone app. – Alan Wells Dec 07 '14 at 23:20

2 Answers2

2

The biggest problem is that you are adding the payload to the options, and the message to be posted to Facebook must be part of the Facebook URL. I updated your code at the bottom of the answer.

Here is what I use:

function fncPostSecurly_(argToPost) { //argToPost is the message to post to Facebook that is passed to this function.
  Logger.log("fncPostSecurly ran: " + argToPost);

  var sttsUpdate = argToPost + "your text to post here" + argToPost;
  var fromLogInTkn = cache.get('fbTkn'); // Get facebook token that was stored in cache

  Logger.log("cache FB token: " + fromLogInTkn);

  //This is added security https://developers.facebook.com/docs/graph-api/securing-requests/
  var appsecret_sig = Utilities.computeHmacSha256Signature(fromLogInTkn, "YourAppSecret");
  var optnPostFB = {"method" : "post"};  //
  var PostToFB_URL = "https://graph.facebook.com/FacebookPageOrGroupID/feed?message=" + sttsUpdate + "&access_token=" 
    + fromLogInTkn;


    //Make a post to the Page
    var whatHappened = UrlFetchApp.fetch(PostToFB_URL, optnPostFB );

    //The return from facebook is an object.  Has to be converted to a string.
    var strFrmFbObj = whatHappened.getContentText();
    Logger.log("Return value of Post: " + strFrmFbObj);

    //When a post is successfully made to Facebook, a return object is passed back with an id value.

    var rtrnVerify = strFrmFbObj.indexOf('{\"id\":\"');
    Logger.log("rtrnVerify: " + rtrnVerify);

    if (rtrnVerify != -1) {
      return true;
    } else {
      return false;
    };
 };

I think you need a question mark after /feed, like this:

var graphUrl="https://graph.facebook.com/"+myId+"/feed?";

Also, the content to get posted to Facebook is part of the URL. You are adding the content to be posted to the payload. That's the wrong place to put it.

Fetch takes two parameters. The second parameter can only be: {"method" : "post"}

You've got:

{method:"post",payload:{
  message:"foo\n"+theDate,
  access_token:myAppAccess_token
}}

Again, you can't add the payload to the options. The payload must be part of the URL. That's how Facebook processes the info using the API.

Updated code:

function updateStatus(){
  var myId="100006898720726";

  var myAppAccess_token="APP-ID|APP-SECRET";
  var graphUrl=  "https://graph.facebook.com/" + myId+ "/feed?message=" + "foo\n" + theDate + "&access_token=" 
    + myAppAccess_token;

  var theDate=new Date().toString();

  var optnPostFB = {"method" : "post"};
  UrlFetchApp.fetch(graphUrl, optnPostFB);

}
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • thanks! it is ok, but i have no problems to post to facebook with the original code...the problem is to set a variable for each item on json in order to use the variable on the message to post. here is the json: http://pipes.yahoo.com/pipes/pipe.run?_id=a6c5eeff220e55a4fe9607065384817a&_render=json and in your answer i can not see the json in action.... – Sergio Quintero Dec 06 '14 at 23:41
0
  • Never post any Access Token. the App Secret is called Secret for a reason. I have edited and removed the App Secret, not sure if it´s in any change history though.
  • You can´t post anything with an App Access Token, you need a User Token with the publish_actions permission to post to a User profile
  • publish_actions initially only works for Users with a role in the App (Admin, Developer, Tester). If you want to go public, you have to go through a review process first.
  • The message always must be 100% User generated and you are not allowed to "autopost". Every single post has to be approved by the User. See the platform policy for more information.

Information about Facebook Login: https://developers.facebook.com/docs/facebook-login

Here´s a solution that is actually allowed on Facebook and does not even required User authorization: https://developers.facebook.com/docs/sharing/reference/share-dialog

andyrandy
  • 72,880
  • 8
  • 113
  • 130