I’m writing some automation against the astoprint api. There are multiple ways to authenticate, some features are only available when you have authenticated using Oauth2.
This took me longer to figure out that I care to admit, so I’ll share for anyone else trying to implement this.
For this example, I’ll try and get a list of printers from https://api.astroprint.com/v2/printers
Wrong way
The wrong way to do it is to request an access token like so:
# This won't have the privileges you need.
access_token=$(curl -s -X POST \
https://api.astroprint.com/v2/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d "client_secret=${client_secret}&client_id=${client_id}&grant_type=client_credentials" | jq .access_token --raw-output)
And then use the access token to make a request.
curl -X GET \
https://api.astroprint.com/v2/printers \
-H "Authorization: Bearer ${access_token}"
You wil find that you get an empty result
{"data":[]}
Correct way
You will want to use postman to make the requests
First make sure you have setup an app in the astroprint development page
In Postman start by making a get requst to the printers url. Then Get New Access Token
Auth Url and Access Token URL are the numbers from the app page on astroprint.com
The callback url doesn’t matter for now, set it to whatever you want.
Scope needs to be specified. Additional scope information here
An authorization request will then appear