Post image to Facebook from system.TemporaryDirectory

I use the code from the SampleCode that comes with Corona Simulator (the project called “Facebook”)

Everything works just fine untill I ´m trying to post from system.TemporaryDirectory

I have done this many times before but now it seems like FB has updated the developer API

[lua]

local attachment = {
     name = “Your image is now on your Facebook page”,
     link = “https://www.facebook.com/pages/3Claws-Games/419060951520798”,
     caption = “Visit 3Claws on Facebook”,
     description = “My Image on FearCam.”,
      source = { baseDir= system.TemporaryDirectory, filename=“imageForFb.jpg”, type=“image” },
   --picture = “http://www.coronalabs.com/links/demo/Corona90x90.png”,
      actions = json.encode( { { name = “My Image”, link = “http://3claws.org” } } )
                                }
              
                        facebook.request( “me/feed”, “POST”, attachment ) 

[/lua]

IT WORKS IF I COMMENT OUT THE source VARIABLE AND USE picture VARIABLE INSTEAD
BUT I HAVE TO PICK FROM THE TemporaryDirectory FOLDER

I have seen the use of type=“photo” as the last arg in the source variable and the facebook.request could maybe be “me/photo” or some other value but then again it wouldn´t be a feed on the users wall i guess…

I have red all I could find on dev facebook API pages, but I cant find the solution.

You cannot make up arbitrary things to pass to a REST API.  Facebook has a well defined set of arguments that it expects on a POST request to the me/feed object.  They are documented here:

https://developers.facebook.com/docs/graph-api/reference/user/#posts

“source” is not something that request is expecting.  There are two ways to attach an image to a post like you are trying to do:

  1. Use a URL to an image on the internet using the “picture” variable.
  2. Use an existing photo from their Facebook library using the “object_attachment” option.  You need the facebook ID for the image before you can use it.

If you want to upload a photo to their library, then that’s a different action than me/feed.  You would want to use me/photos for that, see:
 

https://developers.facebook.com/docs/graph-api/reference/user/#photos

Now that API call takes a source parameter that is a multipoart/form-data type and your source might work for that.  I’ve never done it, but hopefully in the response from uploading the photo, you would get it’s Facebook ID that you could then use in #2 for the me/feed action.

Rob

Thanks for the answer Rob

I finally made it late yesterday night after some hours of testing and failing I experienced all you described, the funny thing is that this worked on my earlier code with me/feed and source = and fetching from my temp folder.  I beleve this has changed in the FB API, you agree?

the solution was, for me, to first do a login check and when event.phase was “login” I used:
[lua] facebook.request( “me” )[/lua]

And for the second chunk:

[lua]

local attachment =
  {
   message = “Your image is now on your Facebook page!”,
   source = { baseDir=system.TemporaryDirectory, filename=“imageForFb.jpg”, type=“image” },
  }
 
   facebook.request( “me/photos”, “POST”, attachment )  – posting the photo
  end

[/lua]

And one very important thing:

I changed the publish type from: 

[lua]facebook.login( appId, listener,  {“publish_action”}  )[/lua]

to:

[lua]facebook.login( appId, listener,  {“publish_stream”}  )[/lua]

Now it worked perfectly! :slight_smile:

The sad part is that I still can´t figure out how to use the “me/feed” with all the options and posting screenshots from temp directory

with links and captiontext and all the other goodstuff.

You cannot make up arbitrary things to pass to a REST API.  Facebook has a well defined set of arguments that it expects on a POST request to the me/feed object.  They are documented here:

https://developers.facebook.com/docs/graph-api/reference/user/#posts

“source” is not something that request is expecting.  There are two ways to attach an image to a post like you are trying to do:

  1. Use a URL to an image on the internet using the “picture” variable.
  2. Use an existing photo from their Facebook library using the “object_attachment” option.  You need the facebook ID for the image before you can use it.

If you want to upload a photo to their library, then that’s a different action than me/feed.  You would want to use me/photos for that, see:
 

https://developers.facebook.com/docs/graph-api/reference/user/#photos

Now that API call takes a source parameter that is a multipoart/form-data type and your source might work for that.  I’ve never done it, but hopefully in the response from uploading the photo, you would get it’s Facebook ID that you could then use in #2 for the me/feed action.

Rob

Thanks for the answer Rob

I finally made it late yesterday night after some hours of testing and failing I experienced all you described, the funny thing is that this worked on my earlier code with me/feed and source = and fetching from my temp folder.  I beleve this has changed in the FB API, you agree?

the solution was, for me, to first do a login check and when event.phase was “login” I used:
[lua] facebook.request( “me” )[/lua]

And for the second chunk:

[lua]

local attachment =
  {
   message = “Your image is now on your Facebook page!”,
   source = { baseDir=system.TemporaryDirectory, filename=“imageForFb.jpg”, type=“image” },
  }
 
   facebook.request( “me/photos”, “POST”, attachment )  – posting the photo
  end

[/lua]

And one very important thing:

I changed the publish type from: 

[lua]facebook.login( appId, listener,  {“publish_action”}  )[/lua]

to:

[lua]facebook.login( appId, listener,  {“publish_stream”}  )[/lua]

Now it worked perfectly! :slight_smile:

The sad part is that I still can´t figure out how to use the “me/feed” with all the options and posting screenshots from temp directory

with links and captiontext and all the other goodstuff.

So what you are saying is that there is no way to post that photo in me/feed?

So what you are saying is that there is no way to post that photo in me/feed?