gramjs icon indicating copy to clipboard operation
gramjs copied to clipboard

signin response PHONE_CODE_INVALID

Open Dn9x opened this issue 2 years ago • 11 comments

我参考了这个issues: https://github.com/gram-js/gramjs/issues/70 完整代码如下:

const { Api, TelegramClient } = require("telegram");
const { StringSession } = require("telegram/sessions");
const { Logger } = require("telegram/extensions");


const apiId = 22;
const apiHash = "";

const phoneNumber = "9996627328";
const phoneCode = "22222";

const createClient = async (stringSession) => {
    const session = new StringSession(stringSession);
    const options = { connectionRetries: 5, baseLogger: new Logger("debug") };
    const client = new TelegramClient(session, apiId, apiHash, options);
    client.session.setDC(2, "149.154.167.50", 443);  // 这里是我的apikey里面的ip和端口
    await client.connect();
    return client;
};

const getLoginCodeCommand = (phoneNumber) => {
    const settings = new Api.CodeSettings();
    const args = { phoneNumber, apiId, apiHash, settings };
    return new Api.auth.SendCode(args);
};

(async () => {
    const client = await createClient("");
    const response = await client.invoke(getLoginCodeCommand(phoneNumber));
    console.log('response:', response);
    const { phoneCodeHash } = response;
    const args = { phoneNumber, phoneCode, phoneCodeHash };
    console.log('args', args);
    const result = await client.invoke(new Api.auth.SignIn(args));
    console.log("RESULT", result);
})();

一直提示号码无效:

/Users/xizao/work/fm/project/telegram/node_modules/telegram/errors/index.js:28
    return new RPCBaseErrors_1.RPCError(rpcError.errorMessage, request, rpcError.errorCode);
           ^

RPCError: 400: PHONE_CODE_INVALID (caused by auth.SignIn)
    at RPCMessageToError (/Users/xizao/work/fm/project/telegram/node_modules/telegram/errors/index.js:28:12)
    at MTProtoSender._handleRPCResult (/Users/xizao/work/fm/project/telegram/node_modules/telegram/network/MTProtoSender.js:517:58)
    at MTProtoSender._processMessage (/Users/xizao/work/fm/project/telegram/node_modules/telegram/network/MTProtoSender.js:442:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async MTProtoSender._recvLoop (/Users/xizao/work/fm/project/telegram/node_modules/telegram/network/MTProtoSender.js:418:17) {
  code: 400,
  errorMessage: 'PHONE_CODE_INVALID'
}

Dn9x avatar Feb 03 '23 08:02 Dn9x

would be great if you wrote the issue in english. You seem to be using the wrong IP. client.session.setDC(2, "149.154.167.40", 443); use that

painor avatar Feb 03 '23 14:02 painor

I have used this for testing, same error。

client.session.setDC(2, "149.154.167.40", 443); 

I have searched all kinds of ways on the search and have tested, but still the same error.

Is this related to the ip? I also use the ip of several countries and regions and still get the same error.

Dn9x avatar Feb 06 '23 02:02 Dn9x

I don't think so. I just copy pasted your code and changed the ip and it worked fine for me.

painor avatar Feb 06 '23 08:02 painor

Can you tell me where your ip belongs to? I tested the Hong Kong ip, Singapore, the United States, and Japan have not been able to, I am not sure why?

Dn9x avatar Feb 06 '23 08:02 Dn9x

my IP has nothing to do with this. this is the code i used:

const phoneNumber = "9996623848";
const phoneCode = "22222";

const createClient = async (stringSession: string) => {
    const session = new StringSession(stringSession);
    const options = { connectionRetries: 5, baseLogger: new Logger("debug") };
    const client = new TelegramClient(session, apiId, apiHash, options);
    client.session.setDC(2, "149.154.167.40", 443);  // 这里是我的apikey里面的ip和端口
    await client.connect();
    return client;
};

const getLoginCodeCommand = (phoneNumber: string) => {
    const settings = new Api.CodeSettings();
    const args = { phoneNumber, apiId, apiHash, settings };
    return new Api.auth.SendCode(args);
};

(async () => {
    const client = await createClient("");
    const response = await client.invoke(getLoginCodeCommand(phoneNumber));
    console.log('response:', response);
    const { phoneCodeHash } = response;
    const args = { phoneNumber, phoneCode, phoneCodeHash };
    console.log('args', args);
    const result = await client.invoke(new Api.auth.SignIn(args));
    console.log("RESULT", result);
})();


Make sure there is no cash being used and check the logs to see if the correct telegram IP is being used .

painor avatar Feb 06 '23 08:02 painor

setdc ip from this: image

Is it possible that the telegram server has been unsuccessful in identifying my device? I'll try another device.

Dn9x avatar Feb 06 '23 08:02 Dn9x

no it has nothing to do with your device. Have you tried the code I sent and checked the logs ?

painor avatar Feb 06 '23 08:02 painor

yes, sign in is sucess. but signup fail:

const phoneNumber = "99966273168";
const phoneCode = "22222";

const createClient = async (stringSession) => {
    const session = new StringSession(stringSession);
    const options = { connectionRetries: 5, baseLogger: new Logger("debug") };
    const client = new TelegramClient(session, apiId, apiHash, options);
    client.session.setDC(2, "149.154.167.40", 443);  // 这里是我的apikey里面的ip和端口
    await client.connect();
    return client;
};

const getLoginCodeCommand = (phoneNumber) => {
    const settings = new Api.CodeSettings();
    const args = { phoneNumber, apiId, apiHash, settings };
    return new Api.auth.SendCode(args);
};

(async () => {
    const client = await createClient("");
    const response = await client.invoke(getLoginCodeCommand(phoneNumber));
    console.log('response:', response);
    const { phoneCodeHash } = response;
    const args = { phoneNumber, phoneCodeHash, firstName: "liu", lastName: "some" };
    console.log('args', args);
    // const result = await client.invoke(new Api.auth.SignIn(args));
    const result = await client.invoke(
        new Api.auth.SignUp(args)
    );
    console.log("RESULT", result);
})();

result : image

Dn9x avatar Feb 06 '23 08:02 Dn9x

I don't think that's not how sign up works. you have first to try to sign in. If telegram returns Api.AuthorizationSignUpRequired then you can use sign up.

painor avatar Feb 06 '23 08:02 painor

I see, thank you, I will test it.

Dn9x avatar Feb 06 '23 08:02 Dn9x

@Dn9x have you succeed?

SkuratovichA avatar Apr 12 '24 03:04 SkuratovichA