Skip to content

Commit

Permalink
Исправлена ошибка #231 + сопутствующие баги
Browse files Browse the repository at this point in the history
В метод авторизации через токен добавлена возможность указать срок
действия токена. Объявления таймера выделено в отдельный метод.
Исправлена ошибка приведения типов в методе
WallCategory.Obsolete.Get(x);
  • Loading branch information
CaCTuCaTu4ECKuu committed May 11, 2016
1 parent b7deb87 commit 061024d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions VkNet/Categories/Obsolete/WallCategory.Obsolete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public ReadOnlyCollection<Post> Get(long ownerId, out int totalCount, int? count
var result = Get(new WallGetParams
{
OwnerId = ownerId,
Count = (ulong)count,
Offset = (ulong)offset,
Count = (ulong)(count ?? 0),
Offset = (ulong)(offset ?? 0),
Filter = filter
});
totalCount = Convert.ToInt32(result.TotalCount);
Expand Down
30 changes: 20 additions & 10 deletions VkNet/VkApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,20 +342,22 @@ public Task AuthorizeAsync(ApiAuthParams @params)
/// </summary>
/// <param name="accessToken">Маркер доступа, полученный извне.</param>
/// <param name="userId">Идентификатор пользователя, установившего приложение (необязательный параметр).</param>
public void Authorize(string accessToken, long? userId = null)
/// <param name="expireTime">Время, в течении которого действует токен доступа (0 - бесконечно).</param>
public void Authorize(string accessToken, long? userId = null, int expireTime = 0)
{
if (string.IsNullOrWhiteSpace(accessToken))
{
return;
}

StopTimer();
StopTimer();

LastInvokeTime = DateTimeOffset.Now;
AccessToken = accessToken;
UserId = userId;
_ap = new ApiAuthParams();
}
LastInvokeTime = DateTimeOffset.Now;
SetTimer(expireTime);
AccessToken = accessToken;
UserId = userId;
_ap = new ApiAuthParams();
}

/// <summary>
/// Получает новый AccessToken используя логин, пароль, приложение и настройки указанные при последней авторизации.
Expand Down Expand Up @@ -419,15 +421,23 @@ private void Authorize(ulong appId, string emailOrPhone, string password, Settin
var authorization = Browser.Authorize(appId, emailOrPhone, password, settings, code, captchaSid, captchaKey, host, port);
if (!authorization.IsAuthorized)
{
throw new VkApiAuthorizationException("Invalid authorization", emailOrPhone, password);
throw new VkApiAuthorizationException("Invalid authorization with {0} - {1}", emailOrPhone, password);
}
var expireTime = (Convert.ToInt32(authorization.ExpiresIn) - 10) * 1000;
SetTimer(expireTime);
AccessToken = authorization.AccessToken;
UserId = authorization.UserId;
}
private void SetTimer(int expireTime)
{
if (expireTime > 0)
{
_expireTimer = new Timer(AlertExpires, null, expireTime, Timeout.Infinite);
}
AccessToken = authorization.AccessToken;
UserId = authorization.UserId;
else
{
_expireTimer = new Timer(AlertExpires, null, Timeout.Infinite, Timeout.Infinite);
}
}
/// <summary>
/// Прекращает работу таймера оповещения
Expand Down

1 comment on commit 061024d

@CaCTuCaTu4ECKuu
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если кто увидит этот комментарий, то тут еще нужно подправить, потому что я сделал запас (var expireTime = (Convert.ToInt32(authorization.ExpiresIn) - 10) * 1000;) так что значение может принять отрицательный оборот

Please sign in to comment.