Imgur.NET 0.1.0

dotnet add package Imgur.NET --version 0.1.0                
NuGet\Install-Package Imgur.NET -Version 0.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Imgur.NET" Version="0.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Imgur.NET --version 0.1.0                
#r "nuget: Imgur.NET, 0.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Imgur.NET as a Cake Addin
#addin nuget:?package=Imgur.NET&version=0.1.0

// Install Imgur.NET as a Cake Tool
#tool nuget:?package=Imgur.NET&version=0.1.0                

Imgur.NET

Imgur API client for .NET and Unity.

NuGet Releases GitHub license

English | 日本語

Overview

Imgur.NET is a client library for the Imgur API designed for .NET.

[!NOTE] Currently, Imgur.NET supports APIs for OAuth2.0 authentication, as well as Image, Album, and Comment functionalities. Features like Account and Gallery are not yet available and are planned to be implemented by v1.0.

Installation

Imgur.NET is distributed via NuGet and supports .NET Standard 2.1, .NET 6.0, and .NET 8.0.

.NET CLI

dotnet add package Imgur.NET

Package Manager

Install-Package Imgur.NET

Additionally, Imgur.NET can be used with Unity. See the Unity section for details.

Basic Usage

You can call the Imgur API using the ImgurClient class.

using System.IO;
using Imgur;

using var client = new ImgurClient
{
    ClientId = "YOUR_CLIENT_ID",
    ClientSecret = "YOUR_CLIENT_SECRET"
};

// Read the image file
var image = await File.ReadAllBytesAsync("image.png");

// Upload the image
var response = await client.Images.UploadAsync(new()
{
    ImageData = image,
    ImageContentType = ImageContentType.Raw,
});

// Get the image URL
Console.WriteLine(response.Link);

Authentication

Some APIs require OAuth2.0 authentication. You can get the authentication URL using GetAuthorizationUrl().

using var client = new ImgurClient
{
    ClientId = "YOUR_CLIENT_ID",
    ClientSecret = "YOUR_CLIENT_SECRET"
};

// Get the authorization URL
var url = client.Authorization.GetAuthorizationUrl();

After authentication, set the obtained Access Token in the ImgurClient.

client.AccessToken = "YOUR_ACCESS_TOKEN";

To reissue an Access Token using a Refresh Token, use GenerateAccessTokenAsync().

var response = await client.Authorization.GenerateAccessTokenAsync(new()
{
    RefreshToken = "YOUR_REFRESH_TOKEN"
});

var newAccessToken = response.AccessToken;
var newRefreshToken = response.RefreshToken;

Image

Operations like retrieving and uploading images are done through ImgurClient.Image.

// Retrieve an image
var image1 = await client.Image.GetAsync(new()
{
    ImageHash = "imageHash"
});

var bytes = await File.ReadAllBytesAsync("image.png");

// Upload an image
var uploadedImage = await client.Image.UploadAsync(new()
{
    ImageData = bytes,
    ImageContentType = ImageContentType.Raw,
});

// Delete an image
await client.Image.DeleteAsync(new()
{
    ImageHash = "imageHash"
});

Album

Album operations are done through ImgurClient.Album.

// Retrieve an album
var album = await client.Album.GetAsync(new()
{
    AlbumHash = "albumHash"
});

// Create a new album
var newAlbum = await client.Album.CreateAsync(new()
{
    Ids = ["image1", "image2"],
    Title = "New Album",
    Description = "Album description",
})

// Delete an album
await client.Album.DeleteAsync(new()
{
    AlbumHash = "albumHash"
});

Comment

Operations like retrieving and posting comments are done through ImgurClient.Comment.

// Retrieve a comment
var comment = await client.Comment.GetAsync(new()
{
    CommentId = 123456789,
});

// Post a comment
var newComment = await client.Comment.CreateAsync(new()
{
    ImageId = "image1",
    Comment = "Comment text"
});

// Delete a comment
client.Comment.DeleteAsync(new()
{
    CommentId = 123456789
});

Exception Handling

If an API call fails, an ImgurException will be thrown.

try
{
    var response = await client.Images.GetAsync(new()
    {
        ImageHash = "...",
    });
}
catch (ImgurException ex)
{
    Console.WriteLine((int)ex.Status);
    Console.WriteLine(ex.Message);
}

Customizing HttpClient

ImgurClient uses the standard HttpClient for communication. If you want to adjust the behavior of HttpClient, you can pass an HttpClientHandler during its creation.

public class ImgurClient : IDisposable
{
    readonly HttpClient httpClient;
    public HttpClient HttpClient => httpClient;

    public ImgurClient(HttpMessageHandler handler, bool disposeHandler)
    {
        httpClient = new(handler, disposeHandler);
    }

    public ImgurClient()
        : this(new HttpClientHandler(), true)
    {
    }

    public ImgurClient(HttpMessageHandler handler)
        : this(handler, true)
    {
    }

    public void Dispose()
    {
        httpClient.Dispose();
    }
}

Unity

Imgur.NET can be used with Unity. To install Imgur.NET in Unity, use NugetForUnity.

  1. Install NugetForUnity
  2. Open the Nuget > Manage NuGet Packages window, search for Imgur.NET, and install it

When using it in environments like WebGL, you need to replace the communication layer with UnityWebRequest. Here is an example using UnityWebRequestHttpMessageHandler.cs.

// Change HttpClientHandler to UnityWebRequestHttpMessageHandler
var client = new ImgurClient(new UnityWebRequestHttpMessageHandler())
{
    ClientId = "YOUR_CLIENT_ID",
    ClientSecret = "YOUR_CLIENT_SECRET",
    AccessToken = "YOUR_ACCESS_TOKEN",
    ConfigureAwait = true // Set ConfigureAwait to true for safe operation in WebGL
};

License

This library is under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.0 111 6/27/2024