Facebook API stream.publish "attachment" doesn't work?

Dear Lazyweb,

When using the Test Console at the bottom of the stream.publish page, I can't seem to ever get it to post an "attachment" or any "action_links", even when using the examples on their own Stream Attachments page -- specifically, everything I put in the "attachment" field seems to be ignored (the entry is just posted without it) and I have yet to find anything I can put in the "action_links" field that does not result in "error JSON decoding action_links".

Can any of you make this work at all? If so, please show me an example. I'm trying:

  • attachment:
    { 'href': 'https://www.dnalounge.com/flyers/2010/10/02.html', 'name': 'test', 'caption': 'test2', 'description': 'test3', 'media': [{ 'type': 'image', 'src': 'https://www.dnalounge.com/flyers/2010/10/02-1.jpg', 'href': 'https://www.dnalounge.com/flyers/2010/10/02.html' }]}
  • action_links:
    [{'name':'Google','href':'http://www.google.com/','description':'Google Home Page'}]

Update: Basically, the documentation is full of lies, including all of the examples: you have to use double-quotes around strings, not single-quotes. Single-quotes work only if you are writing your code in Javascript, but not if you are using the Test Console, Perl, or any other non-Javascript-based API that must communicate through JSON.

Tags: , ,

16 Responses:

  1. scullin says:

    This is a really lame detail, but single quotes around strings aren't valid JSON. A really pedantic parser might care.

    ETA: Of course, I just now clicked through to their examples, which are all invalid JSON. Awesome.

    • jwz says:

      Changing all the ' to " in action_links got me a different error message, "action links must have the 'href' and 'text' attributes". Using 'href':"url" or href:"url" gets me the old "error JSON decoding action_links".

    • skington says:

      It takes skill to produce invalid content in a format as simple as JSON.

      Part of me wants to say "Have we learned nothing from Perl, to only allow one form of quoting character, and suffer endless backslashitis in some rare, but annoying, cases?" - but then, JSON is a serialisation format, and keeping it as simple as possible is probably better than making it slightly easier for humans to hand-code it.

      • scullin says:

        Well, yeah, it sort of depends on what angle you come at it from. If your understanding is that JSON is just JavaScript without code, you can get pretty far until you encounter a strict parser. Even understanding that that wasn't the case, I got tripped up recently by including hex encoded numbers, as in 0x00. Valid JavaScript, and JSON.org's own json2.js library handled it without complaint. Perl's JSON library, on the other hand, barfed all over it.

        • skington says:

          Yeah, but the spec of JSON is so simple that it fits on one web page!

          Also, arguably having JSON exactly resemble Javascript was a mistake, as for security reasons you'd still need to parse it for e.g. closures, evals. Being able to just eval a string you're given is tempting, but ultimately a massive security hole waiting to happen.

  2. does the application you have selected in the console have the publish_stream permission?

  3. dossy says:

    I suspect the Test Console is busted. It should/will work from real JS from a FB app. I've been using FB.ui({ method: 'stream.publish' } ...) in a FB app with great success, so I know it DOES work and what you've shown above (the attachment) looks right to me.

    • jwz says:

      I got it working. There are at least two bugs in the docs: first, you can't use ' as a string-quote, you have to use ". Maybe that works in Javascript, but it doesn't work in the test console, nor does it work from Perl (where you have to pass JSON strings, not "Javascript" strings, I guess.) Second, the "to attach a link with a description, use the following example"-example on the Attachments page says "description" where it should say "text".

      As for bugs in the code itself -- if you have a syntax error or other problem in "action_links", you get an error response back from stream.publish. But, if there is anything wrong at all in "attachments", it just silently ignores the error and pretends that the attachments arg wasn't passed in. That's kind of awful behavior; if I did something wrong, it should tell me...