-
Notifications
You must be signed in to change notification settings - Fork 5
DataAPI SDK japanese MT.DataAPI Basic usage
Taku AMANO edited this page Feb 25, 2014
·
12 revisions
ステータスが「公開」にされているブログ記事などのデータは、以下のように簡単に取得することができます。
var api = new MT.DataAPI({
baseUrl: "https://your-host/mt/mt-data-api.cgi",
clientId: "your-client-id"
});
api.listEntries(siteId, function(response) {
if (response.error) {
// エラー処理
return;
}
for (var i = 0; i < response.items.length; i++) {
var entry = response.items[i];
// ブログ記事を表示
}
});
ステータスが「未公開(原稿)」にされているブログ記事などの公開されていないデータは、取得する際に DataAPI の認証画面でユーザー名とパスワードによる認証を得る必要があります。
var api = new MT.DataAPI({
baseUrl: "https://your-host/mt/mt-data-api.cgi",
clientId: "your-client-id"
});
// 認証を試みる
api.getToken(function(response) {
if (response.error) {
if (response.error.code === 401) {
// まだ認証されていない場合
// 認証が成功した場合、現在の URL に戻ってくる
location.href = api.getAuthorizationUrl(location.href);
}
else {
// エラー処理
}
}
else {
// 認証済みの場合
api.listEntries(siteId, {status: 'Draft'}, function(response) {
if (response.error) {
// エラー処理
return;
}
// 未公開のブログ記事を閲覧する権限を持っていれば、記事一覧が取得されてる
for (var i = 0; i < response.items.length; i++) {
var entry = response.items[i];
// ブログ記事を表示
}
});
}
});
ユーザー自身の情報やステータスが「未公開(原稿)」にされているブログ記事などの公開されていないデータは、取得する際に DataAPI の認証画面でユーザー名とパスワードによる認証を得る必要があります。
var api = new MT.DataAPI({
baseUrl: "https://your-host/mt/mt-data-api.cgi",
clientId: "your-client-id"
});
api.getUser('me', function(response) {
if (response.error) {
if (response.error.code === 401) {
// まだ認証されていない場合
// 認証が成功した場合、現在の URL に戻ってくる
document.getElementById('login').href = api.getAuthorizationUrl(location.href);
return;
}
// エラー処理
return;
}
var user = response;
document.getElementById('login').style.display = 'none';
document.getElementById('username').textContent = user.displayName;
});
未認証時の処理をまとめて記述するために、「authorizationRequired」イベントを利用することもできます。
var api = new MT.DataAPI({
baseUrl: "https://your-host/mt/mt-data-api.cgi",
clientId: "your-client-id"
});
if (window.location.hash === '#_login') {
window.parent.createEntry(window);
}
else {
api.on('error', function(response) {
if (response.error.code === 401) {
return;
}
// エラー処理
});
api.on('authorizationRequired', function(response) {
// まだ認証されていない場合
// 認証が成功した場合、現在の URL に戻ってくる
window.open(api.getAuthorizationUrl(location.href));
});
function createEntry(win) {
api.createEntry(siteId, entryData, function(response) {
if (response.error) {
return;
}
if (win) {
win.close();
}
var entry = response;
// 作成されたブログ記事を表示
});
}
createEntry();
}
Although some requests are needed before attestation in the previous version, you can also reduce request by writing as follows.
var api = new MT.DataAPI({
baseUrl: "https://your-host/mt/mt-data-api.cgi",
clientId: "your-client-id"
});
function goToAuthorizationUrl() {
// 認証が成功した場合、現在の URL に戻ってくる
document.getElementById('login').href = api.getAuthorizationUrl(location.href);
}
if (! this.getTokenData()) {
goToAuthorizationUrl();
}
api.getUser('me', function(response) {
if (response.error) {
if (response.error.code === 401) {
// 以前取得したトークンが期限切れになっているなどの理由で再認証が必要
goToAuthorizationUrl();
return;
}
// エラー処理
return;
}
var user = response;
document.getElementById('login').style.display = 'none';
document.getElementById('username').textContent = user.displayName;
});