LoginSignup
0
0

[Google Cloud Speech-to-Text API(V2)] Javaでサポートされている場所(ロケーション)の情報を取得する

Posted at

Cloud Speech-to-Text API V2(Java)を使用して、
Cloud Speech-to-Textでサポートされている場所の情報を取得します。

APIを利用する環境の準備から始める場合や、コードを実行する際は、
⧉[Google Cloud Speech-to-Text API(V2)] JavaでSpeech-to-Text APIを使ってみる
を参照ください。

No 目次
1 場所の情報リストを取得
1 スコープ
2 実行
3 レスポンスの内容
2 場所の情報を取得
1 スコープ
2 実行
3 レスポンスの内容

1. 場所の情報リストを取得

サポートされている場所の情報リストを取得します。

1.1. スコープ

OAuth2.0でこのAPIを実行するには、以下のスコープを指定してください。
サービスアカウントで実行する場合はスコープの指定は不要です。

https://www.googleapis.com/auth/cloud-platform

1.2. 実行

public static void main(String[] args) throws Exception{
    try(SpeechClient client = getSpeechClient()){
        ListLocationsRequest.Builder builder = ListLocationsRequest.newBuilder();
        builder.setName("projects/プロジェクトID");
    
        SpeechClient.ListLocationsPagedResponse response = client.listLocations(builder.build());
        for(SpeechClient.ListLocationsPage page : response.iteratePages()){
            for(Location location : page.iterateAll()) {
                System.out.println(location);
            }
        }
        System.out.println(response.getNextPageToken());
    }
}

1.2.1. HTTPリクエスト

GET: https://speech.googleapis.com/v2/projects/{プロジェクトID}/locations
が実行されます。

1.2.2. クエリパラメータ

ListLocationsRequest.Builderのsetメソッドにより、リクエストボディを追加できます。
build()メソッドでListLocationsRequestインスタンスを取得し、
listLocations()に渡します。

メソッド 引数 説明
setName String プロジェクトID
形式:"projects/プロジェクトID"
setFilter String 絞り込むためのフィルター
setPageSize int 取得する最大数
setPageToken String 取得するページトークン

1.3. レスポンスの内容

SpeechClient.ListLocationsPagedResponse

メソッド 戻り値 説明
iteratePages Iterable<SpeechClient.ListLocationsPage> 場所のリスト
getNextPageToken String 次ページのトークン

SpeechClient.ListLocationsPage

メソッド 戻り値 説明
iteratePages Iterable<Location> ロケーション情報

Location

メソッド 戻り値 説明
getName String 場所のリソース名
形式:"projects/{プロジェクトID}/locations/{ロケーションID}"
getLocationId String ロケーションID
getDisplayName String この場所のフレンドリ名
getLabels Map<String,String> 場所のサービス間の属性
getMetadata Any サービス固有のメタデータ

2. 場所の情報を取得

指定したロケーションの場所の情報を取得します。

2.1. スコープ

OAuth2.0でこのAPIを実行するには、以下のスコープを指定してください。
サービスアカウントで実行する場合はスコープの指定は不要です。

https://www.googleapis.com/auth/cloud-platform

2.2. 実行

public static void main(String[] args) throws Exception{
    try(SpeechClient client = getSpeechClient()){
        GetLocationRequest.Builder builder = GetLocationRequest.newBuilder();
        builder.setName(String.format("projects/%s/locations/%s","プロジェクトID","ロケーションID"));
        
        Location response = client.getLocation(builder.build());
        System.out.println(response);
    }
}

2.2.1. HTTPリクエスト

GET: https://speech.googleapis.com/v2/projects/{プロジェクトID}/locations/{ロケーションID}
が実行されます。

2.2.2. クエリパラメータ

クエリパラメータはありません。

2.3. レスポンスの内容

Location

1.3. レスポンスの内容(Location)と同じです。




おしまい。。

0
0
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
0
0