dasha-ria / widget-board

https://widget-board.vercel.app
1 stars 0 forks source link

Litter Robot widget #4

Open dasha-ria opened 10 months ago

dasha-ria commented 10 months ago

https://github.com/natekspencer/pylitterbot/blob/main/pylitterbot/robot/litterrobot4.py

jonasthiesen commented 10 months ago

Looks like we need to do three requests. First one is to "login", the second one is to get a "refresh token", and the third to exchange the long lived "refresh token" for a short lived "access token".

The first request will look something like the following, this will give us a token we can use for the second request:

fetch("https://42nk7qrhdg.execute-api.us-east-1.amazonaws.com/prod/login", {
  method: "POST",
  headers: {
    "x-api-key": "dzJ0UEZiamxQMTNHVW1iOGRNalVMNUIyWXlQVkQzcEo3RXk2Zno4dg=="
  },
  data: JSON.stringify({ email: <email>, password, <password> })
})

The second request to get the refresh token:

fetch("https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=QUl6YVN5Q3Y4NGplbDdKa0NRbHNncXJfc2xYZjNmM3gtY01HMTVR", {
  method: "POST",
  headers: {
    "x-ios-bundle-identifier": "com.whisker.ios",
  },
  data: JSON.stringify({ returnSecureToken: true, token: <token_from_previous_request> })
})

Now that we have the refresh token, we can use this refresh token to get an access token with this request:

fetch("https://securetoken.googleapis.com/v1/token?key=QUl6YVN5Q3Y4NGplbDdKa0NRbHNncXJfc2xYZjNmM3gtY01HMTVR", {
  method: "POST",
  headers: {
    "x-ios-bundle-identifier": "com.whisker.ios",
  },
  data: JSON.stringify({ grantType: "refresh_token", refreshToken: <refresh_token_from_previous_response> })
})
jonasthiesen commented 10 months ago

We also need to extract the user ID from the refresh token, because we need to use user ID to do requests to get the robots.

To fetch the Litter Robot 4s we can do this:

fetch("https://lr4.iothings.site/graphql", {
  method: "POST",
  data: JSON.stringify({
    query: `
      query GetLR4($userId: String!) {{
        getLitterRobot4ByUser(userId: $userId) {LITTER_ROBOT_4_MODEL}
      }}
    `,
    variables: { userId: <user_id_from_refresh_token> },
  })
})