gotd / td

Telegram client, in Go. (MTProto API)
MIT License
1.48k stars 131 forks source link

SignIn fail #1454

Open vicabert091 opened 3 days ago

vicabert091 commented 3 days ago
func main() {
    sessionPath := "session_phone.json"
    storage := &session.FileStorage{Path: sessionPath}
    client := telegram.NewClient(appID, appHash, telegram.Options{SessionStorage: storage})

    err := client.Run(context.Background(), func(ctx context.Context) error {
        authCli := client.Auth()

        status, err := authCli.Status(ctx)
        if err != nil {
            log.Fatal(err)
        }

        if !status.Authorized {

            phoneNumber := "+"

            code, err := authCli.SendCode(ctx, phoneNumber, auth.SendCodeOptions{
                AllowAppHash: true,
            })
            if err != nil {
                return fmt.Errorf("failed to send code: %w", err)
            }
            sentCode := code.(*tg.AuthSentCode)

            var receivedCode string
            fmt.Print("Enter the code you received: ")
            fmt.Scanln(&receivedCode)

            _, err = authCli.SignIn(ctx, phoneNumber, sentCode.PhoneCodeHash, receivedCode)
            if err != nil {
                log.Fatal(err)
            }
            fmt.Println("Successfully logged in!")
        }

        return nil
    })

    if err != nil {
        log.Fatalf("Failed to run Telegram client: %v", err)
    }
}
image

The verification code can be received, and the parameters passed are correct. PHONE_CODE_EMPTY is still reported

prdsrm commented 1 day ago

Hi!

Its not a bug, the phone code is actually empty.

Look here:

phoneNumber := "+"
code, err := authCli.SendCode(ctx, phoneNumber, auth.SendCodeOptions{
    AllowAppHash: true,
})

You set the phoneNumber variable to "+", so yes, the phone code is missing, this is a bad request: https://core.telegram.org/api/errors#400-bad-request.

Please set your phone number to a real phone number, the one of your telegram account, for instance: phoneNumber := "+15028258670", and it will work, then you'll be able to close the issue.