TNLNYC
February 11, 2022, 7:41pm
1
I’m scratching my head on it (and suspect someone here figured it out but I couldn’t find the answer via search): how do I do an oauth2 call in pixlet
The use case: I’m trying to get information from my Awair sensor ( see Awair Home & OAuth Developer APIs ) on my Tidbyt. But first, I need to get past the oauth layer to get there.
Any ideas?
drudge
February 15, 2022, 12:54pm
2
Hi @TNLNYC
Pixlet has a new schema field to help with OAuth2 flows:
# Schema
Schema provides structure for configuring apps. We use schema inside of the Tidbyt mobile app to allow configuring starlark applications and store the values inside of our database.
In order to supply fields to the mobile app, we call `get_schema()`. When a user saves the installation, we store the results in our database. Every time we render the app, we pass the values into the `config` key/value at the provided identifier.
> Note: Schema is for configuring apps submitted to [Community Apps](https://github.com/tidbyt/community). We're working on adding tighter integration into Pixlet so that pixlet commands make better use of schema.
## Quick Start
Let's add a toggle and a text input to our hello world example. Here it is before we add schema:
```starlark
load("render.star", "render")
def main():
return render.Root(
child = render.Text("Hello, World!"),
)
```
This is a quick start, so let's start with the code and we'll break it down:
This file has been truncated. show original
You can see the example app code here:
load("http.star", "http")
load("render.star", "render")
load("schema.star", "schema")
load("secret.star", "secret")
load("encoding/json.star", "json")
OAUTH2_CLIENT_SECRET = secret.decrypt("your-client-secret")
EXAMPLE_PARAMS = """
{"code": "your-code", "grant_type": "authorization_code", "client_id": "your-client-id", "redirect_uri": "https://appauth.tidbyt.com/your-app-id"}
"""
def main(config):
token = config.get("auth")
if token:
msg = "Authenticated"
else:
msg = "Unauthenticated"
This file has been truncated. show original
I’ve also put together a PagerDuty application that uses OAuth2 in the community repo:
"""
Applet: PagerDuty
Summary: Show PagerDuty Stats
Description: Show PagerDuty incident stats and on-call status.
Author: drudge
"""
load("cache.star", "cache")
load("encoding/base64.star", "base64")
load("encoding/json.star", "json")
load("http.star", "http")
load("time.star", "time")
load("schema.star", "schema")
load("secret.star", "secret")
load("render.star", "render")
DEFAULT_TIMEZONE = "US/Eastern"
DEFAULT_ONLY_LEVEL_1 = False
DEFAULT_SHOW_ONCALL_BAR = True
DEFAULT_HIDE_WHEN_NOT_ONCALL = False
This file has been truncated. show original
Hope this helps! We are available on the discord in the developers channel if you want to discuss some specific challenges you are facing.