We have published a blog on Integration With Facebook Using Grails OAuth Plugin through which a user can authenticate with Facebook and get some user information. Now one common requirement is post some message or share some link on Facebook. We can easily achieve this using grails OAuth plugin.

  • Authenticate With Facebook
    Use above mentioned link to install grails OAuth plugin and authenticate users.
  • Post A Message
    Use following code to post some content on Facebook-

    postOnWall(Token facebookAccessToken, Map params) {
      Token facebookAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('facebook')]
    
      String url = "https://graph.facebook.com/me/feed?message=${someMessage?.encodeAsURL()}&link=${someLink}&picture=${somePictureLink}"
      def facebookResource = oauthService.postFacebookResource(facebookAccessToken, url)
      def facebookResponse = JSON.parse(facebookResource?.getBody())
    
      if (facebookResponse.id) {
        flash.message = 'Message posted successfully'
      } else {
        flash.error = "Something went wrong..,. ${facebookResponse.error}"
      }
    }
    

Here note that you have to send something message, link and/or picture otherwise you got a error message in the response.


I have deployed a demo project in Heroku.
See this code in action (DEMO).


Hope this helps 🙂 .