yikart/AiToEarn · error · BadRequestException

token和mail是必须的

Error message

token和mail是必须的

What it means

A BadRequestException raised by the getAuthUrl controller handler when the request is missing required inputs: the authenticated user's id from the system token or the mail query parameter. The handler needs both to bind the TikTok OAuth state to a local user.

Source

Thrown at project/aitoearn-electron/server/src/modules/plat/tiktok/tiktok.controller.ts:33

// @UseGuards(AuthGuard)
export class TikTokController {
  constructor(
    private readonly tikTokService: TikTokService,
    private readonly tikTokAuthService: TikTokAuthService,
  ) {}

  /**
   * 获取TikTok授权URL
   */
  @Get('auth/url')
  @ApiOperation({ summary: '获取TikTok授权URL' })
  @ApiQuery({ name: 'mail', required: false, description: '用户邮箱' })
  async getAuthUrl(
    @GetToken() systemToken: TokenInfo,
    @Query('mail') mail: string,
  ) {
    if (!systemToken.id || !mail) {
      throw new BadRequestException('token和mail是必须的');
    }
    return this.tikTokAuthService.getAuthorizationUrl(systemToken.id, mail);
  }

  /**
   * TikTok OAuth2回调处理
   */

  @ApiOperation({ summary: 'TikTok OAuth2回调处理' })
  @Public()
  @ApiQuery({ name: 'code', type: String, description: 'OAuth2授权码' })
  @ApiQuery({ name: 'state', type: String, description: '状态码' })
  @Get('auth/callback')
  async handleAuthCallback(
    @Query('code') code: string,
    @Query('state') state: string,
    @Res() res: Response,
  ) {

View on GitHub (pinned to d3aa8bea5b)

Solutions

  1. Add the mail query parameter to the request URL.
  2. Ensure the request carries a valid system token (Authorization header with a current token) so systemToken.id resolves.
  3. Re-login to obtain a fresh system token if it expired.
  4. In the client, validate inputs before calling and show a form validation error instead of hitting the API.

Example fix

// before
const url = `/api/plat/tiktok/auth-url`;
// after — client side
if (!userEmail) throw new Error('mail is required');
const url = `/api/plat/tiktok/auth-url?mail=${encodeURIComponent(userEmail)}`;
// with headers: { Authorization: `Bearer ${systemToken}` }
Defensive patterns

Strategy: validation

Validate before calling

// client-side pre-check
if (!systemToken?.id) throw new Error('system token missing — log in first');
if (!mail || !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(mail)) throw new Error('mail query param required');

Type guard

function canRequestAuthUrl(p: { systemToken?: { id?: string }; mail?: string }): p is { systemToken: { id: string }; mail: string } {
  return typeof p.systemToken?.id === 'string' && typeof p.mail === 'string' && p.mail.includes('@');
}

Try / catch

try {
  return await api.getTikTokAuthUrl(mail);
} catch (e) {
  if ((e as any).status === 400 && /token和mail/.test((e as Error).message)) {
    await ensureLoggedIn();
    return api.getTikTokAuthUrl(mail);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling GET /tiktok/auth-url without the mail query parameter, or without a valid system token (GetToken decorator fails to resolve token.id) — e.g. an unauthenticated request or a missing/expired Authorization header.

Common situations: Frontend forgot to append ?mail=..., the JWT/Authorization header was dropped by a proxy, the system token expired so token.id is undefined, calling the endpoint without login.

Understand the failure class

Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.

Related errors


AI-assisted analysis of yikart/AiToEarn@d3aa8bea5b (2026-08-31). Data as JSON: /api/errors/c1df4f43179be0bf. Report an issue: GitHub.