# BootCamp の Schedule
# Google APIs Discovery Service
Google URL Shortener API
ログインした状態で http://goo.gl/ に行くと、自分が作った Short URL の一覧がみれる


Details をクリックすると、解析結果がみれる

---
Google API を使うには、API キーが必要 Google API Console で取得する
最初にプロジェクトを作成する
左側の Services をクリックして、使いたい Service を ON にする

左側の API Access をクリックして、API key を確認できる。

Eclipse に Google Plugin for Eclipse を入れる
新しい Web Application Project を作成し、

作成したプロジェクトを選択した状態で、[Add Google APIs...] をクリック

追加したい Google API を選択して [Finish] をクリック
今回は Google URL Shortener API を選択する

プログラムを書く
package yanzm.example.shortener.sample;
import java.io.IOException;
import javax.servlet.http.*;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.urlshortener.v1.Urlshortener;
import com.google.api.urlshortener.v1.model.Url;
@SuppressWarnings("serial")
public class GoogleUrlShortenerTestServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
HttpTransport transport = new NetHttpTransport();
final JsonFactory jsonFactory = new JacksonFactory();
Urlshortener shortener = new Urlshortener("yanzm test", transport, jsonFactory);
shortener.accessKey = "YOUR API KEY";
Url returnUrl = shortener.url.get("http://goo.gl/ERwbw").execute();
resp.setContentType("text/plain");
resp.getWriter().println(returnUrl.longUrl);
}
}
実行すると、goo.gl/ERwbw を展開した URL が表示される
# Urlshortener の使い方がさっぱりわからなくて、
# 結局 UrlShortener とか UrlShortener.Url とかのコードをみて理解した。
0 件のコメント:
コメントを投稿