Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

jInstagram Usage

Tony Bargnesi edited this page Oct 19, 2016 · 38 revisions
InstagramService service = new InstagramAuthService()
    						.apiKey("your_client_id")
    						.apiSecret("your_client_secret")
    						.callback("your_callback_url")     
    						.build();

[With Scope]

Note : Multiple scopes can be provided with a space in between. For ex. .scope("likes comments") Please see Instagram API for more details regarding the supported scopes.

Commenting access - You need to register your clientId and Application Name on http://bit.ly/instacomments, to get access to commenting.

InstagramService service = new InstagramAuthService()
    						.apiKey("your_client_id")
    						.apiSecret("your_client_secret")
    						.callback("your_callback_url") 
    						.scope("comments")
    						.build();

Note : An empty token can be define as follows -

private static final Token EMPTY_TOKEN = null;
  • Validate your user against Instagram
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
  • Getting the Access Token
Verifier verifier = new Verifier("verifier you get from the user");
Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
Instagram instagram = new Instagram(accessToken);
  • Or Creating the Instagram Object with enforce signed header (Note: Enforce signed header of the APP settings should be checked)
// 1. your_client_secret: do not use accessToken.getSecret(), it may be null.
// 2. your IPs: comma-separated list of one or more IPs.
//    You can use the 127.0.0.1 loopback address during testing.
Instagram instagram = new Instagram(accessToken.getToken(), "your_client_secret", "your_IPs");
InstagramOembed oembed = new InstagramOembed();
OembedInformation info = oembed.getOembedInformation("http://instagram.com/p/BUG/");
  • Get basic information about a user.
String userId = "3";
UserInfo userInfo = instagram.getUserInfo(userId);

UserInfoData userData = userInfo.getData();
System.out.println("id : " + userData.getId());
System.out.println("first_name : " + userData.getFirstName());
System.out.println("last_name : " + userData.getLastName());
System.out.println("profile_picture : " + userData.getProfilePicture());
System.out.println("website : " + userData.getWebsite());
  • See the authenticated user's feed.
MediaFeed mediaFeed = instagram.getUserFeeds();
List<MediaFeedData> mediaFeeds = mediaFeed.getData();

for (MediaFeedData mediaData : mediaFeeds) {
	System.out.println("id : " + mediaData.getId());
	System.out.println("created time : " + mediaData.getCreatedTime());
	System.out.println("link : " + mediaData.getLink());
	System.out.println("tags : " + mediaData.getTags().toString());
	System.out.println("filter : " + mediaData.getImageFilter());
	System.out.println("type : " + mediaData.getType());

	System.out.println("-- Comments --");
	Comments comments = mediaData.getComments();

	System.out.println("-- Caption --");
	Caption caption = mediaData.getCaption();

	System.out.println("-- Likes --");
	Likes likes = mediaData.getLikes();

	System.out.println("-- Images --");
	Images images = mediaData.getImages();

	ImageData lowResolutionImg = images.getLowResolution();
	ImageData highResolutionImg = images.getHighResolution();
	ImageData thumbnailImg = images.getThumbnail();

	Location location = mediaData.getLocation();
	System.out.println();
}
  • Get the most recent media published by a user.
String userId = "3";
MediaFeed mediaFeed = instagram.getRecentMediaFeed(userId);
  • See the authenticated user's list of media they've liked.
//Add code here
  • Search for a user by name.
String query = "jack";
 UserFeed userFeed = instagram.searchUser(query);

 List<UserFeedData> userList = userFeed.getUserList();
 
//Please see the UserFeedData element for more usage. 
  • Get the list of users this user follows.
String userId = "3";
UserFeed feed = instagram.getUserFollowList(userId);
List<UserFeedData> users = feed.getUserList();

//Please see the UserFeedData element for more usage. 
  • Get the list of users this user is followed by.
String userId = "3";
UserFeed feed = instagram.getUserFollowedByList(userId);
List<UserFeedData> users = feed.getUserList();

//Please see the UserFeedData element for more usage. 
  • List the users who have requested this user's permission to follow
UserFeed feed = instagram.getUserRequestedBy(); 
List<UserFeedData> users = feed.getUserList();
  • Get information about the current user's relationship (follow/following/etc) to another user.
String userId = "3";
RelationshipFeed feed = instagram.getUserRelationship(userId);
 
System.out.println("incoming_status : " + feed.getData().getIncomingStatus());
System.out.println("outgoing_status : " + feed.getData().getOutgoingStatus());
  • Modify the relationship between the current user and the target user
String userId = "3";
RelationshipFeed feed = instagram.setUserRelationship(userId, Relationship.FOLLOW);
 
System.out.println("Meta Code : "       + feed.getMeta().getCode());
System.out.println("Incoming_Status : " + feed.getData().getIncomingStatus());
  • Get information about a media object.
String mediaId = "3";
MediaInfoFeed mediaInfoFeed = instagram.getMediaInfo(mediaId);  
System.out.println("id : "   + mediaInfoFeed.getData().getId());
System.out.println("created_time : " + mediaInfoFeed.getData().getCreatedTime());

//Please see the MediaInfoFeed element for more usage.
  • Search for media in a given area.
double latitude = 48.858844;
double longitude = 2.294351;

MediaFeed feed = instagram.searchMedia(latitude, longitude); 
List<MediaFeedData> feeds = feed.getData();

// Please see the MediaFeedData element for more usage.
  • Get a list of what media is most popular at the moment.
MediaFeed feed = instagram.getPopularMedia();
  • Get a full list of comments on a media.
String mediaId = "555";

MediaCommentsFeed feed = instagram.getMediaComments(mediaId);
List<CommentData> comments = feed.getCommentDataList();

for (CommentData comment : comments) {
	System.out.println("id : " + comment.getId());
	System.out.println("created_time : " + comment.getCreatedTime());
	System.out.println("text : " + comment.getText());

}
  • Create a comment on a media.
String mediaId = "142856861";

MediaCommentResponse response = instagram.setMediaComments(mediaId, "Sample Comment");
CommentData commentData = response.getCommentData();
  • Remove a comment either on the authenticated user's media or authored by the authenticated user.
String mediaId = "12345";
String commentId = "123456";
MediaCommentResponse response = instagram.deleteMediaCommentById(mediaId, commentId);

Meta meta = response.getMeta();
System.out.println("Code : " + meta.getCode());
  • Get a list of users who have liked this media.
String mediaId = "555";
LikesFeed feed = instagram.getUserLikes(mediaId);

List<User> users = feed.getUserList();

for (User user : users) {
	System.out.println("id : " + user.getId());
	System.out.println("full_name : " + user.getFullName());
	System.out.println("user_name : " + user.getUserName());
	System.out.println("profile_picture : " + user.getProfilePictureUrl());
	System.out.println("website : " + user.getWebsiteUrl());
	System.out.println();

}
  • Set a like on this media by the currently authenticated user.
//Code goes here
  • Remove a like on this media by the currently authenticated user.
//Code goes here
  • Get information about a tag object.
String tagName = "nofilter";
TagInfoFeed feed = instagram.getTagInfo(tagName);

TagInfoData tagData = feed.getTagInfo();
System.out.println("name : " + tagData.getTagName());
System.out.println("media_count : " + tagData.getMediaCount());
  • Get a list of recently tagged media.
String tagName = "snow";
TagMediaFeed mediaFeed = instagram.getRecentMediaTags(tagName);

List<MediaFeedData> mediaFeeds = mediaFeed.getData();
  • Search for tags by name - results are ordered first as an exact match, then by popularity.
String query = "snow";
TagSearchFeed searchFeed = instagram.searchTags(query);

List<TagInfoData> tags = searchFeed.getTagList();

for (TagInfoData tagData : tags) {
	System.out.println("name : " + tagData.getTagName());
	System.out.println("media_count : " + tagData.getMediaCount());
	System.out.println();
}
  • Get information about a location.
String locationId = "1";
LocationInfo locationInfo = instagram.getLocationInfo(locationId);

Location location = locationInfo.getLocationData();
 
System.out.println("id : " + location.getId());
System.out.println("name : " + location.getName());
System.out.println("latitude : " + location.getLatitude());
System.out.println("longitude : " + location.getLongitude());
  • Get a list of recent media objects from a given location.
String locationId = "514276";
MediaFeed mediaFeed = instagram.getRecentMediaByLocation(locationId);

List<MediaFeedData> mediaFeeds = mediaFeed.getData();

//Please see the MediaFeed Element for more details....
  • Search for a location by geographic coordinate.
double latitude = 48.858844;
double longitude = 2.29435;
LocationSearchFeed searchFeed = instagram.searchLocation(latitude, longitude);

for (Location location : searchFeed.getLocationList()) {
	System.out.println("id : " + location.getId());
	System.out.println("name : " + location.getName());
	System.out.println("latitude : " + location.getLatitude());
	System.out.println("longitude : " + location.getLongitude());
	System.out.println();
}
  • Meta
Meta meta = mediaFeed.getMeta();

System.out.println("code : " + meta.getCode());
System.out.println("error message : " + meta.getErrorMessage());
System.out.println("error type : " + meta.getErrorType());
  • UserFeedData
List<UserFeedData> users = feed.getUserList();

for (UserFeedData user : users) {
    System.out.println("id : "      + user.getId());
	System.out.println("username : " + user.getUserName());
	System.out.println("first_name : " + user.getFirstName());
	System.out.println("last_name : "       + user.getLastName());
	System.out.println("profile_picture : " + user.getProfilePictureUrl());	
}
  • Caption
Caption caption = mediaData.getCaption();
System.out.println("id : "              + caption.getId());
System.out.println("created time : "    + caption.getCreatedTime());
System.out.println("text : "            + caption.getText());

System.out.println("** From **");
System.out.println("id : "              + caption.getFrom().getId());
System.out.println("full name : "       + caption.getFrom().getFullName());
System.out.println("user name : "       + caption.getFrom().getUsername());
System.out.println("profile picture : " + caption.getFrom().getProfilePicture());
  • Comments
Comments comments = mediaData.getComments();
List<CommentData> commentList = comments.getComments();

for (CommentData comment : commentList) {
	System.out.println("commentId : "    + comment.getId());
	System.out.println("created Time : " + comment.getCreatedTime());
	System.out.println("text : " + comment.getText());
	
	System.out.println("** From **");
	System.out.println("id : "+ comment.getCommentFrom().getId());
	System.out.println("full name : " + comment.getCommentFrom().getFullName());
	System.out.println("user name : " + comment.getCommentFrom().getUsername());
	System.out.println("profile picture : " + comment.getCommentFrom().getProfilePicture());
}
  • Location
Location location = mediaData.getLocation();
System.out.println("id : "+ location.getId());
System.out.println("name : "      + location.getName());
System.out.println("latitude : "  + location.getLatitude());
System.out.println("longitude : " + location.getLongitude());
  • ImageData
ImageData lowResolutionImg = images.getLowResolution();

System.out.println("url : "    + lowResolutionImg.getImageUrl());
System.out.println("width : "  + lowResolutionImg.getImageWidth());
System.out.println("height : " + lowResolutionImg.getImageHeight());     
  • Likes
//Code goes here
  • Pagination

Pagination Example Code

  • User
//Code goes here