Esempio n. 1
0
        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns></returns>
        public async Task <BitmapImage> GetCaptchaAsync()
        {
            var stream = await BiliTool.GetStreamFromWebAsync($"{Api.PASSPORT_CAPTCHA}?ts=${AppTool.GetNowSeconds()}");

            if (stream != null)
            {
                var bitmap = new BitmapImage();
                await bitmap.SetSourceAsync(stream.AsRandomAccessStream());

                return(bitmap);
            }
            return(new BitmapImage(new Uri("ms-appx:///Assets/captcha_refresh.png")));
        }
Esempio n. 2
0
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="captcha">验证码</param>
        /// <returns></returns>
        public async Task <LoginCallback> LoginAsync(string userName, string password, string captcha = "")
        {
            string param = $"appkey={BiliTool.AndroidKey.Appkey}&build={BiliTool.BuildNumber}&mobi_app=android&password={Uri.EscapeDataString(await EncryptedPasswordAsync(password))}&platform=android&ts={AppTool.GetNowSeconds()}&username={Uri.EscapeDataString(userName)}";

            if (!string.IsNullOrEmpty(captcha))
            {
                param += "&captcha=" + captcha;
            }
            param += "&sign=" + BiliTool.GetSign(param);
            string response = await BiliTool.PostContentToWebAsync(Api.PASSPORT_LOGIN, param);

            var result = new LoginCallback();

            result.Status = LoginResultType.Error;
            if (!string.IsNullOrEmpty(response))
            {
                try
                {
                    var jobj = JObject.Parse(response);
                    int code = Convert.ToInt32(jobj["code"]);
                    if (code == 0)
                    {
                        var data    = JsonConvert.DeserializeObject <LoginResult>(jobj["data"].ToString());
                        var package = new TokenPackage(data.token_info.access_token, data.token_info.refresh_token, AppTool.DateToTimeStamp(DateTime.Now.AddSeconds(data.token_info.expires_in)));
                        InitToken(package);
                        TokenChanged?.Invoke(this, package);
                        result.Status = LoginResultType.Success;
                        await SSO();
                    }
                    else if (code == -2100)
                    {
                        result.Status = LoginResultType.NeedValidate;
                        result.Url    = jobj["url"].ToString();
                    }
                    else if (code == -105)
                    {
                        result.Status = LoginResultType.NeedCaptcha;
                    }
                    else if (code == -449)
                    {
                        result.Status = LoginResultType.Busy;
                    }
                    else
                    {
                        result.Status = LoginResultType.Fail;
                    }
                }
                catch (Exception)
                {
                    return(new LoginCallback {
                        Status = LoginResultType.Fail, Url = ""
                    });
                }
            }
            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// 刷新令牌
        /// </summary>
        /// <returns></returns>
        public async Task <bool> RefreshTokenAsync()
        {
            try
            {
                var param = $"access_token={_accessToken}&refresh_token={_refreshToken}&appkey={BiliTool.AndroidKey.Appkey}&ts={AppTool.GetNowSeconds()}";
                param += "&sign=" + BiliTool.GetSign(param);
                var content = await BiliTool.PostContentToWebAsync(Api.PASSPORT_REFRESH_TOKEN, param);

                var obj = JObject.Parse(content);
                if (Convert.ToInt32(obj["code"]) == 0)
                {
                    var data    = JsonConvert.DeserializeObject <TokenInfo>(obj["data"].ToString());
                    var package = new TokenPackage(data.access_token, data.refresh_token, AppTool.DateToTimeStamp(DateTime.Now.AddSeconds(data.expires_in)));
                    InitToken(package);
                    TokenChanged?.Invoke(this, package);
                    await SSO();

                    return(true);
                }
                return(false);
            }
            catch (Exception)
            {
                return(false);
            }
        }