speg03の雑記帳

主に未来の自分のために試したことなどを記録しています

JavaScriptだけでTwitterにpostは無理そう?

前回、statusが取れるようになって喜んでいたのもつかの間、どうにもpostができなくって困っていた。

試しに作ってみたのはこんな感じ。

// 'u'が押されたらプロンプトを表示して入力された文字列を発言する(つもり)

$(document).keypress(function (e) {
    execute_command(String.fromCharCode(e.which));
});

function execute_command(c) {
    switch (c) {
    case 'u':
        var message = prompt();
        if (message != '') update_status(message);
        break;
    default:
    }
}

function update_status(message) {
    $.ajax({
        type: 'POST',
        url: 'http://twitter.com/statuses/update.json',
        username: 'USERNAME',
        password: 'PASSWORD',
        data: {
            status: encodeURIComponent(message)
        },

        complete: function (xhr, text) { alert('complete: ' + text); },
        error: function (xhr, text, err) { alert('error: ' + err); }
    });
}

これを実行すると "Access to restricted URI denied" などと言われてpostできない。どうもクロスドメインとかの関係だろうけど、結局どうすればいいのかわからなかった。

vimperatorのtwitter.jsとか、Jetpackだとできてるみたいなので、次はJetpackあたりで試してみようと思う。