If Meta's token generator ends on a blank page, you can still get your Instagram or Threads access token with the redirect method. It needs no server and works from a phone.
We'll cover Instagram's Unified Login first, then Threads.
Before you start
We assume you've already created your Meta app and hit the blank page. If not, set up your app first with the Instagram guide or the Threads guide.
For Instagram, your app needs the Unified Login configuration from that guide, with a business portfolio connected.
You'll also need:
- An HTTPS address you control for your redirect URL. Your blog's homepage works; it doesn't need any special setup.
- A free GitHub account with a private repository.
How the redirect method works
- Open the login link, sign in, and approve the permissions.
- Meta redirects you to your chosen URL with a one-time
codein the address. - Copy the code and give it to the helper, which exchanges it for an access token using your app credentials.
Set up the helper once
The helper is a GitHub Actions workflow. You run it manually, paste in your authorization code, and copy the token from its log.
- Create a private GitHub repository. Any name will do.
- Go to Settings → Secrets and variables → Actions and add the two secrets listed in the Instagram or Threads section below.
- Choose Add file → Create new file, enter the workflow path from the section below, paste in the code, and commit it. The path starts with a dot:
.github/workflows/. Without the dot, the workflow won't show up.
Keep the repository private. The workflow log contains your access token, which anyone could read if the repository were public.
Instagram (Unified Login)
Step 1: Register your redirect URL and copy the Login URL
- Go to Use cases → Customize → API setup with Unified login in your Meta app dashboard.
- Under 1. Configure your app, select your configuration.
- Enter an HTTPS address you control, such as your blog's homepage, under Redirect URL and click Save.
- Copy the displayed Login URL.
The Login URL contains your configuration ID, which isn't secret.
Note your redirect URL. You'll need it again in Step 3.
Step 2: Add the helper to your repository
Add these two repository secrets:
META_APP_IDMETA_APP_SECRET
In the left menu, near the bottom, open App settings → Basic. Copy the App ID at the top of the page, then click Show next to App secret (Meta may ask for your Facebook password).
Create .github/workflows/get-instagram-token.yml and paste in this workflow:
name: Get Instagram token
on:
workflow_dispatch:
inputs:
code:
description: "The whole address from your browser after you log in"
required: true
redirect_uri:
description: "The redirect URL you saved, exactly as saved"
required: true
jobs:
token:
runs-on: ubuntu-latest
steps:
- name: Exchange the code for a token
env:
APP_ID: ${{ secrets.META_APP_ID }}
APP_SECRET: ${{ secrets.META_APP_SECRET }}
CODE: ${{ inputs.code }}
REDIRECT: ${{ inputs.redirect_uri }}
run: |
set -uo pipefail
CODE="${CODE//[[:space:]]/}" # remove stray spaces or line breaks
REDIRECT="${REDIRECT//[[:space:]]/}"
CODE="${CODE#*code=}" # keep only what follows code=
CODE="${CODE%%#*}" # drop #_=_ at the end
CODE="${CODE%%&*}" # drop &state=...
# Meta can expect the redirect URL with or without a trailing slash, so try both
for r in "${REDIRECT%/}" "${REDIRECT%/}/"; do
answer=$(curl -sS -G https://graph.facebook.com/v27.0/oauth/access_token \
--data-urlencode "client_id=$APP_ID" \
--data-urlencode "client_secret=$APP_SECRET" \
--data-urlencode "redirect_uri=$r" \
--data-urlencode "code=$CODE")
token=$(echo "$answer" | jq -r '.access_token // empty')
if [ -n "$token" ]; then
echo "Your access token. Copy it, then delete this run:"
echo "$token"
exit 0
fi
done
echo "Meta did not return a token. Its answer was:"
echo "$answer"
exit 1
The exchange runs on GitHub's servers, so your app secret never touches your browser. At this step Meta can expect your redirect URL with or without a trailing slash, so the workflow tries both.
Step 3: Log in and run the helper
The code expires quickly. Open your repository's Actions page in a second tab first, so you can paste the code as soon as you have it.
- Open the Login URL in your browser. If only Facebook login appears, add
&prompt=loginto the end of the URL. - Sign in with Instagram or Facebook.
- On Select the business assets to share, choose your business portfolio and Instagram account, then click Continue. The ad account is optional.
- On Review what you'll share, click Save.
- Meta redirects you to your URL. The page may load normally, show an error, or stay blank. That's fine. Copy the whole address from your browser. It looks like
https://yoursite.com/?code=YOUR_CODE&state=%3CSTATE%3E#_=_. The helper picks out the code for you, so it doesn't matter what Meta adds at the end, such as#_or#_=_. - Open Actions in your repository. Select Get Instagram token, tap Run workflow, and paste the whole address into the first box. Enter your redirect URL in the second box and run it.
- Open the completed run and expand Exchange the code for a token. Your access token appears on the last line.
This method generates a system-user access token. Its expiry depends on your configuration.
If your configuration uses a user access token instead, you'll receive a short-lived token. Exchange it for a long-lived one before it expires.
Threads
Threads uses separate credentials and a different token flow. Its app ID, app secret, and access tokens aren't shared with Instagram.
We also assume you've already added yourself as a Threads Tester, as covered in the Threads guide.
Find your Threads credentials under Use cases → Access the Threads API → Customize → Settings. You'll also see them on App settings → Basic, in the second box.
Use the Threads app ID and Threads app secret, not the App ID and App secret at the top of that page.
Step 1: Register your redirect URL
On the Threads API Settings page, add an HTTPS address you control, such as your blog's homepage, under Redirect Callback URLs and save it.
Leave the Uninstall and Delete callback URLs blank. You don't need them for your own account.You'll need this URL again when you run the workflow. Meta can expect it with or without a trailing slash, so the workflow tries both.
Step 2: Open the authorization URL
Open this URL in your browser, replacing the example values with your own:
https://threads.com/oauth/authorize
?client_id=YOUR_THREADS_APP_ID
&redirect_uri=YOUR_REDIRECT_URI
&scope=threads_basic,threads_content_publish
&response_type=code
threads_basic is required. The threads_content_publish permission allows your app to publish posts.
On Android, open the URL in your browser rather than the Threads app.
After you approve the permissions, Threads redirects you to your URL with an authorization code. Copy the whole address from your browser.
The code expires, so run the helper soon after you approve. If it fails, log in again for a fresh code.
Step 3: Run the helper
Add these two repository secrets using the Threads credentials from the Settings page:
THREADS_APP_IDTHREADS_APP_SECRET
Create .github/workflows/get-threads-token.yml and add this workflow:
name: Get Threads token
on:
workflow_dispatch:
inputs:
code:
description: "The whole address from your browser after you approve"
required: true
redirect_uri:
description: "The redirect URL you saved, exactly as saved"
required: true
jobs:
token:
runs-on: ubuntu-latest
steps:
- name: Exchange the code for a 60-day token
env:
APP_ID: ${{ secrets.THREADS_APP_ID }}
APP_SECRET: ${{ secrets.THREADS_APP_SECRET }}
CODE: ${{ inputs.code }}
REDIRECT: ${{ inputs.redirect_uri }}
run: |
set -uo pipefail
CODE="${CODE//[[:space:]]/}" # remove stray spaces or line breaks
REDIRECT="${REDIRECT//[[:space:]]/}"
CODE="${CODE#*code=}" # keep only what follows code=
CODE="${CODE%%#*}" # drop #_ at the end
CODE="${CODE%%&*}" # drop anything after the code
# Meta can expect the redirect URL with or without a trailing slash, so try both
token=""
for r in "${REDIRECT%/}" "${REDIRECT%/}/"; do
short=$(curl -sS -X POST https://graph.threads.com/oauth/access_token \
-F "client_id=$APP_ID" -F "client_secret=$APP_SECRET" \
-F "grant_type=authorization_code" -F "redirect_uri=$r" -F "code=$CODE")
token=$(echo "$short" | jq -r '.access_token // empty')
user=$(echo "$short" | jq -r '.user_id // empty')
if [ -n "$token" ]; then break; fi
done
if [ -z "$token" ]; then
echo "Threads did not return a token. Its answer was:"
echo "$short"
exit 1
fi
long=$(curl -sS -G https://graph.threads.com/access_token \
--data-urlencode "grant_type=th_exchange_token" \
--data-urlencode "client_secret=$APP_SECRET" \
--data-urlencode "access_token=$token")
longtoken=$(echo "$long" | jq -r '.access_token // empty')
if [ -z "$longtoken" ]; then
echo "Threads did not return a long-lived token. Its answer was:"
echo "$long"
exit 1
fi
echo "Your Threads user ID: $user"
echo "Your access token (valid for about 60 days). Copy it, then delete this run:"
echo "$longtoken"
Open Actions, select Get Threads token, and tap Run workflow.
Paste the whole address into the first box. Enter your redirect URL in the second box. Then run the workflow.
Once it finishes, expand Exchange the code for a 60-day token. The last lines show your Threads user ID and access token.
Step 4: Refresh your token
You can refresh the token for another 60 days once it's at least 24 hours old, as long as it hasn't expired. Run:
curl -G https://graph.threads.com/refresh_access_token \
-d "grant_type=th_refresh_token" \
-d "access_token=YOUR_LONG_LIVED_TOKEN"
Meta has moved Threads from threads.net to threads.com, although some documentation still mentions both. If a request fails, check Meta's current documentation for the correct host.
Clean up
Before using your token, secure it and remove the temporary credentials and workflow.
- Save the token in a password manager or secure secret. Don't share it in chats or screenshots.
- Open the workflow run, tap the three-dot menu, and select Delete workflow run.
- Delete the workflow file and the temporary app ID and app secret secrets.
- If your app secret was exposed, reset it in the dashboard.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
| "Matching code was not found or was already used", "This authorization code has expired", or the workflow says no token was returned | The code expired, was already used, or the app credentials are wrong. | Start the login again and paste the address from the new login right away. Check that you're using the Meta app credentials for Instagram and the Threads credentials for Threads. |
| The workflow doesn't appear in the Actions tab | The file isn't in .github/workflows/, usually because the dot at the start of .github is missing. |
Open the file, tap the pencil icon to edit it, correct the path in the filename box, and commit. |
| The login rejects your redirect URL | The URL doesn't match the saved value. | Copy it from the saved list, including any trailing slash. |
| "Error validating verification code. Please make sure your redirect_uri is identical…" | The redirect URL in the second box differs from the one used at login by more than a trailing slash. | Check it for typos, such as http instead of https, or a different path. Then get a fresh code and run the workflow again. |
| Your redirect page is blank or shows an error | The page doesn't need to display anything. | Copy the address from your browser and extract the authorization code. |
| Only Facebook login appears (Instagram) | You're already logged in to Facebook in that browser. | Add &prompt=login to the Login URL. |
| The Threads login opens in the app and fails (Android) | Your phone opened the URL in the Threads app. | Open the authorization URL directly in your browser. |
You can now generate your Instagram or Threads access token without Meta's token generator. Store it securely and track its expiry date.
What's next?
Put your token to work: learn how to automate posting on "Instagram" or "Threads".



Comments