Skip to content

Apps Script API returns 401 "invalid authentication credentials" while Drive API works with same credentials #205

Open

Description

@tjmpn2

gws version: 0.4.4
OS: macOS (Apple Silicon, MacBook Pro)
Account type: Personal @gmail.com
Description
After authenticating with gws auth login --scopes https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/script.projects, all Google Drive API calls work correctly but all Apps Script API calls fail with 401 "invalid authentication credentials."
Steps to Reproduce

Install gws via npm: npm install -g @googleworkspace/cli
Place custom client_secret.json in ~/.config/gws/
Enable both Google Drive API and Apps Script API in GCP project
Enable Apps Script API at https://script.google.com/home/usersettings
Authenticate:

bashgws auth login --scopes https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/script.projects

Export unmasked credentials (required due to separate Keychain issue — see below):

bashgws auth export --unmasked > ~/.config/gws/credentials.json

Verify auth status shows correct scopes:

bashgws auth status

Shows scope_count: 2, token_valid: true, both drive and script.projects scopes listed

Test Drive API — works:

bashgws drive files list --params '{"pageSize": 5, "fields": "files(id,name,mimeType)"}' --format table

Returns file listing successfully

Test Apps Script API — fails:

bashgws apps-script projects getContent --params '{"scriptId": "VALID_SCRIPT_ID"}'

Returns 401 error

Expected Behavior
gws apps-script projects getContent should return the script project content, since the credentials include the script.projects scope and the Apps Script API is enabled.
Actual Behavior
json{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"reason": "unknown"
}
}
Additional Context
Scope issue required revoking app access
Initially, after adding script.projects to an existing auth session, the API returned 403 "insufficient authentication scopes" even though gws auth status showed the scope was present. This was resolved by revoking app access at https://myaccount.google.com/permissions and re-authenticating from scratch. After revoking, the error changed from 403 to 401.
Separate issue: encrypted credentials not usable for API calls
On macOS, gws auth login saves encrypted credentials via OS Keyring, but API calls (e.g., gws drive files list) fail with 401 "No credentials provided" unless you first run gws auth export --unmasked > ~/.config/gws/credentials.json to create a plaintext credentials file. The gws auth status command can read the encrypted credentials fine, but the API call code path cannot. This workaround resolves Drive API calls but does not resolve the Apps Script 401.

Activity

  1. jpoehnelt commented on Mar 6, 2026

    @jpoehnelt
    Contributor

    are you still running into this?

  2. OBugroviy commented on Mar 6, 2026

    @OBugroviy

    Same issue.

    Update: Deleting all the stale credential and token cache files (credentials*.enc and token_cache*.json) from the ~/.config/gws/ directory and reauthorizing helps.

  3. balajibr commented on Aug 21, 2026

    @balajibr

    I hit the same symptom shape on a different API pair (Pub/Sub dead, Gmail fine, same credential) and tracked down a probable root cause — filed as #904 with the details.

    Short version: the token cache is keyed per scope, and one entry can get written with expires_at: None. gws then can never conclude that entry is stale, so it never refreshes it and re-sends the same access token forever. Once that token's grant ages out or is superseded, every call needing that one scope fails permanently while all other scopes keep working off their own valid entries. I decrypted the cache to confirm it — the failing scope's entry had a null expiry while every sibling entry had a real timestamp, and the cached token itself was well-formed, just long dead.

    That explains a few things in this thread:

    • Why deleting token_cache*.json helps (the workaround already reported above — it matches exactly what fixed mine). Worth adding that in my case deleting only token_cache.json was sufficient; credentials.enc did not need touching, and no re-authorisation was needed.
    • Why re-authenticating alone doesn't help. gws auth login does not invalidate the token cache, so a stale per-scope entry survives it. On my machine credentials.enc was rewritten at 08:22 while token_cache.json sat untouched from 08:05, and the error stayed byte-identical across two successful re-auths — because the credential is never consulted when a cached entry looks valid.
    • The 403 → 401 transition after revoking app access is consistent with this too: 403 is the genuine "insufficient scope" answer, whereas this bug's 401 means the server could not resolve the bearer token at all. Useful as a diagnostic split — a real scope problem gives you 403; a 401 with reason: ACCESS_TOKEN_TYPE_UNSUPPORTED (or unknown, as in the original report) points at a dead cached token instead.

    So for anyone still on this: try rm ~/.config/gws/token_cache.json by itself before revoking access or re-authing — if the 401 clears immediately, it's this bug rather than a scope or API-enablement problem.

    A non-destructive way to confirm it without deleting anything, using a copy of the config dir:

    mkdir -p /tmp/gwscfg && chmod 700 /tmp/gwscfg
    cp -a ~/.config/gws/client_secret.json ~/.config/gws/credentials.enc \
          ~/.config/gws/.encryption_key /tmp/gwscfg/     # note: no token_cache.json
    GOOGLE_WORKSPACE_CLI_CONFIG_DIR=/tmp/gwscfg gws <the failing command>

    If it succeeds there and fails against the real config dir, the cache is the culprit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions