LoginSignup
2
0

More than 1 year has passed since last update.

Google Nest CamのAPIを叩くまで

Last updated at Posted at 2022-12-10

はじめに

最近買ったGoogleのスマートカメラをAPI経由でアクセスしてみたいと思ってやってみると意外と手順が面倒だったので、それについてまとめます。

準備

基本的には公式のGet Startedに沿った内容となります

アプリインストールやユーザー登録など

実際に開発に着手する前に以下の準備が必要となります。

GCPの設定

APIを使用するために、Smart Device Management APIの有効化OAuth 2.0 のClient IDが必要になります。以下のEnable ths API and get an OAuth 2.0 Client IDというところをクリックするとダイアログ上で設定が完結します。

image.png

OAuthの設定のところではウェブサーバーを選択します。リダイレクト先の設定も必要となるので、一旦https://www.google.com/とかにしておきます(実際は自分が作成するウェブアプリのURLにします。)。

また、OAuth2.0などのクレデンシャル情報が最後に表示されているので、それをメモします。ダウンロードもしておくことをオススメします。

Device Accessでプロジェクト作成

Device accessのコンソールにアクセスして、Create projectからプロジェクトを作成します。
このときにOAuth2.0のクライアントIDが必要になるので、メモした内容を入力します。

作成が終われば、プロジェクトIDがUUIDの形式で発行されるのでそれをメモします。

アカウントをリンクする

Nest CamのAPIにアクセスするためには、Googleアカウントとの連携が必要となります。
具体的には、以下のようなURLにブラウザでアクセスしてもらいます。ここでは、oauth2-client-idproject-idを実際の値に書き換えます。

https://nestservices.google.com/partnerconnections/{project-id}/auth?redirect_uri=https://www.google.com&access_type=offline&prompt=consent&client_id={oauth2-client-id}&response_type=code&scope=https://www.googleapis.com/auth/sdm.service

アクセスをするとGoogleの同意画面が出てくるので、許可をします。

image.png

次にログインしたあとに警告が出ますが、続行をします。

image.png

無事リダイレクトができたらブラウザ上のURLを確認します。そしたら以下のようなクエリ付きのURLになっていると思います。

リダイレクト先のURL?code=長い文字列&scope=https://www.googleapis.com/auth/sdm.service

code=長い文字列が認可コード(Authrization code)となるのでメモをしておきます。

アクセストークン取得

Nest CamのAPIを使うためには、認可コードではなくアクセストークンが必要となります。そのために以下のAPIを実行します。

curl -L -X POST 'https://www.googleapis.com/oauth2/v4/token?client_id={oauth2-client-id}&client_secret={oauth2-client-secret}&code={authorization-code}&grant_type=authorization_code&redirect_uri=https://www.google.com'

client_id={oauth2-client-id} client_secret={oauth2-client-secret} code={authorization-code}を実際の値に書き換えます。

以下のようなレスポンスが返ってきます。

{
  "access_token": "access-token",
  "expires_in": 3599,
  "refresh_token": "refresh-token",
  "scope": "https://www.googleapis.com/auth/sdm.service",
  "token_type": "Bearer"
}

access-token refresh-tokenの実際の値をメモしておきます。

デバイスのリストを受け取ってみる

以下のAPIを実行して、デバイスのリストを受け取ってみます。

curl -X GET 'https://smartdevicemanagement.googleapis.com/v1/enterprises/{project-id}/devices' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer {access-token}'

project-id access-tokenを実際の値に書き換えます。実行すると以下のようなレスポンスが返ってきます。

{
  "devices": [
    {
      "name": "enterprises/project-id/devices/device-id",
      "type": "sdm.devices.types.device-type",
      "traits": { ... },
      "parentRelations": [
        {
          "parent": "enterprises/project-id/structures/structure-id/rooms/room-id",
          "displayName": "device-room-name"
        }
      ]
    }
  ]
}

さいごに

以上がNest CamのAPIにアクセスするまでの道のりでした。

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