td
td copied to clipboard
SignIn fail
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)
}
}
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.
_, err = authCli.SignIn(ctx, phoneNumber, sentCode.PhoneCodeHash, receivedCode)
You need to swap two last arguments. First code, than codeHash