Telegram - Getting AUTH_KEY_UNREGISTERED and API_ID_INVALID errors

Viewed 2259

I'm using the Node.js GramJS Telegram npm package to query the Telegram API. I also tested this with the @mtproto/core and I'm getting the same AUTH_KEY_UNREGISTERED and API_ID_INVALID RPCErrors with both NPM packages.

I copied both the api_id and api_hash from the site and into the apiId and apiHash variables.

I don't know if someone could test this code to see if it works as is, and perhaps it's an issue with Telegram and the API tokens that I received?

import { Api, TelegramClient } from "telegram";
import { StringSession } from "telegram/sessions/index.js";

const apiId = 1234567;
const apiHash = "this has the api_hash from the telegram website in my code";

const session = new StringSession("");
const client = new TelegramClient(session, apiId, apiHash);

(async function run() {
  await client.connect();

  console.log("You should now be connected.");
  console.log(client.session.save());

  const result = await client.invoke(
    new Api.channels.GetParticipant({
      channel: new Api.InputChannel({ channelId: -1001536877977 }),
      participant: new Api.ChannelParticipant({ userId: 1425866959 }),
    }),
  );

  console.log(result); // prints the result
})();

1 Answers

Okay so I solved my issue. I chose to use the Logging in as a User option where you enter your Telegram cellphone number to authenticate.

This works so I'll probably add some session storage with Redis so that I don't have to retype my number to reauthenticate each time I start the app.

Just wanted to post an answer if anyone else comes across this :)

(async function run() {
  // authenticate as a user
  await client.start({
    phoneNumber: async () => await input.text("number ?"),
    password: async () => await input.text("password?"),
    phoneCode: async () => await input.text("Code ?"),
    onError: (err) => console.log(err),
  });

  await client.connect();

  console.log("You should now be connected.");
  console.log(client.session.save());

  const result = await client.invoke(
    new Api.channels.GetParticipant({
      channel: -1001536877977,
      participant: "ajroos",
    }),
  );
  
  console.log(result); // prints the result
})();

Related