2

Greasemonkey スクリプトで oauth.js と sha1.js を使用しています。

// ==UserScript==
// @name        twitter_api
// @namespace   twitter
// @include     *
// @version     1
// @require http://oauth.googlecode.com/svn/code/javascript/oauth.js
// @require http://pajhome.org.uk/crypt/md5/sha1.js
// ==/UserScript==

var url = "https://api.twitter.com/1.1/statuses/update.json";
var accessor = {
  token: "XXX",
  tokenSecret: "XXX",
  consumerKey : "XXX",
  consumerSecret: "XXX"
};
var message = {
  action: url,
  method: "POST",
  parameters: {status: "apitest"}
};
OAuth.completeRequest(message, accessor);
OAuth.SignatureMethod.sign(message, accessor);
var authorization = "OAuth oauth_consumer_key=\"" + encodeURIComponent(OAuth.getParameterMap(message.parameters).oauth_consumer_key) + "\""
            + ", oauth_nonce=\"" + encodeURIComponent(OAuth.getParameterMap(message.parameters).oauth_nonce) + "\""
            + ", oauth_signature=\"" + encodeURIComponent(OAuth.getParameterMap(message.parameters).oauth_signature) + "\""
            + ", oauth_signature_method=\"HMAC-SHA1\""
            + ", oauth_timestamp=\"" + encodeURIComponent(OAuth.getParameterMap(message.parameters).oauth_timestamp) + "\""
            + ", oauth_token=\"" + encodeURIComponent(OAuth.getParameterMap(message.parameters).oauth_token) + "\""
            + ", oauth_version=\"1.0\"";

POST リクエストで Authorization ヘッダーを使用する:

GM_xmlhttpRequest({
  method: "POST",
  url: url,
  data: "status=apitest",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": authorization
  },
  onload: function(response) {
    alert(response.responseText);
  }
});

POST リクエストでのみ GET パラメータを使用する:

GM_xmlhttpRequest({
  method: "POST",
  url: url + '?' + OAuth.formEncode(message.parameters),
  onload: function(response) {
    alert(response.responseText);
  }
});

POST リクエストでのみ POST パラメータを使用する:

GM_xmlhttpRequest({
  method: "POST",
  url: url,
  data: OAuth.formEncode(message.parameters),
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  onload: function(response) {
    alert(response.responseText);
  }
});

Twitter が Authorization ヘッダーを送信する必要があると明確に言っているのに、この 3 つの AJAX リクエストが機能するのはなぜですか? https://dev.twitter.com/docs/auth/authorizing-request

GET、POST、HTTP ヘッダーの承認の両方が受け入れられる場合、HTTP ヘッダーを優先する必要があるのはなぜですか? HTTP ヘッダーの方が安全なのでしょうか、それとも API 呼び出しであるため問題ではないのでしょうか?

4

0 に答える 0