Added ability to refresh token MAL
The func does not update the settings however
This commit is contained in:
parent
6d6c88a283
commit
d2d5196c28
|
@ -22,24 +22,35 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
|
|||
|
||||
private static string _Codechallenge = "";
|
||||
|
||||
private IImportListRepository _importListRepository;
|
||||
|
||||
public override string Name => "MyAnimeList";
|
||||
public override ImportListType ListType => ImportListType.Other;
|
||||
public override TimeSpan MinRefreshInterval => TimeSpan.FromSeconds(10); // change this later
|
||||
|
||||
// This constructor the first thing that is called when sonarr creates a button
|
||||
public MalImport(IHttpClient httpClient, IImportListStatusService importListStatusService, IConfigService configService, IParsingService parsingService, Logger logger)
|
||||
public MalImport(IImportListRepository netImportRepository, IHttpClient httpClient, IImportListStatusService importListStatusService, IConfigService configService, IParsingService parsingService, Logger logger)
|
||||
: base(httpClient, importListStatusService, configService, parsingService, logger)
|
||||
{
|
||||
if (MalTvdbIds.Count == 0)
|
||||
{
|
||||
MalTvdbIds = GetMalToTvdbIds();
|
||||
}
|
||||
|
||||
_importListRepository = netImportRepository;
|
||||
}
|
||||
|
||||
// This method should refresh (dunno what that means) the token
|
||||
// This method also fetches the anime from mal?
|
||||
public override IList<ImportListItemInfo> Fetch()
|
||||
{
|
||||
if (Settings.Expires < DateTime.UtcNow.AddMinutes(5))
|
||||
{
|
||||
_logger.Info($"current token: {Settings.AccessToken}");
|
||||
RefreshToken();
|
||||
_logger.Info($"new token: {Settings.AccessToken}");
|
||||
}
|
||||
|
||||
//_importListStatusService;
|
||||
return FetchItems(g => g.GetListItems());
|
||||
}
|
||||
|
@ -76,6 +87,7 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
|
|||
accessToken = jsonResult.AccessToken,
|
||||
refreshToken = jsonResult.RefreshToken,
|
||||
expires = DateTime.UtcNow.AddSeconds(int.Parse(jsonResult.ExpiresIn))
|
||||
//expires = DateTime.UtcNow.AddSeconds(5)
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -182,5 +194,34 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshToken()
|
||||
{
|
||||
var httpReq = new HttpRequestBuilder(OAuthTokenUrl)
|
||||
.Post()
|
||||
.AddFormParameter("client_id", Settings.ClientId)
|
||||
.AddFormParameter("client_secret", Settings.ClientSecret)
|
||||
.AddFormParameter("grant_type", "refresh_token")
|
||||
.AddFormParameter("refresh_token", Settings.RefreshToken)
|
||||
.Build();
|
||||
try
|
||||
{
|
||||
var httpResp = _httpClient.Post(httpReq);
|
||||
var jsonResp = Json.Deserialize<MalAuthToken>(httpResp.Content);
|
||||
Settings.AccessToken = jsonResp.AccessToken;
|
||||
Settings.RefreshToken = jsonResp.RefreshToken;
|
||||
Settings.Expires = DateTime.UtcNow.AddSeconds(int.Parse(jsonResp.ExpiresIn));
|
||||
//Settings.Expires = DateTime.UtcNow.AddSeconds(5);
|
||||
|
||||
if (Definition.Id > 0)
|
||||
{
|
||||
_importListRepository.UpdateSettings((ImportListDefinition)Definition);
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
_logger.Error("Error trying to refresh MAL access token.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,16 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
|
|||
{
|
||||
public MalSettingsValidator()
|
||||
{
|
||||
RuleFor(c => c.BaseUrl).ValidRootUrl();
|
||||
RuleFor(c => c.ClientId).NotEmpty()
|
||||
.OverridePropertyName("ClientId")
|
||||
.WithMessage("Must input Client ID from MAL, created in the API section of your account");
|
||||
RuleFor(c => c.ClientSecret).NotEmpty()
|
||||
.OverridePropertyName("ClientSecret")
|
||||
.WithMessage("Must input Client Secret from MAL, created in the API section of your account");
|
||||
RuleFor(c => c.AccessToken).NotEmpty()
|
||||
.OverridePropertyName("SignIn")
|
||||
.WithMessage("Must authenticate with MyAnimeList");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,13 +44,13 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
|
|||
[FieldDefinition(0, Label = "Client Secret", Type = FieldType.Textbox)]
|
||||
public string ClientSecret { get; set; }
|
||||
|
||||
[FieldDefinition(0, Label = "Access Token", Type = FieldType.Textbox, Hidden = HiddenType.Hidden)]
|
||||
[FieldDefinition(0, Label = "Access Token", Type = FieldType.Textbox, Hidden = HiddenType.Visible)]
|
||||
public string AccessToken { get; set; }
|
||||
|
||||
[FieldDefinition(0, Label = "Refresh Token", Type = FieldType.Textbox, Hidden = HiddenType.Hidden)]
|
||||
[FieldDefinition(0, Label = "Refresh Token", Type = FieldType.Textbox, Hidden = HiddenType.Visible)]
|
||||
public string RefreshToken { get; set; }
|
||||
|
||||
[FieldDefinition(0, Label = "Expires", Type = FieldType.Textbox, Hidden = HiddenType.Hidden)]
|
||||
[FieldDefinition(0, Label = "Expires", Type = FieldType.Textbox, Hidden = HiddenType.Visible)]
|
||||
public DateTime Expires { get; set; }
|
||||
|
||||
[FieldDefinition(99, Label = "Authenticate With MyAnimeList", Type = FieldType.OAuth, HelpTextWarning = "Client ID and Client Secret must be filled out before authenticating with MAL")]
|
||||
|
|
Loading…
Reference in New Issue