Skip to content

DataAPI SDK english MT.DataAPI Basic usage

Taku AMANO edited this page Jul 5, 2013 · 10 revisions

Basic usage

Fetch public data

You can fetch public data (e.g. published entry), without authorization.

var api = new DataAPI({
  baseUrl:  "https://your-host/your-mt-api.cgi",
  clientId: "your-client-id"
});

api.listEntries(siteId, function(response) {
  if (response.error) {
    // Handle error
    return;
  }

  for (var i = 0; i < response.items; i++) {
      var entry = response.items[i];
      // Render an entry
  }
});

Fetch private data

You can fetch private data, with authorization.

var api = new DataAPI({
  baseUrl:  "https://your-host/your-mt-api.cgi",
  clientId: "your-client-id"
});

// Try to authenticate
api.getToken(function(response) {
  if (response.error) {
    if (response.error.code === 401) {
      // You have not been authenticated yet.

      // You will return to current URL after authorization succeeded.
      location.href = api.getAuthorizationUrl(location.href);
    }
    else {
      // Handle error
    }
  }
  else {
    // You have been authenticated.
    api.listEntries(siteId, {status: 'Draft'}, function(response) {
      if (response.error) {
        // Handle error
        return;
      }
  
      // Fetched a list of drafts if you have a permission reading these.
      for (var i = 0; i < response.items; i++) {
          var entry = response.items[i];
          // Render an entry
      }
    });
  }
});
Clone this wiki locally