disquo-api

Java Disqus Rest Api Library

License

License

Categories

Categories

Ant Build Tools Net
GroupId

GroupId

net.anthavio.disquo
ArtifactId

ArtifactId

disquo-api
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

disquo-api
Java Disqus Rest Api Library
Project URL

Project URL

https://github.com/anthavio/disquo
Project Organization

Project Organization

Anthavio
Source Code Management

Source Code Management

https://github.com/anthavio/disquo

Download disquo-api

How to add to project

<!-- https://jarcasting.com/artifacts/net.anthavio.disquo/disquo-api/ -->
<dependency>
    <groupId>net.anthavio.disquo</groupId>
    <artifactId>disquo-api</artifactId>
    <version>1.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/net.anthavio.disquo/disquo-api/
implementation 'net.anthavio.disquo:disquo-api:1.0.2'
// https://jarcasting.com/artifacts/net.anthavio.disquo/disquo-api/
implementation ("net.anthavio.disquo:disquo-api:1.0.2")
'net.anthavio.disquo:disquo-api:jar:1.0.2'
<dependency org="net.anthavio.disquo" name="disquo-api" rev="1.0.2">
  <artifact name="disquo-api" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.anthavio.disquo', module='disquo-api', version='1.0.2')
)
libraryDependencies += "net.anthavio.disquo" % "disquo-api" % "1.0.2"
[net.anthavio.disquo/disquo-api "1.0.2"]

Dependencies

compile (7)

Group / Artifact Type Version
commons-lang : commons-lang jar 2.6
commons-codec : commons-codec jar 1.3
org.slf4j : slf4j-api jar 1.7.7
org.slf4j : jcl-over-slf4j jar 1.7.7
net.anthavio : hatatitla jar 1.5.0
org.apache.httpcomponents : httpclient jar 4.2.5
com.fasterxml.jackson.core : jackson-databind jar 2.3.4

test (4)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.1.1
junit : junit jar 4.11
org.assertj : assertj-core jar 1.6.1
org.mockito : mockito-all jar 1.9.5

Project Modules

There are no modules declared in this project.

Disquo

Build Status Coverage Status Maven Central

Disqus Rest Api Java Client Library - see https://help.disqus.com/customer/portal/articles/1104798-using-the-api

Maven Repository & Coordinates

    <dependency>
        <groupId>net.anthavio.disquo</groupId>
        <artifactId>disquo-api</artifactId>
        <version>1.0.2</version>
    </dependency>

Fluent foolproof API

Fluent Buider pattern is used for complex request creation and execution

		/*
		To get Application keys
		  1. Visit http://disqus.com/api/applications/ and Log in to Disqus or Create an Account
		  2. Visit http://disqus.com/api/applications/ and Register new application
		  3. Use generated "Public Key" and "Secret Key" (Access Token is optional)
		*/
		DisqusApplicationKeys keys = new DisqusApplicationKeys("...api_key...", "...secret_key...");
		//Construct Disqus API client
		DisqusApi disqus = new DisqusApi(keys);
		//Use Fluent Builder to gather method parameters and call execute to get Response 
		DisqusResponse<List<DisqusPost>> response = disqus.posts().list(threadId, null);
		//Get list of Posts from Response
		List<DisqusPost> posts = response.getResponse();
		for (DisqusPost post : posts) {
			//Do whatever you want to...
			String text = post.getAuthor().getName() + " posted " + post.getMessage();
			System.out.println(text);
		}
		disqus.close();

Paging

When returning list, Disqus API returns 30 items. This size can be maxed to 100 items. To paginate through larger sets of items, you need to use cursor.

For details visit http://disqus.com/api/docs/cursors/

			
		DisqusApplicationKeys keys = new DisqusApplicationKeys("...api_key...", "...secret_key...");
		DisqusApi disqus = new DisqusApi(keys);
		String cursor = null;
		do {
			DisqusPage page = new DisqusPage(cursor);
			DisqusResponse<List<DisqusPost>> response = disqus.posts().list(threadId, page);
			List<DisqusPost> posts = response.getResponse();
			for (DisqusPost post : posts) {
				//process post...
			}
			cursor = response.getCursor().getNext();
		} while (cursor != null);
		disqus.close();

Related

		DisqusApplicationKeys keys = new DisqusApplicationKeys("...api_key...", "...secret_key...");
		DisqusApi disqus = new DisqusApi(keys);
		//Get basic thread details
		DisqusResponse<DisqusThread> slimResponse = disqus.threads().details(threadId);
		//Field forum is just a String with Forum name
		assertThat(slimResponse.getResponse().getForum()).isInstanceOf(String.class);

		//Use Related.forum to tell API to join Forum details into returned Thread object 
		DisqusResponse<DisqusThread> joinedResponse = disqus.threads().details(threadId, Related.forum);
		//Field forum is now Bean with many fields
		assertThat(joinedResponse.getResponse().getForum()).isInstanceOf(DisqusForum.class);
		disqus.close();

Authentication

While reading or listing API operations does not usually need end user authentication, writing calls such as posting must be authenticated.

For details visit http://disqus.com/api/docs/auth/

		//While reading or listing API operations does not usually need authentication, writing calls must be authenticated

		//Authenticating as the Account Owner

		//Application access_token can be used when creating Disqus API client optionaly
		DisqusApplicationKeys keys = new DisqusApplicationKeys("...api_key...", "...secret_key...", "...access_token...");
		DisqusApi disqus = new DisqusApi(keys);
		//Application identity (Account of the user who owns Application) is used to create post
		DisqusResponse<DisqusPost> response = disqus.posts().createBuilder(threadId, "Hello world " + new Date()).execute();

		String yourCallbackUrl = "http://www.changeme.com/disqus_will_redirect_user_here_after_login";

		//You can use OauthAuthenticator to construct correct calls 
		OAuth2 oauth = new OAuth2Builder().setAuthorizationUrl("/api/oauth/2.0/authorize/")
				.setTokenEndpoint(disqus.getSender(), "/api/oauth/2.0/access_token/").setClientId(keys.getApiKey())
				.setClientSecret(keys.getApiSecret()).setRedirectUri(yourCallbackUrl).build();

		//Server-Side OAuth

		String disqusOauthUrl = oauth.getAuthorizationUrl("read,write", "some-random-to-check");
		//Use HttpServletResponse to redirect user to Disqus Login page - servletResponse.sendRedirect(disqusOauthUrl);

		//If the user Logs in successfully on Disqus site, he will redirected back to the yourCallbackUrl with code http parameter
		String code = "Get me from the HttpServletRequest..."; //servletRequest.getParameter("code")
		//Call Disqus to convert "code" into "token" and possibly store returned TokenResponse in HttpSession 
		TokenResponse tokenResponse = oauth.access(code).get(TokenResponse.class);
		//Use access_token to authenticate calls as user 
		String userAccessToken = tokenResponse.getAccess_token();
		//User identity is used to create post
		disqus.posts().create(Identity.access(userAccessToken), threadId, "Hello world " + new Date(), null);

		//Single Sign-On Authentication

		// Available only for Pro subscription customers - https://help.disqus.com/customer/en/portal/articles/1104796-single-sign-on
		// If you do not have it, you'll get DisqusServerException: http: 400 code: 18 Invalid argument, 'remote_auth': SSO authentication is not configured for this application.
		SsoAuthData ssoauth = new SsoAuthData("custom-12345-id", "Firstname", "Surname");
		//SSO User identity is used to create post
		disqus.posts().create(ssoauth, keys.getApiSecret(), threadId, "Hello world " + new Date(), null);

		disqus.close();

Versions

Version
1.0.2
1.0.1
1.0.0