Instagram Getting a list of recent media
Instagram Platform API deprecation
As of April 4th 2018, Instagram has suddenly decided to close down most of their Platform API. This unfortunately also means that many of the features in this package are no longer working. You can read more about the changes to the Instagram Platform API via the links below:

Platform Changelog

API and Other Platform Product Changes

Get a list of recent media

In the users endpoint, the GetRecentMedia method lets you request the most recent media of either the authenticated user or a specific user. The method has a number of overloads.

If you call the method without any parameters, the most recent media of the authenticated user will be returned:

@using Skybrud.Social.Instagram.Models.Media
@using Skybrud.Social.Instagram.Models.Users
@using Skybrud.Social.Instagram.Responses.Users
@inherits WebViewPage<Skybrud.Social.Instagram.InstagramService>
@{
    
    // Make the request to the Instagram API
    InstagramGetUserRecentMediaResponse response = Model.Users.GetRecentMedia();

    // Get a reference to the response body
    InstagramGetUserRecentMediaEnvelope body = response.Body;

    // Iterate through the returned media
    foreach (InstagramMedia media in body.Data) {

        <pre>@media.Link</pre>

    }

}

By using one of the method overloads, you can instead request the most recent media of a specific user. The Instagram doesn't support specifying the username of the user, so you must instead specify the ID of the user:

@using Skybrud.Social.Instagram.Models.Media
@using Skybrud.Social.Instagram.Models.Users
@using Skybrud.Social.Instagram.Responses.Users
@inherits WebViewPage<Skybrud.Social.Instagram.InstagramService>
@{
    
    // Make the request to the Instagram API
    InstagramGetUserRecentMediaResponse response = Model.Users.GetRecentMedia(653220932);

    // Get a reference to the response body
    InstagramGetUserRecentMediaEnvelope body = response.Body;

    // Iterate through the returned media
    foreach (InstagramMedia media in body.Data) {

        <pre>@media.Link</pre>

    }

}

Understanding the response

The GetRecentMedia method will return an instance of the InstagramGetUserRecentMediaResponse class, which tries to represent the entire response received from the Instagram API.

In a similar way, the InstagramGetUserRecentMediaEnvelope class represents the response body. As the API doesn't just return an array with the recent media, but an object with information about the collection (called an envelope), you can access the media through the Data property.

The Data property is then an array of instances of InstagramMedia. This class serves as a base class, so since a media can by either an image, a video or a carousel, the items in the array can may be either an instance of InstagramImage, InstagramVideo or InstagramCarousel.

Pagination and other options

The list of media returned by the Instagram API is paginated, so you will only get a subset of the media the user has posted. If you wish to get media beyond the first page, you can use the InstagramGetUserRecentMediaOptions class. The class has the following properties:

Name Type Description
Count int Gets or sets the maximum amount of media to be returned.
MaxTimestamp EssentialsDateTime Gets or sets the maximum timestamp for the search. Only media before this timestamp is returned.
MinTimestamp EssentialsDateTime Gets or sets the minimum timestamp for the search. Only media after this timestamp is returned.
MinId string Gets or sets the minimum media ID for the search. Only media after this ID is returned.
MaxId string Gets or sets the maximum media ID for the search. Only media before this ID is returned.

You can do pagination based on either timestamps or IDs, but generally using IDs is the most reliable approach since multiple media may share the same timestamp.

@using Skybrud.Social.Instagram.Models.Media
@using Skybrud.Social.Instagram.Options.Users
@using Skybrud.Social.Instagram.Responses.Users
@inherits WebViewPage<Skybrud.Social.Instagram.InstagramService>
@{

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

    // Declare the initial search options
    InstagramGetUserRecentMediaOptions options = new InstagramGetUserRecentMediaOptions {
        UserId = 653220932,
        Count = 5
    };

    // Make the request to the Instagram API
    InstagramGetUserRecentMediaResponse response = Model.Users.GetRecentMedia(options);

    // 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 request to the Instagram API
        response = Model.Users.GetRecentMedia(options);

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

    }

    <pre>@temp.Count</pre>

}