New to API Builds in Google Sheets - How to call FatSecret API into Google Sheets project

321 views
Skip to first unread message

Keshan Harris

unread,
Dec 18, 2015, 1:55:14 AM12/18/15
to FatSecret Platform API
It has been ~10 years since I last coded, so bare with me on this.

I want to integrate the fatsecret API into a Google sheet so that I can pull nutritional information for various foods into the sheet as needed in building out a meal planner. I have no earhtly clue how to even consume the API within Google Sheets or where to start. Some questions:

Starting simply: Connecting to OAuth service to access the API:

FatSecret has their instructions on how to access the APO through OAuth, but so does Google Scripts guides...assuming that Google is the consumer and fatsecret the provider, how do i structure the request to go through OAuth and actually access the FatSecret information?

I'll start there...I need some basics on it I know. Really will appreciate the help of the community! :)

Thanks,
Keshan 
Message has been deleted

Keshan Harris

unread,
Dec 21, 2015, 5:09:24 PM12/21/15
to FatSecret Platform API
Here is what I've accomplished so far. I removed my consumer/secret obviously:

function makeRequest() {
  var fsService = getFatSecretService();
  var response = fsService.fetch('http://platform.fatsecret.com/rest/server.api');
}

function getFatSecretService() {
  // Create a new service with the given name. The name will be used when
  // persisting the authorized token, so ensure it is unique within the
  // scope of the property store.
  return OAuth1.createService('fatsecret')
      // Set the endpoint URLs.
      .setAccessTokenUrl('http://www.fatsecret.com/oauth/access_token')
      .setRequestTokenUrl('http://www.fatsecret.com/oauth/request_token')
      .setAuthorizationUrl('http://www.fatsecret.com/oauth/authorize')

      // Set the consumer key and secret.
      .setConsumerKey('****')
      .setConsumerSecret('****')

      // Set the name of the callback function in the script referenced
      // above that should be invoked to complete the OAuth flow.
      .setCallbackFunction('authCallback')

      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getUserProperties());
}

function showSidebar() {
  var fsService = getFatSecretService();
  if (!fsService.hasAccess()) {
    var authorizationUrl = fsService.authorize();
    var template = HtmlService.createTemplate(
        '<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>. ' +
        'Reopen the sidebar when the authorization is complete.');
    template.authorizationUrl = authorizationUrl;
    var page = template.evaluate();
    DocumentApp.getUi().showSidebar(page);
  } else {
    
  }
}

function authCallback(request) {
  var fsService = getFatSecretService();
  var isAuthorized = fsService.handleCallback(request);
  if (isAuthorized) {
    return HtmlService.createHtmlOutput('Success! You can close this tab.');
  } else {
    return HtmlService.createHtmlOutput('Denied. You can close this tab');
  }
}

Any help on where I'm going wrong would be greatly appreciated. :D
Reply all
Reply to author
Forward
Message has been deleted
0 new messages