This is a easy-to-use and fluent Java API client for the pastebin.com API.
- Java 8+
- Maven or Gradle
You can use either Jitpack or GitHub Packages for including this in your project. Add either of the following to your build.gradle
file:
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.caseyscarborough:pastebin-api-java:0.1.3'
}
repositories {
maven { url 'https://maven.pkg.github.com/caseyscarborough/pastebin-api-java' }
}
dependencies {
implementation 'com.pastebin.api:pastebin-api:0.1.3'
}
See the Example.java for a complete API example.
Create a new client using the PastebinClient
builder:
final PastebinClient client = PastebinClient
.builder()
.developerKey("my-developer-key")
// optional
.userKey("my-user-key")
.build();
Retrieve a user key if you don't already have one, and you would like to create pastes that are associated with your account. This will automatically assign the user key to the client for reuse in later calls.
final String userKey = client.login("username", "password");
System.out.println(userKey);
You can create a new paste here. All fields are optional except for content
.
final PasteRequest request = PasteRequest
.content("print(\"Hello, world!\")")
// the remaining fields are optional
.visibility(Visibility.PRIVATE)
.format(Format.PYTHON)
.name("Hello World in Python")
.expiration(Expiration.ONE_HOUR)
// the folder key can be found in the URL of your folder
.folderKey("N2a8jwyY")
.build();
// returns the URL of the newly created paste.
final String url = client.paste(request);
System.out.println(url);
There are many different formats and expirations to choose from.
Retrieve a list of your pastes. This takes a single parameter, the amount of pastes to return. It defaults to 50 with a min of 1 and a max of 100.
// Get a list of pastes on your account.
final List<Paste> pastes = client.list(5);
for (Paste paste : pastes) {
System.out.println("Title: " + paste.getTitle());
System.out.println("Key: " + paste.getKey());
System.out.println("URL: " + paste.getUrl());
System.out.println("Date: " + paste.getDate());
System.out.println("Expiration: " + paste.getExpiration());
System.out.println("Format: " + paste.getFormat().getName());
System.out.println("Size: " + paste.getSize());
System.out.println("Hits: " + paste.getHits());
System.out.println("Visibility: " + paste.getVisibility().getName());
System.out.println();
}
Retrieve your accounts information:
client.user();
Delete a paste on your account:
client.delete("qHrK7Tq7");
This is used to retrieve a paste from your account. Can be any public, private, or unlisted paste.
client.getUserPaste("HWAfaWmg");
This is for any public or unlisted paste (does not need to be your own account).
client.getPaste("BPaf5niB");
If an error is returned from the API, the client will throw a PastebinException
.
try {
final String raw = client.getPaste("af30fj21");
} catch (PastebinException e) {
logger.error("An error occurred retrieving paste", e);
}