2013年2月13日水曜日

GoogleのURL短縮サービスをJavaScriptから使う

サイト放ったらかしすぎてヤバイ。


Googleの短縮URLサービスの利用方法。


1.まずGoogle APIs Consoleから、Google URL Shortenerを「ON」にしてKEY確認。

https://code.google.com/apis/console


2.上記サービスを使いたいページのヘッダーで下記を呼ぶ。

下記を読み込めば、GoogleのAPIに簡単アクセスできて便利。たぶん。

<script src="https://apis.google.com/js/client.js?onload=load"></script>









3.下記JavaScriptを実行すると、取得できる。

// ---------------------------------------------------------
// Googleサービスで短縮URL取得
// ---------------------------------------------------------
function getShortUrl(url, callBack){
    var key = "(Googleから取得したキー)";
    if(!gapi) return callBack(false);
    gapi.client.setApiKey(key);
    gapi.client.load('urlshortener', 'v1', function(){
        var req = gapi.client.urlshortener.url.insert({
            resource: {
                'longUrl': url
            }
        });
        req.execute(function(data){
            if(data.error) return callBack(false);
            callBack(true, data.id);
        });
    });
};
// ---------------------------------------------------------
// 取得例
// ---------------------------------------------------------
getShortUrl("http://google.com", function(f, d){
    // 取得失敗した
    if(!f) return false;
    // 取得キーをアラート表示
    alert(d);
});
// ---------------------------------------------------------

1 件のコメント:

  1. Your Affiliate Money Making Machine is waiting -

    And getting it set up is as simple as 1, 2, 3!

    Here's how it all works...

    STEP 1. Input into the system which affiliate products the system will advertise
    STEP 2. Add some PUSH BUTTON TRAFFIC (this LITERALLY takes 2 minutes)
    STEP 3. Watch the affiliate products system grow your list and sell your affiliate products for you!

    So, do you want to start making money???

    Check it out here

    返信削除