Skip to content

Fix 'Anonymous token is invalid' error with automatic token refresh#30

Open
konard wants to merge 3 commits into
mainfrom
issue-16-f8294119
Open

Fix 'Anonymous token is invalid' error with automatic token refresh#30
konard wants to merge 3 commits into
mainfrom
issue-16-f8294119

Conversation

@konard

@konard konard commented Oct 17, 2025

Copy link
Copy Markdown
Owner

Summary

This PR fixes issue #16 by adding automatic token refresh when the VK API returns an "Anonymous token is invalid" error (error code 1116).

Problem

The acceptAllFriendRequests function was failing with the error:

{
  "error": {
    "error_code": 1116,
    "error_msg": "Anonymous token is invalid",
    "request_params": [
      {
        "key": "method",
        "value": "execute.acceptAllFriendRequests"
      }
    ]
  }
}

Root Cause Analysis

After extensive research, I found that:

  1. Error Code 1116: VK API error 1116 means "Anonymous token is invalid" - the token is being treated as anonymous and rejected
  2. Token Type Mismatch: The OAuth token obtained through Implicit Flow (response_type=token) may sometimes be categorized incorrectly by VK's servers as an "anonymous" token
  3. Execute Method Requirements: The execute method with custom stored functions requires proper user authentication tokens
  4. Similar Issues: This is one of several token-related authentication errors in the VK API:
    • Error 1114: "Anonymous token has expired"
    • Error 1116: "Anonymous token is invalid" (this issue)
    • Error 1118: "Anonymous token ip mismatch"

Solution

The fix follows the same pattern as PR #31 for handling token errors:

  • Added ANONYMOUS_TOKEN_INVALID_ERROR_MESSAGE constant to detect the specific error message
  • Updated the error handling logic in execute.sh to trigger automatic token refresh when this error occurs
  • When detected, the script automatically acquires a new access token using Selenium automation with user credentials

Changes

  • Updated execute.sh to detect and handle the "Anonymous token is invalid" error message
  • Added the error to the existing token refresh mechanism that handles expired tokens, IP address changes, and missing tokens

Test Plan

The fix handles the error gracefully by:

  1. Detecting the "Anonymous token is invalid" error message in the API response
  2. Automatically running the Selenium-based token acquisition flow
  3. Retrying the operation with the new token

Related Issues

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: undefined
@konard konard self-assigned this Oct 17, 2025
When VK API returns error 1116 ("Anonymous token is invalid"), the script
now automatically acquires a new access token using the same mechanism
that handles other token-related errors (expired, IP mismatch, missing).

This fix handles the case when VK API treats a user token as anonymous
and rejects it, which can occur when:
- The token format is not recognized properly by VK servers
- There's a mismatch in token validation on VK's side
- The token authentication state has changed

The solution follows the same pattern as PR #31 for handling invalid
access_token errors by detecting the specific error message and
triggering automatic token refresh.

Fixes #16

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Anonymous token is invalid error Fix 'Anonymous token is invalid' error with automatic token refresh Oct 17, 2025
@konard konard marked this pull request as ready for review October 17, 2025 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anonymous token is invalid error

1 participant