After a user has granted your application access to their service provider profile, you'll be able to interact with that service provider to update or retrieve the user's data. Your application may, for example, post a Tweet on behalf of a user or review a user's list of contacts to see if any of them have also created connections to your application.
Each service provider exposes their data and functionality through an API. Spring Social provides Java-based access to those APIs via provider-specific templates, each implementing a provider operations interface.
Spring Social comes with six provider API templates/operations for the following service providers:
TripIt
GitHub
Gowalla
From a user's perspective, Twitter's function is rather simple: Enable users to post whatever they're thinking, 140 characters at a time.
In contrast, Twitter's API is rather rich, enabling applications to interact with Twitter in ways that may not be obvious from the humble tweet box.
Spring Social offers interaction with Twitter's service API through the TwitterApi
interface and its implementation, TwitterTemplate
.
Creating an instance of TwitterTemplate
involves invoking its constructor, passing in the application's OAuth credentials and an access token/secret pair authorizing the application to act on a user's behalf.
For example:
String consumerKey = "..."; // The application's consumer key String consumerSecret = "..."; // The application's consumer secret String accessToken = "..."; // The access token granted after OAuth authorization String accessTokenSecret = "..."; // The access token secret granted after OAuth authorization TwitterApi twitterApi = new TwitterTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);
In addition, TwitterTemplate
has a default constructor that creates an instance without any OAuth credentials:
TwitterApi twitterApi = new TwitterTemplate();
When constructed with the default constructor, TwitterTemplate
will allow a few simple operations that do not require authorization, such as searching.
Attempting other operations, such as tweeting will fail with an IllegalStateException
being thrown.
If you are using Spring Social's service provider framework, as described in Chapter 2, Service Provider 'Connect' Framework, you can get an instance of TwitterApi
via a Connection
.
For example, the following snippet calls getApi()
on a connection to retrieve a TwitterApi
:
Connection<TwitterApi> connection = connectionRepository.findPrimaryConnectionToApi(TwitterApi.class);
TwitterApi twitterApi = connection.getApi();
Here, ConnectionRepository
is being asked for the primary connections that the current user has with Twitter.
From that connection, it retrieves a TwitterApi
instance that is configured with the connection details received when the connection was first established.
Once you have a TwitterApi
, you can perform a several operations against Twitter.
TwitterApi
is defined as follows:
public interface TwitterApi { boolean isAuthorizedForUser(); DirectMessageOperations directMessageOperations(); FriendOperations friendOperations(); ListOperations listOperations(); SearchOperations searchOperations(); TimelineOperations timelineOperations(); UserOperations userOperations(); }
The isAuthorizedForUser
helps determine if the TwitterApi
instance has been created with credentials to perform on behalf of a user.
It will return true if it is capable of performing operations requiring authorization; false otherwise.
The remaining six methods return sub-APIs, partitioning the Twitter service API into divisions targeting specific facets of Twitter functionality. These sub-APIs are defined by interfaces described in Table 6.1, “TwitterApi's Sub-APIs”.
Table 6.1. TwitterApi's Sub-APIs
Sub-API Interface | Description |
---|---|
DirectMessageOperations | Reading and sending direct messages. |
FriendOperations | Retrieving a user's list of friends and followers and following/unfollowing users. |
ListOperations | Maintaining, subscribing to, and unsubscripting from user lists |
SearchOperations | Searching tweets and viewing search trends |
TimelineOperations | Reading timelines and posting tweets. |
UserOperations | Retrieving user profile data. |
What follows is a survey of common tasks you may perform with TwitterApi
and its sub-APIs.
For complete details on the Spring Social's entire Twitter API binding, refer to the JavaDoc.
To get a user's Twitter profile, call UserOperations
' getUserProfile()
:
TwitterProfile profile = twitterApi.userOperations().getUserProfile();
This returns a TwitterProfile
object containing profile data for the authenticated user.
This profile information includes the user's Twitter screen name, their name, location, description, and the date that they created their Twitter account.
Also included is a URL to their profile image.
If you want to retrieve the user profile for a specific user other than the authenticated user, you can so do by passing the user's screen name as a parameter to getUserProfile()
:
TwitterProfile profile = twitterApi.userOperations().getUserProfile("habuma");
If all you need is the screen name for the authenticating user, then call UserOperations.getScreenName()
:
String profileId = twitterApi.userOperations().getScreenName();
To post a message to Twitter the simplest thing to do is to pass the message to the updateStatus()
method provided by TimelineOperations
:
twitterApi.timelineOperations().updateStatus("Spring Social is awesome!")
Optionally, you may also include metadata about the tweet, such as the location (latitude and longitude) you are tweeting from.
For that, pass in a StatusDetails
object, setting the location property:
StatusDetails statusDetails = new StatusDetails().setLocation(51.502f, -0.126f); twitterApi.timelineOperations().updateStatus("I'm tweeting from London!", statusDetails)
To have Twitter display the location in a map (on the Twitter web site) then you should also set the displayCoordinates
property to true
:
StatusDetails statusDetails = new StatusDetails().setLocation(51.502f, -0.126f).setDisplayCoordinates(true); twitterApi.timelineOperations().updateStatus("I'm tweeting from London!", statusDetails)
If you'd like to retweet another tweet (perhaps one found while searching or reading the Twitter timeline), call the retweet()
method, passing in the ID of the tweet to be retweeted:
long tweetId = tweet.getId();
twitterApi.timelineOperations().retweet(tweetId);
Note that Twitter disallows repeated tweets.
Attempting to tweet or retweet the same message multiple times will result in a DuplicateTweetException
being thrown.
From a Twitter user's perspective, Twitter organizes tweets into four different timelines:
User - Includes tweets posted by the user.
Friends - Includes tweets from the user's timeline and the timeline of anyone that they follow, with the exception of any retweets.
Home - Includes tweets from the user's timeline and the timeline of anyone that they follow.
Public - Includes tweets from all Twitter users.
To be clear, the only difference between the home timeline and the friends timeline is that the friends timeline excludes retweets.
TimelineOperations
also supports reading of tweets from one of the available Twitter timelines.
To retrieve the 20 most recent tweets from the public timeline, use the getPublicTimeline()
method:
List<Tweet> tweets = twitterApi.timelineOperations().getPublicTimeline();
getHomeTimeline()
retrieves the 20 most recent tweets from the user's home timeline:
List<Tweet> tweets = twitterApi.timelineOperations().getHomeTimeline();
Similarly, getFriendsTimeline()
retrieves the 20 most recent tweets from the user's friends timeline:
List<Tweet> tweets = twitterApi.timelineOperations().getFriendsTimeline();
To get tweets from the authenticating user's own timeline, call the getUserTimeline()
method:
List<Tweet> tweets = twitterApi.timelineOperations().getUserTimeline();
If you'd like to retrieve the 20 most recent tweets from a specific user's timeline (not necessarily the authenticating user's timeline), pass the user's screen name in as a parameter to getUserTimeline()
:
List<Tweet> tweets = twitterApi.timelineOperations().getUserTimeline("rclarkson");
In addition to the four Twitter timelines, you may also want to get a list of tweets mentioning the user.
The getMentions()
method returns the 20 most recent tweets that mention the authenticating user:
List<Tweet> tweets = twitterApi.timelineOperations().getMentions();
A key social concept in Twitter is the ability for one user to "follow" another user.
The followed user's tweets will appear in the following user's home and friends timelines.
To follow a user on behalf of the authenticating user, call the FriendOperations
' follow()
method:
twitterApi.friendOperations().follow("habuma");
Similarly, you may stop following a user using the unfollow()
method:
twitterApi.friendOperations().unfollow("habuma");
If you want to see who a particular user is following, use the getFriends()
method:
List<TwitterProfile> friends = twitterApi.friendOperations().getFriends("habuma");
On the other hand, you may be interested in seeing who is following a given user.
In that case the getFollowers()
method may be useful:
List<TwitterProfile> followers = twitterApi.friendOperations().getFollowers("habuma");
In addition to following other users, Twitter provides the ability for users to collect users in lists, regardless of whether or not they are being followed. These lists may be private to the use who created them or may be public for others to read and subscribe to.
To create a new list, use ListOperations
' createList()
method:
UserList familyList = twitterApi.listOperations().createList( "My Family", "Tweets from my immediate family members", false);
createList()
takes three parameters and returns a UserList
object representing the newly created list.
The first parameter is the name of the list.
The second parameter is a brief description of the list.
The final parameter is a boolean indicating whether or not the list is public.
Here, false indicates that the list should be private.
Once the list is created, you may add members to the list by calling the addToList()
method:
twitterApi.listOperations().addToList(familyList.getSlug(), "artnames");
The first parameter given to addToList()
is the list slug (which is readily available from the UserList
object).
The second parameter is the screen name of a user to add to the list.
To remove a member from a list, pass the same parameters to removedFromList()
:
twitterApi.listOperations().removeFromList(familyList.getSlug(), "artnames");
You can also subscribe to a list on behalf of the authenticating user.
Subscribing to a list has the effect of including tweets from the list's members in the user's home timeline.
The subscribe()
method is used to subscribe to a list:
twitterApi.listOperations().subscribe("habuma", "music");
Here, subscribe()
is given the list owner's screen name ("habuma") and the list slug ("music").
Similarly, you may unsubscribe from a list with the unsubscribe()
method:
twitterApi.listOperations().unsubscribe("habuma", "music");
SearchOperations
enables you to search the public timeline for tweets containing some text through its search()
method.
For example, to search for tweets containing "#spring":
SearchResults results = twitterApi.searchOperations().search("#spring");
The search()
method will return a SearchResults
object that includes a list of 50 most recent matching tweets as well as some metadata concerning the result set.
The metadata includes the maximum tweet ID in the search results list as well as the ID of a tweet that precedes the resulting tweets.
The sinceId
and maxId
properties effectively define the boundaries of the result set.
Additionally, there's a boolean lastPage
property that, if true
, indicates that this result set is the page of results.
To gain better control over the paging of results, you may choose to pass in the page and results per page to search()
:
SearchResults results = twitterApi.searchOperations().search("#spring", 2, 10);
Here, we're asking for the 2nd page of results where the pages have 10 tweets per page.
Finally, if you'd like to confine the bounds of the search results to fit between two tweet IDs, you may pass in the since and maximum tweet ID values to search()
:
SearchResults results = twitterApi.searchOperations().search("#spring", 2, 10, 145962, 210112);
This ensures that the result set will not contain any tweets posted before the tweet whose ID is 146962 nor any tweets posted after the tweet whose ID is 210112.
In addition to posting tweets to the public timelines, Twitter also supports sending of private messages directly to a given user.
DirectMessageOperations
' sendDirectMessage()
method can be used to send a direct message to another user:
twitterApi.directMessageOperations().sendDirectMessage("kdonald", "You going to the Dolphins game?")
DirectMessageOperations
can also be used to read direct messages received by the authenticating user through its getDirectMessagesReceived()
method:
List<DirectMessage> twitterApi.directMessageOperations().getDirectMessagesReceived();
getDirectMessagesReceived()
will return the 20 most recently received direct messages.
Spring Social's FacebookApi
and its implementation, FacebookTemplate
provide the operations needed to interact with Facebook on behalf of a user.
Creating an instance of FacebookTemplate
is as simple as constructing it by passing in an authorized access token to the constructor:
String accessToken = "f8FX29g..."; // access token received from Facebook after OAuth authorization FacebookApi facebook = new FacebookTemplate(accessToken);
If you are using Spring Social's service provider framework, as described in Chapter 2, Service Provider 'Connect' Framework, you can get an instance of FacebookApi
via a Connection
.
For example, the following snippet calls getApi()
on a connection to retrieve a FacebookApi
:
Connection<FacebookApi> connection = connectionRepository.findPrimaryConnectionToApi(FacebookApi.class);
FacebookApi facebookApi = connection.getApi();
Here, ConnectionRepository
is being asked for the primary connections that the current user has with Facebook.
From that connection, it retrieves a FacebookApi
instance that is configured with the connection details received when the connection was first established.
With a FacebookApi
in hand, there are several ways you can use it to interact with Facebook on behalf of the user.
Spring Social's Facebook API binding is divided into 9 sub-APIs exposes through the methods of FacebookApi
:
public interface FacebookApi extends GraphApi { CommentOperations commentOperations(); EventOperations eventOperations(); FeedOperations feedOperations(); FriendOperations friendOperations(); GroupOperations groupOperations(); LikeOperations likeOperations(); MediaOperations mediaOperations(); PlacesOperations placesOperations(); UserOperations userOperations(); }
The sub-API interfaces returned from FacebookApi
's methods are described in Table 6.2, “FacebookApi's Sub-APIs”.
Table 6.2. FacebookApi's Sub-APIs
Sub-API Interface | Description |
---|---|
CommentOperations | Add, delete, and read comments on Facebook objects. |
EventOperations | Create and maintain events and RSVP to event invitations. |
FeedOperations | Read and post to a Facebook wall. |
FriendOperations | Retrieve a user's friends and maintain friend lists. |
GroupOperations | Retrieve group details and members. |
LikeOperations | Retrieve a user's interests and likes. Like and unlike objects. |
MediaOperations | Maintain albums, photos, and videos. |
PlacesOperations | Checkin to location in Facebook Places and retrieve places a user and their friends have checked into. |
UserOperations | Retrieve user profile data and profile images. |
The following sections will give an overview of common tasks that can be performed via FacebookApi
and its sub-APIs.
For complete details on all of the operations available, refer to the JavaDoc.
You can retrieve a user's Facebook profile data using FacebookApi
' getUserProfile()
method:
FacebookProfile profile = facebookApi.userOperations().getUserProfile();
The FacebookProfile
object will contain basic profile information about the authenticating user, including their first and last name and their Facebook ID.
Depending on what authorization scope has been granted to the application, it may also include additional details about the user such as their email address, birthday, hometown, and religious and political affiliations.
For example, getBirthday()
will return the current user's birthday if the application has been granted "user_birthday" permission; null otherwise.
Consult the JavaDoc for FacebookProfile
for details on which permissions are required for each property.
If all you need is the user's Facebook ID, you can call getProfileId()
instead:
String profileId = facebookApi.userOperations().getProfileId();
Or if you want the user's Facebook URL, you can call getProfileUrl()
:
String profileUrl = facebookApi.userOperations().getProfileUrl();
An essential feature of Facebook and other social networks is creating a network of friends or contacts.
You can access the user's list of Facebook friends by calling the getFriendIds()
method from FriendOperations
:
List<String> friendIds = facebookApi.friendOperations().getFriendIds();
This returns a list of Facebook IDs belonging to the current user's list of friends.
This is just a list of String
IDs, so to retrieve an individual user's profile data, you can turn around and call the getUserProfile()
, passing in one of those IDs to retrieve the profile data for an individual user:
FacebookProfile firstFriend = facebookApi.userOperations().getUserProfile(friendIds.get(0));
Or you can get a list of user's friends as FacebookProfile
s by calling getFriendProfiles()
:
List<FacebookProfile> friends = facebookApi.friendOperations().getFriendProfiles();
Facebook also enables users to organize their friends into friend lists.
To retrieve a list of the authenticating user's friend lists, call getFriendLists()
with no arguments:
List<Reference> friends = facebookApi.friendOperations().getFriendLists();
You can also retrieve a list of friend lists for a specific user by passing the user ID (or an alias) to getFriendLists()
:
List<Reference> friends = facebookApi.friendOperations().getFriendLists("habuma");
getFriendLists()
returns a list of Reference
objects that carry the ID and name of each friend list.
To retieve a list of friends who are members of a specific friend list call getFriendListMembers()
, passing in the ID of the friend list:
List<Reference> friends = facebookApi.friendOperations().getFriendListMembers("193839228");
FriendOperations
also support management of friend lists.
For example, the createFriendList()
method will create a new friend list for the user:
Reference collegeFriends = facebookApi.friendOperations().createFriendList("College Buddies");
createFriendList()
returns a Reference
to the newly created friend list.
To add a friend to the friend list, call addToFriendList()
:
facebookApi.friendOperations().addToFriendList(collegeFriends.getId(), "527631174");
addToFriendList()
takes two arguments: The ID of the friend list and the ID (or alias) of a friend to add to the list.
In a similar fashion, you may remove a friend from a list by calling removeFromFriendList()
:
facebookApi.friendOperations().removeFromFriendList(collegeFriends.getId(), "527631174");
To post a message to the user's Facebook wall, call FeedOperations
' updateStatus()
method, passing in the message to be posted:
facebookApi.feedOperations().updateStatus("I'm trying out Spring Social!");
If you'd like to attach a link to the status message, you can do so by passing in a FacebookLink
object along with the message:
FacebookLink link = new FacebookLink("http://www.springsource.org/spring-social", "Spring Social", "The Spring Social Project", "Spring Social is an extension to Spring to enable applications to connect with service providers."); facebookApi.feedOperations().updateStatus("I'm trying out Spring Social!", link);
When constructing the FacebookLink
object, the first parameter is the link's URL, the second parameter is the name of the link, the third parameter is a caption, and the fourth is a description of the link.
If you want to read posts from a user's feed, FeedOperations
has several methods to choose from.
The getFeed()
method retrieves recent posts to a user's wall.
When called with no parameters, it retrieves posts from the authenticating user's wall:
List<Post> feed = facebookApi.feedOperations().getFeed();
Or you can read a specific user's wall by passing their Facebook ID to getFeed()
:
List<Post> feed = facebookApi.feedOperations().getFeed("habuma");
In any event, the getFeed()
method returns a list of Post
objects.
The Post
class has six subtypes to represent different kinds of posts:
CheckinPost
- Reports a user's checkin in Facebook Places.
LinkPost
- Shares a link the user has posted.
NotePost
- Publicizes a note that the user has written.
PhotoPost
- Announces a photo that the user has uploaded.
StatusPost
- A simple status.
VideoPost
- Announces a video that the user has uploaded.
The Post
's getType()
method identifies the type of Post
.
LinkedIn is a social networking site geared toward professionals. It enables its users to maintain and correspond with a network of contacts they have are professionally linked to.
Spring Social offers integration with LinkedIn via LinkedInApi
and its implementation, LinkedInTemplate
.
To create an instance of LinkedInTemplate
, you may pass in your application's OAuth 1 credentials, along with an access token/secret pair to the constructor:
String consumerKey = "..."; // The application's consumer key String consumerSecret = "..."; // The application's consumer secret String accessToken = "..."; // The access token granted after OAuth authorization String accessTokenSecret = "..."; // The access token secret granted after OAuth authorization LinkedInApi linkedinApi = new LinkedInTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);
If you are using Spring Social's service provider framework, as described in Chapter 2, Service Provider 'Connect' Framework, you can get an instance of LinkedInApi
via a Connection
.
For example, the following snippet calls getApi()
on a connection to retrieve a LinkedInApi
:
Connection<LinkedInApi> connection = connectionRepository.findPrimaryConnectionToApi(LinkedInApi.class);
LinkedInApi linkedinApi = connection.getApi();
Here, ConnectionRepository
is being asked for the primary connections that the current user has with LinkedIn.
From that connection, it retrieves a LinkedInApi
instance that is configured with the connection details received when the connection was first established.
Once you have a LinkedInApi
you can use it to interact with LinkedIn on behalf of the user who the access token was granted for.
To retrieve the authenticated user's profile data, call the getUserProfile()
method:
LinkedInProfile profile = linkedin.getUserProfile();
The data returned in the LinkedInProfile
includes the user's LinkedIn ID, first and last names, their "headline", the industry they're in, and URLs for the public and standard profile pages.
If it's only the user's LinkedIn ID you need, then you can get that by calling the getProfileId()
method:
String profileId = linkedin.getProfileId();
Or if you only need a URL for the user's public profile page, call getProfileUrl()
:
String profileUrl = linkedin.getProfileUrl();
To retrieve a list of LinkedIn users to whom the user is connected, call the getConnections()
method:
List<LinkedInProfile> connections = linkedin.getConnections();
This will return a list of LinkedInProfile
objects for the user's 1st-degree network (those LinkedIn users to whom the user is directly linked--not their extended network).
TripIt is a social network that links together travelers. By connecting with other travelers, you can keep in touch with contacts when your travel plans coincide. Also, aside from its social aspects, TripIt is a rather useful service for managing one's travel information.
Using Spring Social's TripItApi
and its implementation, TripItTemplate
, you can develop applications that integrate a user's travel information and network.
To create an instance of TripItTemplate
, pass in your application's OAuth 1 credentials along with a user's access token/secret pair to the constructor:
String consumerKey = "..."; // The application's consumer key String consumerSecret = "..."; // The application's consumer secret String accessToken = "..."; // The access token granted after OAuth authorization String accessTokenSecret = "..."; // The access token secret granted after OAuth authorization TripItApi tripitApi = new TripItTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);
If you are using Spring Social's service provider framework, as described in Chapter 2, Service Provider 'Connect' Framework, you can get an instance of TripItApi
via a Connection
.
For example, the following snippet calls getApi()
on a connection to retrieve a TripItApi
:
Connection<TripItApi> connection = connectionRepository.findPrimaryConnectionToApi(TripItApi.class);
TripItApi tripitApi = connection.getApi();
Here, ConnectionRepository
is being asked for the primary connections that the current user has with TripIt.
From that connection, it retrieves a TripItApi
instance that is configured with the connection details received when the connection was first established.
In either event, once you have a TripItApi
, you can use it to retrieve a user's profile and travel data from TripIt.
TripItApi
' getUserProfile()
method is useful for retrieving the authenticated user's TripIt profile data.
For example:
TripItProfile userProfile = tripit.getUserProfile();
getUserProfile()
returns a TripItProfile
object that carries details about the user from TripIt.
This includes the user's screen name, their display name, their home city, and their company.
If all you need is the user's TripIt screen name, you can get that by calling getProfileId()
:
String profileId = tripit.getProfileId();
Or if you only need a URL to the user's TripIt profile page, then call getProfileUrl()
:
String profileUrl = tripit.getProfileUrl();
If the user has any upcoming trips planned, your application can access the trip information by calling getUpcomingTrips()
:
List<Trip> trips = tripit.getUpcomingTrips();
This returns a list of Trip
objects containing details about each trip, such as the start and end dates for the trip, the primary location, and the trip's display name.
Although many developers think of GitHub as Git-based source code hosting, the tagline in GitHub's logo clearly states that GitHub is about "social coding". GitHub is a social network that links developers together and with the projects they follow and/or contribute to.
Spring Social's GitHubApi
and its implementation, GitHubTemplate
, offer integration with GitHub's social platform.
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 GitHubApi githubApi = new GitHubTemplate(accessToken);
If you are using Spring Social's service provider framework, as described in Chapter 2, Service Provider 'Connect' Framework, you can get an instance of GitHubApi
via a Connection
.
For example, the following snippet calls getApi()
on a connection to retrieve a GitHubApi
:
Connection<GitHubApi> connection = connectionRepository.findPrimaryConnectionToApi(GitHubApi.class);
GitHubApi githubApi = connection.getApi();
Here, ConnectionRepository
is being asked for the primary connections that the current user has with GitHub.
From that connection, it retrieves a GitHubApi
instance that is configured with the connection details received when the connection was first established.
With a GitHubApi
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 GitHubApi
'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();
Gowalla is a location-based social network where users may check in to various locations they visit and earn pins and stamps for having checked in a locations that achieve some goal (for example, a "Lucha Libre" pin may be earned by having checked into 10 different Mexican food restaurants).
Spring Social supports interaction with Gowalla through the GowallaApi
interface and its implementation, GowallaTemplate
.
To obtain an instance of GowallaTemplate
, you can instantiate it by passing an authorized access token to its constructor:
String accessToken = "f8FX29g..."; // access token received from Gowalla after OAuth authorization GowallaApi gowallaApi = new GowallaTemplate(accessToken);
If you are using Spring Social's service provider framework, as described in Chapter 2, Service Provider 'Connect' Framework, you can get an instance of GowallaApi
via a Connection
.
For example, the following snippet calls getApi()
on a connection to retrieve a GowallaApi
:
Connection<GowallaApi> connection = connectionRepository.findPrimaryConnectionToApi(GowallaApi.class);
GowallaApi gowallaApi = connection.getApi();
Here, ConnectionRepository
is being asked for the primary connections that the current user has with Gowalla.
From that connection, it retrieves a GowallaApi
instance that is configured with the connection details received when the connection was first established.
With a GowallaApi
in hand, there are a handful of operations it provides to interact with Gowalla on behalf of the user.
These will be covered in the following sections.
You can retrieve a user's Gowalla profile using the getUserProfile()
method:
GowallaProfile profile = gowalla.getUserProfile();
This will return the Gowalla profile data for the authenticated user.
If you want to retrieve profile data for another user, you can pass the user's profile ID into getUserProfile()
:
GowallaProfile profile = gowalla.getUserProfile("habuma");
The GowallaProfile
object contains basic information about the Gowalla user such as their first and last names, their hometown, and the number of pins and stamps that they have earned.
If all you want is the authenticated user's profile ID, you can get that by calling the getProfileId()
:
String profileId = gowalla.getProfileId();
Or if you want the URL to the user's profile page at Gowalla, use the getProfileUrl()
method:
String profileUrl = gowalla.getProfileUrl();
GowallaApi
also allows you to learn about the user's favorite checkin spots.
The getTopCheckins()
method will provide a list of the top 10 places that the user has visited:
List<Checkin> topCheckins = gowalla.getTopCheckins();
Each member of the returns list is a Checkin
object that includes the name of the location as well as the number of times that the user has checked in at that location.