Mr1Penguin / DeviantartApi

DeviantartApi Library in C# (netstandard 2)
MIT License
6 stars 3 forks source link
api deviantart nuget rest uwp winrt

DeviantartApi Library in C# (netstandard 2)

Library for using Deviantart API from .NET.

Acquiring

nuget (outdated)

NuGet

git

prefered for now

Usage

If you use platform with implemented login process:


void RefreshTokenUpdated(string newRefreshToken)
{
    if(SavedToken != newRefreshToken)
        SaveToken(newRefreshToken, DateTime.Now.AddMonths(3));
}
...
// There is no valid RefreshToken
var result = await DeviantartApiLogin.Platform.Login.SignInAsync(ClientId, Secret, CallbackUrl, RefreshTokenUpdated, 
                                                   new[]
                                                   {
                                                        DeviantartApi.Login.Scope.Browse,
                                                        DeviantartApi.Login.Scope.User,
                                                        DeviantartApi.Login.Scope.Feed
                                                   }));
if(result.IsLoginError) 
{
    ShowError(result.LoginErrorText);
    return;
}

SaveToken(result.RefreshToken, DateTime.Now.AddMonths(3));
return;
...
// You have valid RefreshToken
DeviantartApiLogin.Platform.Login.AttachLogin(); //So TokenChecker know what must be called if refreshtoken invalid
var result = await DeviantartApi.Login.SetAccessTokenByRefreshAsync(ClientId, Secret, CallbackUrl, RefreshToken, RefreshTokenUpdated, new[]
                                                   {
                                                        DeviantartApi.Login.Scope.Browse,
                                                        DeviantartApi.Login.Scope.User,
                                                        DeviantartApi.Login.Scope.Feed
                                                   });
if(result.IsLoginError) 
{
    ShowError(result.LoginErrorText);
    return;
}
SaveToken(result.RefreshToken, DateTime.Now.AddMonths(3));
return;

Otherwise:


void RefreshTokenUpdated(string newRefreshToken)
{
    if(SavedToken != newRefreshToken)
        SaveToken(newRefreshToken, DateTime.Now.AddMonths(3));
}
...
// There is no valid RefreshToken
DeviantartApi.Login.CustomSignInAsync = MySignInAsync;
var result = await DeviantartApi.Login.SignInAsync(ClientId, Secret, CallbackUrl, RefreshTokenUpdated, 
                                                   new[]
                                                   {
                                                        DeviantartApi.Login.Scope.Browse,
                                                        DeviantartApi.Login.Scope.User,
                                                        DeviantartApi.Login.Scope.Feed
                                                   }));
if(result.IsLoginError) 
{
    ShowError(result.LoginErrorText);
    return;
}

SaveToken(result.RefreshToken, DateTime.Now.AddMonths(3));
return;
...
// You have valid RefreshToken
DeviantartApi.Login.CustomSignInAsync = MySignInAsync; //So TokenChecker know what must be called if refreshtoken invalid
var result = await DeviantartApi.Login.SetAccessTokenByRefreshAsync(ClientId, Secret, CallbackUrl, RefreshToken, RefreshTokenUpdated, new[]
                                                   {
                                                        DeviantartApi.Login.Scope.Browse,
                                                        DeviantartApi.Login.Scope.User,
                                                        DeviantartApi.Login.Scope.Feed
                                                   });
if(result.IsLoginError) 
{
    ShowError(result.LoginErrorText);
    return;
}
SaveToken(result.RefreshToken, DateTime.Now.AddMonths(3));
return;

Requests example


var result = await new DeviantartApi.Requests.User.WhoAmIRequest().ExecuteAsync();
if (result.IsError)
{
    ShowError(result.ErrorText);
    return;
}
UserName = result.Object.Username;
UserAvatarUri = new Uri(result.Object.UserIcon);

//or 
var Feed = new HomeRequest();
Feed.LoadMature = true;
var result = await Feed.GetNextPageAsync();
if(result.IsError)
{
    ShowError(result.ErrorText);
    return;
}
//use data
...
//get next page

result = await Feed.GetNextPageAsync();

//start from beginning
Feed.Cursor = "";

result = await Feed.GetNextPageAsync();