在谷歌应用程序脚本上从json发布到Facebook

时间:2014-12-06 20:41:52

标签: javascript json facebook google-apps-script

我有以下.gs代码

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
}});
}

和y有一个JSON网址http://pipes.yahoo.com/pipes/pipe.run?_id=a6c5eeff220e55a4fe9607065384817a&_render=json

我想在Facebook上发布每个带有说明和链接的新项目

我尝试了几种方法,但谷歌控制台总是显示错误

提前致谢

2 个答案:

答案 0 :(得分:2)

最大的问题是您要将有效负载添加到选项中,要发布到Facebook的消息必须是Facebook URL的一部分。我在答案的底部更新了您的代码。

以下是我使用的内容:

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;
    };
 };

我认为你需要/feed之后的问号,如下所示:

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

此外,发布到Facebook的内容是URL的一部分。您正在添加要发布到有效负载的内容。这是错误的地方。

Fetch有两个参数。第二个参数只能是:{"method" : "post"}

你有:

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

同样,您无法将有效负载添加到选项中。有效负载必须是URL的一部分。这就是Facebook使用API​​处理信息的方式。

更新的代码:

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);

}

答案 1 :(得分:0)

  • 绝不发布任何访问令牌。 App Secret被称为Secret是有原因的。我已经编辑并删除了App Secret,但不确定它是否在任何更改历史记录中。
  • 您无法使用应用访问令牌发布任何内容,您需要具有publish_actions权限的用户令牌才能发布到用户个人资料
  • publish_actions最初仅适用于在App(管理员,开发人员,测试人员)中具有角色的用户。如果你想上市,你必须首先通过review process
  • 消息始终必须为100%用户生成并且您不能“自动提交”。每个帖子都必须得到用户的批准。有关详细信息,请参阅platform policy

有关Facebook登录的信息:https://developers.facebook.com/docs/facebook-login

这是Facebook上实际允许的解决方案,甚至不需要用户授权:https://developers.facebook.com/docs/sharing/reference/share-dialog

相关问题