angularjsfacebook-graph-apifacebook-workplace

Using AngularJS $http to access Facebook Workplace Graph API


I am using the Facebook workplace API for posting to group feed. The sample code by Facebook uses requestjs for API calls, which is loaded via RequireJS. Below is the Workplace Graph API access code:

var feed = require('feed-read');
var request = require('request');

var graphapi = request.defaults({
    baseUrl: 'https://graph.facebook.com',
    auth: {
        'bearer' : ACCESS_TOKEN
    }
});

graphapi({
    method: 'POST',
    url: '/' + TARGET_GROUP + '/feed',
    qs: {
        'message': article.title,
        'link': article.link
    }
},function(error,response,body) {
    if(error) {
        console.error(error);
    } else {
        var post_id = JSON.parse(body).id;
        console.log('Published "' + article.title + '": ' + post_id);
        last_check = Date.now();
    }
});

The issue is that I am using AngularJS setup that does not use RequireJS for module loading and I don't want to setup that. Also there is $http object for API calls provided by AngularJS, but can it be used to access the GraphAPI? How should I retrieve the graphapi that this code is using via $http?


Solution

  • What about

    $http.post('https://graph.facebook.com/'+ TARGET_GROUP + '/feed', {
        'message': article.title,
        'link': article.link'
      }, {
        headers: {
          'bearer' : ACCESS_TOKEN
        }
      })
      .then(function(response) {
        console.log(response)
      },function(error) {
        console.log(error)
      });