Instagram Get recent media from a location

GetRecentMedia

Based on the ID, you can use the GetRecentMedia method to get recent media from a given location.

An example on how to use this method could something like:

// Make the call to the API
InstagramLocationRecentMediaResponse response = service.Locations.GetRecentMedia(241201111);

// Iterate through the retrieved media
foreach (InstagramMedia media in response.Body.Data) {

    Console.WriteLine(media.CaptionText);
    Console.WriteLine(media.Link);
    Console.WriteLine();

}

Pagination

The result for recent media is paginated, so the example above will only return the first page. The method in the Instagram API seems to have a default limit of 20 if nothing else is specified.

To specify any options for the call to the API, we can specify an instance of InstagramLocationRecentMediaOptions as the second parameter. This class lets you set limit the amount of media returned on each page, as well as a few options for either timestamp or ID based pagination.

ID based pagination is the recommended approach, since it is the most reliable.

// Temporary list for storing the retrieved media
List<InstagramMedia> temp = new List<InstagramMedia>();

// Declare the initial options
InstagramLocationRecentMediaOptions options = new InstagramLocationRecentMediaOptions {
    Count = 10
};

// Make the call to the API
InstagramLocationRecentMediaResponse response = service.Locations.GetRecentMedia(241201111, new InstagramLocationRecentMediaOptions());

// Add the media to the list
temp.AddRange(response.Body.Data);

// No reason to make another request if the first request didn't return any media
if (temp.Count > 0) {

    // Update the options
    options.MaxId = temp.Last().Id;

    // Make another call to the Instagram API
    response = service.Locations.GetRecentMedia(241201111, options);

    // Add the media to the list
    temp.AddRange(response.Body.Data);

}