Finding and fiddling with Slacks APIs
It’s starting to feel a little bit as though the noose is starting to tighten a lot in the IT industry at the moment, whether it be Open Source projects spuriously changing their licenses and pulling the rug from their users to companies that expanded too far and too quickly during the pandemic suddenly contracting. This tightening of the belt has also impacted a bunch of tooling that people have come to depend on as part of their day to day life, or their workflow.
Slack isn’t slack when it comes to the community usage
So! Whats this rambling collection of words about?
Like all of the wonderful platforms as a service out there IRC for money Slack has the various tiers, each unlocking more and more functionality whilst ramping up the various costs associated. A lot of communities (especially in the Open Source world) have been built on the free and open communication that takes place on a Slack instance/workspace devoted to a project or community. But the problem i’m trying to address in this post is the hoops people need to jump though in order to join these communities hosted on Slack.
When the community first starts out, it makes perfect sense that could be a need for simple restraints and/or approval process for joining a community. This is usually handled through an invitation process, if you want to join the community then you need to somehow signal that intent allowing the owner(s) of the community to then invite you to join. However once you start to hit any sort of scale then this simply becomes an unmanageable task, ultimately impacting the growth and health of a hopefully growing community and it is at this point where you would potentially want to open the floodgates as the community (hopefully) exponentially grows.
Note On the side of security, we’re moving to a reactive instead of proactive approach for maintaining community membership (again coming with it’s own challenges).
So what are your options?
The legitimate option
You’re main options without trying to automate anything are to simply select the + Add coworkers
button and create an invite link, this can be set to never expire and is meant to work for 400 people (YMMV). It doesn’t really look like there are many options available to automate this procedure, you can open Slack in a web browser with “developer mode” enabled to capture the API endpoint but i’ve been unable to determine what token can be used.
The endpoint for this API call is https://<workspace>.slack.com/api/users.admin.createSharedInvite
, which should be posted with a FORM with fields such as token
, set_active
however no token I could produce would seem to get this to work (even taken the token used in the browser).
Example code:
1 | token := os.Getenv("SLACK_TOKEN") |
The “other” option
With trying to replicate the Slack client behaviour not working, we’re left to trying less legitimate routes to make it easier for people to join the Slack workspace. So there are a number of Open Source projects that are quite old and unmaintained that make use of an old API (undocumented) endpoint /api/users.admin.invite
(not to be confused with /api/admin.users.invite
, api docs). This endpoint works with a legacy token that you’re no longer able to create (as of 2020).. it turns out invoking the correct sequence will allow you to create a token that can still use this endpoint however!
- Create an App https://api.slack.com/apps
- Create the App from scratch, give it a name (any will do)
- Scroll down and make a copy the
Client ID
and theClient Secret
- Scroll further up and find the Permissions box
- In the Redirect URLs box we will need to enter a bogus URL (make sure it doesn’t actually resolve) such as
https://0.0.0.0/
, ensure you clickSave URLs
- Under the User Token Scopes, select the
Add an OAuth Scope
and addadmin
- Finally scroll back up to OAuth Tokens for Your Workspace and select
Install to Workspace
. - Select the
Allow
button to create our first token!
At this point we will have a User OAuth Token, however this token even with it’s permissions wont work with the users.admin.invite
endpoint 🙄
To generate our actual token we need to do some additional oauth steps!
- Build our specific url!
https://<workspace>.slack.com/oauth?client_id=<Client ID>&scope=admin%2Cclient
- Replace the
<workspace>
with the name of your particular workspace, and change<Client ID>
to the ID from your application. - Paste this URL into a new tab on the logged in browser, you’ll be redirected to a webpage that won’t work (remember the 0.0.0.0). However you should see the new URL in the address bar! (Including a
code=
part of the URL). Grab this code for the next part! - Build our new URL!
https://<workspace>.slack.com/api/oauth.access?client_id=<Client ID>&client_secret=<Client Secret>&code=<code>
- Either open this in a web browser again, or in curl (remember to put the URL in quotes
"
) - The output from this will be some json
{"ok":true,"access_token":"xoxp-...
, theaccess_token
is what we’re after !! - Verify that you can invite people with your new token!
1 | curl -X POST 'https://<workspace>.slack.com/api/users.admin.invite' \ |
A few seconds later…
1 | {"ok":true} |
Just to test, you can run the invite command again and if it’s all working as expected you should receive an error!
1 | {"ok":false,"error":"already_in_team_invited_user"} |
In conclusion
That seems like a lot of effort, but in the end it’s now possible again to build a simple web form allowing people to register themselves to join your Slack workspace!