Neo Inspiration

Avatar

中の人:jakkrokk (HN統一しました) がphp,javascript,air,flex などなど好き勝手書いてます。仕事は主にWEBシステムの設計と開発、SEO全般など。

AirでGoogleにログインする

Airで というよりも jqueryで というほうが正しいきもしますが、
seo-webmasterではRESTでGoogleにログインするわけですが、
実装はこんなかんじの雑なソースになっております。

    var auth_url = "https://www.google.com/accounts/ClientLogin";
    var auth_id = $("#auth_id").val(); //フォームのIDを取得
    var auth_pass = $("#auth_pass").val(); //フォームのパスワードを取得

    $.ajax({
        url : auth_url,
        type : "POST",
        data  : {"accountType":"GOOGLE","Email":auth_id,"Passwd":auth_pass,"service":"サービス名","source":"ソース名"},
        error : function(XMLHttpRequest, textStatus, errorThrown){
            //エラー処理
        },
        success: function(rs) {
            var token = rs.match(/Auth=([a-zA-Z0-9_\-]+)/); //ここでトークンを切り出して保存する
            $('#SITE-TOKEN').val(token[1]);
        }
    });

これはPOSTなので、いいのですが、次はサイトの認証。
こっちはPUTを使います。

    var webmaster_url = "https://www.google.com/webmasters/tools/feeds/sites/" + encodeURIComponent(サイトID).replace(/\./g,"%2E");
    var token = $('#SITE-TOKEN').val()//認証トークン;

    //送信するXMLを生成する
    var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><atom:entry xmlns:atom='http://www.w3.org/2005/Atom' xmlns:wt='http://schemas.google.com/webmasters/tools/2007'><atom:id>" + site_id + "</atom:id><atom:category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/webmasters/tools/2007#site-info'/><wt:verification-method type='htmlpage' in-use='true'/></atom:entry>";

    $.ajax({
        url : webmaster_url,
        type : "PUT",
        processData : false, //デフォルトがtrueなので必ずきってく
        dataType:"xml", //なくても動いたけど一応 受信コンテンツタイプの指定
        contentType : "application/atom+xml; charset=UTF-8", //コンテンツタイプXMLにしないとデフォルトが urlencodedなのでエラーになる
        data: xml, //送信xml
        beforeSend : function(req) {
            //ログイン時の認証トークンを送る
            req.setRequestHeader('Authorization', 'GoogleLogin auth=' + token);
        },
        error : function(XMLHttpRequest, textStatus, errorThrown){
            //エラー処理
        },
        success: function(rs) {
            //成功時の処理
        }
    });

ポイントとしては processData をきることと、
contentType をちゃんとatom+xmlにすること、
それからbeforeSendでsetRequestHeaderにトークンを埋め込むことかなー

ここらへんのリファレンスみたいなのがあんまなくて(読みかたがわからないだけかも)
適当に大文字にしたり小文字にしたり半角スペース入れたり
してたら通った感じなので、もしかしたら間違ってるかも。

AirでGoogle認証を使うならこんなかんじで試してみてください。

jquery を使ってRESTなAPIにXMLを送りつけてDELETEとか

GoogleのデータAPIを使いまくり&つまりまくりなんですが、
とりあえず基本となるAuth -> XMLを送りつけるところを
jqueryで実装してるとこんな感じになるよというメモ。

$.ajax({
    url : url,
    type : "DELETE", PUTも可(ブラウザに依存)
    contentType : 'application/atom+xml; charset=UTF-8', 設定しないとapplication/x-www-form-urlencodedになっちゃうので必須
    processData :false, 設定しないとapplication/x-www-form-urlencodedなGETクエリに変換にしちゃうので必須
    data: xml_document, ←XMLドキュメント本体
    beforeSend : function(request) {
        request.setRequestHeader('Authorization', 'GoogleLogin auth=' + token); ←Googleだとこんなかんじ
    },
    error : function(XMLHttpRequest, status, error){
        ~~~
    },
    success: function(result) {
        ~~~
    }
});
}

にしてもマイナーなAPI使うとドキュメントがなさすぎてきつい。。

GoogleWebmasterTool のApiリファレンスが・・

GoogleWebmasterTool を使った
サイトマップ一括更新とかのツールを作ってるわけですが、
その中でAPIリファレンスが間違ってるせいで、いきづまってたわけで、
なんで通らないんだろうと・・・

カンでAPIを変えたら通った!

リファレンス間違ってるっつーーーのwwww

これで3時間くらい悩んでしまった。。。

間違ってる箇所はここ

http://code.google.com/intl/ja/apis/webmastertools/docs/2.0/developers_guide_protocol.html

Deleting Sitemaps
You can delete a Sitemap for a specified site. The site and sitemap are identified by their URLs, encoded using URL encoding. For example, to delete the Sitemap http://www.example.com/sitemap.xml for the site http://www.example.com/, send an authenticated DELETE request to:

https://www.google.com/webmasters/tools/feeds/http%3A%2F%2Fwww%2Eexample%2Ecom%2F/
sitemaps/http%3A%2F%2Fwww%2Eexample%2Ecom%2F/sitemaps/http%3A%2F%2Fwww%2Eexample%2Ecom%2Fsitemap%2Exml

肝心のAPIが間違ってます。

正しくはこう

https://www.google.com/webmasters/tools/feeds/http%3A%2F%2Fwww%2Eexample%2Ecom%2F/
sitemaps/http%3A%2F%2Fwww%2Eexample%2Ecom%2Fsitemap%2Exml

直感でURLを直したらうまくいったからいいものの、
DELETE投げるのがjquery経由でミスってるのかとか
調べまくって3時間ほど無駄にしてしまった・・・

とりあえずメールってみました。

結論

Googleだから正しいと思わないこと!

Continue Previous page Next page

Twitter