rubenlagus / TelegramApi

Java library to create Telegram Clients
MIT License
296 stars 108 forks source link

Phone code always expired #78

Open junjie870113 opened 6 years ago

junjie870113 commented 6 years ago

I'm trying to do some test against this lib, although I can get the SMS code, the call to authSingIn always says that the SMS code expired.

the telegram client is deployed in Chicago(VPS node) while the phone is in China. I try to change the timezone to 'Asia/Shanghai' but still failed

can anyone help me?

TelegramApi#1001:<< #1 error #400 PHONE_CODE_EXPIRED in 202 ms MTProto#1002#Scheduller:Forgetting message: #10 MTProto#1002:Response Iteration TelegramApi#1001:auth.signIn#bcd51581 java.util.concurrent.ExecutionException: org.telegram.api.engine.RpcException: PHONE_CODE_EXPIRED

private <T extends TLObject> T callNonAuth(TLMethod<T> method) {
    T t = null;
    try {
        t = telegramApi.doRpcCallNonAuth(method);
    } catch (RpcException e) {
        if (e.getErrorCode() == 303) {
            try {
                    int destDC = Integer.valueOf(e.getErrorTag().replaceAll("NETWORK_MIGRATE_", "").replaceAll("PHONE_MIGRATE_", "").replaceAll("USER_MIGRATE_", ""));
                    System.out.println("switch to DC:" + destDC);
                    telegramApi.switchToDc(destDC);
                    t = telegramApi.doRpcCallNonAuth(method);
            } catch (Exception e2) {
                System.err.println(e2.getMessage());
            }
        } else
                System.err.println(e.getMessage());
    } catch (TimeoutException e) {
        System.err.println(e.getMessage());
    }
    return t;
}

public void userAuthorization(String countryCode, String phoneNumber, int apiId, String apiHash) throws Exception {
    TLSentCode tlSentCode = this.callNonAuth(new TLRequestAuthSendCodeBuilder().phoneNumber(countryCode + phoneNumber).apiId(apiId).apiHash(apiHash).build());
    System.out.println("wait for sms...");
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Activation code:");
    String phoneCode = reader.readLine();
    System.out.println("phoneCode:" + phoneCode + " phoneCodeHash:" + tlSentCode.getPhoneCodeHash() + " type:" + tlSentCode.getType());
    TLAuthorization tlAuthorization = this.callNonAuth(new TLRequestAuthSignInBuilder().phoneNumber(phoneNumber).phoneCode(phoneCode)
                        .phoneCodeHash(tlSentCode.getPhoneCodeHash()).build());
    TLUser user = (TLUser) tlAuthorization.getUser();
    System.out.println("access hash:" + user.getAccessHash() + " userName:" + user.getUserName());
}