Spring Social GitHub's GitHub interface and its implementation, GitHubTemplate, offer an API binding to GitHub's REST API.
To obtain an instance of GitHubTemplate, you can instantiate it by passing an authorized access token to its constructor:
String accessToken = "f8FX29g..."; // access token received from GitHub after OAuth authorization GitHub github = new GitHubTemplate(accessToken);
If you are using Spring Social's service provider framework, you can get an instance of GitHub from a Connection.
For example, the following snippet calls getApi() on a connection to retrieve a GitHub:
Connection<GitHub> connection = connectionRepository().findPrimaryConnection(GitHub.class); if (connection != null) { GitHub github = connection.getApi(); // ... use GitHub API binding }
Here, ConnectionRepository is being asked for the primary connection that the current user has with GitHub.
If a connection with GitHub is found, it retrieves a GitHub instance that is configured with the connection details received when the connection was first established.
With a GitHub in hand, there are a handful of operations it provides to interact with GitHub on behalf of the user.
These will be covered in the following sections.
To get the currently authenticated user's GitHub profile data, call GitHub's getUserProfile() method:
GitHubUserProfile profile = github.getUserProfile();
The GitHubUserProfile returned from getUserProfile() includes several useful pieces of information about the user, including their...
Name
Username (ie, login name)
Company
Email address
Location
Blog URL
Date they joined GitHub
If all you need is the user's GitHub username, you can get that by calling the getProfileId() method:
String username = github.getProfileId();
And if you need a URL to the user's GitHub profile page, you can use the getProfileUrl() method:
String profileUrl = github.getProfileUrl();