LoginSignup
6
5

More than 5 years have passed since last update.

Yelp fusion APIの使用まで ~備忘録~

Posted at

Yelp fusion APIを使用するまで

Yelp fusion API を使用していくまでの方法を備忘録的に書いていきます。

Yelp DevelopersDocumentation
https://www.yelp.com/developers/documentation/v3

1.Yelpへの登録,アプリの作成

まず、最初にYelpのユーザ登録を行います。
https://www.yelp.com/developers
上記のページで
右上Sighinでユーザ登録を行います。

その後、https://www.yelp.com/developers/documentation/v3
に行きログインをします。
そして、左メニューのManageAppをクリックし、アプリケーションを作成します。
この時入力は各々適したものを入力します。
これにてアプリの作成は終了です。

2.BearerTokenの取得

1で作成したアプリケーションをもう一度Manage Appの画面から見ると発行されたClient_idとClient_secretを確認することができます。こちらを使ってBearerTokenを取得します。
今回はJavaのHttpURLConnectionを使用して取得していきます。

URL:https://api.yelp.com/oauth2/token

リクエストメソッド:POST

パラメータ

Name Path Description
grant_type String Oath2.0のgrant_typeの中のclient_credentialsを使用
client_id String 先ほど取得したclient_id
client_secret String 先ほど取得したclient_secret
BearerToken.java
String BearerToken(){
   HttpURLConnection con = null;
   String url_str = "https://api.yelp.com/oauth2/token";
   String client_id = "YOUR_CLIENT_ID";
   String client_secret = "YOUR_CLIENT_SECRET";
   try{
      URL url = new URL(url_str);
      con = (HttpURLConnetion)url.openConnection();
      con.setDoInput(true);
      con.setDoOutput(true);
      con.setUseCashes(false);
      con.setRequestMethod("POST");

      String param = "grant_type=client_credentials"
         +"&client_id="+client_id
         +"&client_secret="+client_secret;
      PrintWriter pr = new PrintWriter(con.getOutputStream());
      pr.print(param);
      pr.close();

      BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
      String str = null;
      String line;
      while((line = br.readLine()) != null){
         str += line;
      }
      return str;
   }catch(Exception e){
      e.printStackTrace();
   }
   return null;
}

帰ってくる値strはJSON形式になっています。

return.json
{
  "access_token": "your access token",
  "token_type": "bearer",
  "expires_in": 15552000
}

ここでの各返り値は下のようになっています。

Name Path Description
access_token String Yelp fusion APIに使用するBearerToken
token_type String Tokenの種類
expire_in int 必ず15552000が返ってきます。

これでBearerTokenの取得が完了しました。

3.BearerTokenの使用

ここまでで取得できたBearerTokenを実際に使用していきたいと思います。
同じくJavaのHttpURLConnectionを使用していきます。
ここでは例として
https://www.yelp.com/developers/documentation/v3/get_started
の例の1番上のものを真似ていきます。

Oath2.0に準じ、HttpヘッダにBearerTokenを添付してOath認証を行いAPIを使用していきます。
BearerTokenの認証にJavaではsetRequestPropertyのメソッドを用いてリクエストプロパティにTokenを書き込みます。

URL:https://api.yelp.com/oauth2/token

リクエストメソッド:GET

key value
Authorization Bearer "your_bearer_token"
test.java
String test(){
   HttpURLConnection con = null;
   String url_str = "https://api.yelp.com/v3/autocomplete?text=del&latitude=37.786882&longitude=-122.399972";
   String bearer_token = "YOUR_BEARER_TOKEN";
   try{
      URL url = new URL(url_str);
      con = (HttpURLConnetion)url.openConnection();
      con.setDoInput(true);
      con.setDoOutput(true);
      con.setUseCashes(false);
      con.setRequestMethod("GET");
      con.setRequestProperty("Authorization","Bearer " + bearer_token);
      con.connect();

      BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
      String str = null;
      String line;
      while((line = br.readLine()) != null){
         str += line;
      }
      return str;
   }catch(Exception e){
      e.printStackTrace();
   }
   return null;
}

ここでの返り値strもJSON形式になっています。基本的に返り値はJSON形式です。

return2.json
{
  "terms": [
    {
      "text": "Delivery"
    }
  ],
  "businesses": [
    {
      "id": "YqvoyaNvtoC8N5dA8pD2JA",
      "name": "Delfina"
    },
    {
      "id": "vu6PlPyKptsT6oEq50qOzA",
      "name": "Delarosa"
    },
    {
      "id": "bai6umLcCNy9cXql0Js2RQ",
      "name": "Pizzeria Delfina"
    }
  ],
  "categories": [
    {
      "alias": "delis",
      "title": "Delis"
    },
    {
      "alias": "fooddeliveryservices",
      "title": "Food Delivery Services"
    },
    {
      "alias": "couriers",
      "title": "Couriers & Delivery Services"
    }
  ]
}

上記のJSON形式の値が帰ってくれば成功です!

あとは同様に様々な種類のAPIを叩いて、活用してください。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5