MaryKor
MaryKor

Reputation: 57

RTC Token with Agora keeps throwing Incorrect Token in Flutter

This is my cloud function for generating RTC tokens for Agora video calls in my flutter app. If I generate a temp token through the Agora console, it works without issues. However, for some reason, if I generate the token through the cloud function, it throws an error. AppID and AppCertificates are correct.

import pkg from 'agora-access-token';
const { RtcTokenBuilder, RtcRole } = pkg;

export default async ({ req, res, log, error }) => {
   if (req.method === 'POST') {
   let requestBody;
if (typeof req.body === 'string') {
  requestBody = JSON.parse(req.body);
} else {  
  requestBody = req.body; 
}
const { channelName, uid } = requestBody;
const appId = process.env.APP_ID;
const appCertificate = process.env.APP_CERTIFICATE;
const role = RtcRole.PUBLISHER;
log('Request body:', requestBody);
log('Channel Name:', channelName);
log('UID:', uid);
log('App ID:', appId);
log('App Certificate:', appCertificate);

const expirationTimeInSeconds = 3600;
const currentTimestamp = Math.floor(Date.now() / 1000);
const privilegeExpiredTs = Math.floor(currentTimestamp + expirationTimeInSeconds);

const token = RtcTokenBuilder.buildTokenWithUid(
  appId, 
  appCertificate, 
  channelName, 
  uid,
  role, 
  privilegeExpiredTs
);

log('Generated Token:', token);

return res.json({ token, uid: uid.toString() });
} else {
res.status(405).json({ message: 'Method not allowed' });
}
}

Upvotes: 0

Views: 49

Answers (0)

Related Questions