Esempio n. 1
0
        /// <summary>
        /// 刷新令牌
        /// </summary>
        /// <returns></returns>
        public async Task <bool> RefreshTokenAsync()
        {
            try
            {
                var param = $"access_token={_accessToken}&refresh_token={_refreshToken}&appkey={BiliFactory.AndroidKey.Appkey}&ts={BiliFactory.GetNowSeconds()}";
                param += "&sign=" + BiliFactory.GetSign(param);
                var content = await BiliClient.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, BiliFactory.GetTimeStampFuture(data.expires_in));
                    InitToken(package);
                    TokenChanged?.Invoke(this, package);
                    await SSO();

                    return(true);
                }
                return(false);
            }
            catch (Exception)
            {
                return(false);
            }
        }
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={BiliFactory.AndroidKey.Appkey}&build={BiliFactory.BuildNumber}&mobi_app=android&password={Uri.EscapeDataString(await BiliFactory.EncryptAsPasswordAsync(password))}&platform=android&ts={BiliFactory.GetNowSeconds()}&username={Uri.EscapeDataString(userName)}";

            if (!string.IsNullOrEmpty(captcha))
            {
                param += "&captcha=" + captcha;
            }
            param += "&sign=" + BiliFactory.GetSign(param);
            string response = await BiliClient.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, BiliFactory.GetTimeStampFuture(data.token_info.expires_in));
                        InitToken(package);
                        TokenChanged?.Invoke(this, package);
                        result.Status = LoginResultType.Success;
                        await SSO();

                        await GetUserSpaceAsync(286725648);
                    }
                    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>
        /// <param name="accessToken">令牌</param>
        /// <param name="mid">用户ID</param>
        /// <returns></returns>
        public async Task <bool> SetLoginStatusAsync(string accessToken, string refreshToken = "", int expiry = 0)
        {
            try
            {
                string refe    = string.IsNullOrEmpty(refreshToken) ? accessToken : refreshToken;
                var    package = new TokenPackage(accessToken, refe, BiliFactory.GetTimeStampFuture(expiry == 0 ? 7200 : expiry));
                InitToken(package);
                TokenChanged?.Invoke(this, package);
                await SSO();

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Esempio n. 4
0
        static BiliClient()
        {
            _httpClient = new HttpClient();

            _httpClient.DefaultRequestHeaders.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            _httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19041");
            _httpClient.SendAsync(new HttpRequestMessage()
            {
                Method = new HttpMethod("HEAD"),
                RequestUri = new Uri("http://www.bilibili.com/")
            }).Result.EnsureSuccessStatusCode();

            var package = new TokenPackage("", "", 0);
            AccessToken = "";
            Account = new AccountService(package);

            Directory.CreateDirectory(FileCache = Environment.CurrentDirectory + "\\FileCache");
        }
Esempio n. 5
0
 private void InitToken(TokenPackage p)
 {
     BiliClient.AccessToken = _accessToken = p.AccessToken;
     _refreshToken          = p.RefreshToken;
     _expiry = Convert.ToInt32(p.Expiry);
 }
Esempio n. 6
0
 public AccountService(TokenPackage p)
 {
     InitToken(p);
 }