zerobias / telegram-mtproto

Telegram client api (MTProto) library
MIT License
617 stars 136 forks source link

can't login with errors PHONE_NUMBER_UNOCCUPIED or AUTH_KEY_UNREGISTERED when dev config is different #206

Closed helxsz closed 5 years ago

helxsz commented 5 years ago

I am trying to get access to Telegram login with APPID, API HASH in the development test server mode.

const API_ID = xxxxxx;
const API_HASH = 'xxxxxxxxxxx';
let client = new Telegram(API_ID,API_HASH,true); // set true to make the test server

Once setting dev to be true as shown below

MTProto({
       server: {
        **dev: true,**
      },'xxx'})

I can get phone_code_hash and sms code, however whenever I input the sms code, I will receive the [42.055] Error 400 PHONE_NUMBER_UNOCCUPIED 2 2

{ code: 400,
  type: 'PHONE_NUMBER_UNOCCUPIED',
  description: 'CODE#400 PHONE_NUMBER_UNOCCUPIED',
  originalError: 
   { _: 'rpc_error',
     error_code: 400,
     error_message: 'PHONE_NUMBER_UNOCCUPIED' } }

if I set dev to be false

MTProto({
       server: {
        **dev: true,**
      },'xxx'})

I will receive an error like this

[15.362] Error 401 AUTH_KEY_EMPTY 2 5
[16.250] Error 401 AUTH_KEY_UNREGISTERED 2 5
const { MTProto } = require('telegram-mtproto');
const readline = require('readline');

const LAYER = 57;
const INIT_CONNECTION = 0x69796de9;

class Telegram {

  constructor (api_id=API_ID, api_hash=API_HASH, dev_server) {
    this._apiId = api_id;
    this._apiHash = api_hash;

    // eslint-disable-next-line new-cap
    this._client = MTProto({
      server: {
        dev: dev_server,
        //webogram: true
      },
      api: {
        layer: LAYER,
        initConnection: INIT_CONNECTION,
        api_id
      }
    });
  }

  async signIn (phone_number) {
    try {
          const { phone_code_hash } = await this._client('auth.sendCode', {
            phone_number,
            current_number: true,
            api_id: this._apiId,
            api_hash: this._apiHash
          });
          console.log('sendCode',phone_code_hash);

          const phone_code = await this.askForCode();
          console.log(`Your code: ${phone_code}`);

          const { user } = await this._client('auth.signIn', {
            phone_number   : phone_number,
            phone_code_hash: phone_code_hash,
            phone_code     : phone_code
          });

          console.log('signed as ', user);

          return phone_code_hash;
      } catch (error) {
        console.error(error)
      }

  }

  // This function will stop execution of the program until you enter the code
  // that is sent via SMS or Telegram.
   askForCode() {
    return new Promise((resolve) => {
      const rl = readline.createInterface({
        input : process.stdin,
        output: process.stdout
      });

      rl.question('Please enter passcode for :\n', (num) => {
        rl.close();
        resolve(num);
      });
    })
  }
}
slinker-hiwa commented 5 years ago

Hi guy.

dev DC and live is different, so when you config {dev: true}, all API will call to dev DC, your phone didn't register on this DC

So please catch response API auth.sendCode field phone_registered: false or undefined.

if phone_registered = false, use "auth.signUp" instead "auth.signIn" with same params. Have fun.

:D