Chrome Extensions API で遊んでいるときに次の様な現象を確認した。
「ブラウザにてアカウントAでGmail にログインした状態のまま、アカウントBのGmailフィードをXMLHttpRequestで取得すると、アカウントAのエントリーが返ってくる。」
何故?セッションとかクッキーの問題??
とりあえずググるとフィードを取得するURLが2種類ある事に気が付く。
- https://mail.google.com/mail/feed/atom
- https://mail.google.com/gmail/feed/atom
更に、XMLHttpRequestでリスエストのメソッド(“GET” or “POST”)の違いでも挙動が変わる模様。
検証の結果
フィードのURL |
リスエストのメソッド |
結果 |
https://mail.google.com/mail/feed/atom |
GET |
× |
https://mail.google.com/mail/feed/atom |
POST |
× |
https://mail.google.com/gmail/feed/atom |
GET |
× |
https://mail.google.com/gmail/feed/atom |
POST |
○ |
ようするに、gが付いてる方のURLに、(sendが空でも)”POST”でリスエストすれば良いみたい。
仕様をきちんと理解していれば当然なのか、「穴」的な処理なのかはわかりませんが、急に仕様が変わるコトもありえるGoogleさんなので今現在は可能というコトで理解しておこう。
以下、ソース。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function getGmailFeed(mail, pass, callback) {
var xhr = new XMLHttpRequest();
xhr.open( "POST" , url, true , mail, pass);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (callback) {
callback(xhr.responseXML);
}
}
}
}
xhr.send();
}
|
BASIC認証をHTTPヘッダで処理するもよし。
xhr.open( "POST" , url, true );
xhr.setRequestHeader( "Authorization" , "Basic " + window.btoa(mail + ":" + pass));
|
Content-Typeを指定するもよし。
xhr.setRequestHeader( "Content-type" , "application/atom+xml" );
|
以上、備忘録でした。