Skip to content

Commit 7f30772

Browse files
feat: auto-refresh Threads access token every 10 days (#41)
Signed-off-by: Tsung-Ju Lii <[email protected]>
1 parent 4f629db commit 7f30772

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Update Threads Access Token
2+
3+
on:
4+
schedule:
5+
- cron: "0 0 */10 * *" # Runs every 10 days at midnight
6+
workflow_dispatch: # Allows manual triggering from GitHub Actions UI
7+
8+
jobs:
9+
update-token:
10+
runs-on: ubuntu-latest
11+
environment: deployment
12+
steps:
13+
- name: Get long-lived Threads access token
14+
run: |
15+
# Use your existing short-lived access token and app secret
16+
SHORT_LIVED_TOKEN=${{ secrets.THREADS_ACCESS_TOKEN }}
17+
APP_SECRET=${{ secrets.THREADS_APP_SECRET }}
18+
19+
# Exchange short-lived token for a long-lived token
20+
RESPONSE=$(curl -i -X GET "https://graph.threads.net/access_token?grant_type=th_exchange_token&client_secret=$APP_SECRET&access_token=$SHORT_LIVED_TOKEN")
21+
22+
# Extract the long-lived access token from the response
23+
LONG_LIVED_TOKEN=$(echo "$RESPONSE" | grep -oP '(?<=access_token":")[^"]*')
24+
25+
if [ -z "$LONG_LIVED_TOKEN" ]; then
26+
echo "Failed to retrieve long-lived token"
27+
exit 1 # Fail fast if the token isn't found
28+
fi
29+
30+
echo "Long-lived access token retrieved successfully."
31+
32+
# Save the long-lived token as a GitHub secret
33+
echo "THREADS_ACCESS_TOKEN=$LONG_LIVED_TOKEN" >> $GITHUB_ENV
34+
35+
- name: Update GitHub Secret with new token
36+
uses: actions/github-script@v7
37+
with:
38+
github-token: ${{ secrets.GITHUB_TOKEN }}
39+
script: |
40+
github.rest.actions.createOrUpdateRepoSecret({
41+
owner: context.repo.owner,
42+
repo: context.repo.repo,
43+
secret_name: "THREADS_ACCESS_TOKEN",
44+
encrypted_value: process.env.THREADS_ACCESS_TOKEN
45+
})

0 commit comments

Comments
 (0)