In this tutorial, we will be building a service that integrates with GitHub. We’ll start right from generating the basic project template, and finish off with a Service that loads, saves, and hits actions on the GitHub API.

Creating the Service base

Follow the instruction on the Building a Service page to get the base structure of your Service set up.

Building the Describe response

Every Service needs to return a Describe response from its /metadata endpoint. This response tells ManyWho what the Service supports (i.e. Database, Identity, Logic, Files and Social). The Java SDK provides a simple Builder class so it’s super easy to create a Describe response.

Open up DescribeController from inside the controllers package

Our GitHub Service will provide the following:

Database (for loading a list of user repositories)

Identity (for logging into a flow using GitHub)

Logic (for forking a repository)

Add a call to setProvidesIdentity(true) inside the describe() method, so it looks like this:

Create a GitHub application

We’ll need an application on GitHub so that we can authorize users through OAuth2 and make calls on their behalf. You can create a new application on GitHub for free here, and make sure the Callback URL is https://flow.manywho.com/api/run/1/oauth2. Make a note of the generated Client ID and Client Secret, as we’ll need them later.

Adding Authentication (Identity)

As the GitHub API is built around authorizing calls using OAuth2, we’ll integrate an identity provider into our Service that ManyWho can use to log into a Flow, which means that we can make calls on behalf of the logged in user without having to store tokens anywhere, or ask them for their password.

Creating the OAuth2 Provider

Each service that integrates with OAuth2 for authorization needs to have a Provider class, that gives the SDK’s OAuth2 library the correct secrets and where to request tokens.

Create a new package inside com.manywho.services.github called oauth2.

Inside this new package, create a class called GitHubProvider.

Make the new class extend AbstractOauth2Provider, which is a base class in the SDK for creating a provider in services that implement OAuth2.

Press Ctrl+I then Enter to implement the required methods from the interface.

Fill in the details based on this example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

publicclassGitHubProviderextendsAbstractOauth2Provider{

@Override

publicStringgetName(){

return"GitHub";

}

@Override

publicStringgetClientId(){

return"";// This value should be your Client ID from your application (https://github.com/settings/applications/new)

}

@Override

publicStringgetClientSecret(){

return"";// This value should be your Client Secret from your application (https://github.com/settings/applications/new)

Basically, in the code we just pasted, the authenticateWithGitHub() method will be called from inside the AuthController (which we’ll create in a minute), it calls out to the GitHub API to fetch the logged in user (using the GitHub Java library) and it returns an AuthenticatedWhoResult object that contains all of the logged-in user’s information ready to be used inside a flow.

Finally, we’ll need to add the AuthService into the application binder, so that it can be injected into other classes by Jersey:

Open ApplicationBinder in the root of the main package (com.manywho.services.github).

Inside the configure() method, add a new line that binds any calls to AuthService to an instance of AuthService, like so:

1

2

3

4

@Override

protectedvoidconfigure(){

bind(AuthService.class).to(AuthService.class);

}

Creating the AuthController

Create a new class in the controllers package called AuthController.

Make the new class extend AbstractOauth2Controller, which is a base controller class in the ManyWho SDK for Services that wish to implement authorization using OAuth2.

Press Ctrl+I then Enter to implement the required authentication() method.

Press Alt+Insert then click Constructor to implement the required constructor that inherits from the AbstractOauth2Controller class.

Now add a new field of type AuthService called authService, and annotate it with the @Inject annotation (so Jersey will automatically supply an instance into the class at runtime).

Finally, add a new method called authentication that returns an AuthenticatedWhoResult, and takes a single AuthenticationCredentials parameter.

Inside this method, return the result of a call to authService.authenticateWithGitHub(oauth2Provider, getOauthService(), authenticationCredentials);