Instagram

Instagram

While the Instagram API mainly focuses on media, is also has methods for accessing locations, users and similar.

The Instagram API is divided into multiple endpoints serving different purposes - eg. media, users and locations. Skybrud.Social follows the same structure as the API, so head over to the list of endpoints for more information. This is also where you will find examples for the various endpoints / methods in the Instagram API.

Authentication

To get data from the Instagram API, you either need an access token (on behalf of a user) or a client ID (on behalf of your app). Information about obtaining either of these can be found on the authentication page.

Rate limiting

The Instagram API enforces rate limiting - meaning that you can only make a certain amount of request over a given time. Most methods have rather high limit, while a few methods has a very low limit. Instagram.com has a good description of limits.

Making calls to the API

To make requests to the Instagram API, you must initialize an instance of the InstagramService class. As described on the authentication page, you can make requests to the API using either an access token or a client ID.

So to initialize the class using an obtained access token, your code could look as:

// Initialize a new service instance from an access token
InstagramService service = InstagramService.CreateFromAccessToken(accessToken);

The InstagramService class builds on top of the InstagramClient class, s you can also specify an instance of this class instead. This scenario can be used if you need to specify a client ID instead of an access token:

// Initialize a new service instance from an OAuth client
InstagramService service = InstagramService.CreateFromOAuthClient(new InstagramOAuthClient {
    ClientId = clientId
});

In future versions (after v0.9.3), you can also create a new instance of InstagramService directly from a client ID. You can see more about this at the page of the issue on GitHub.

// Initialize a new service instance from a client ID
InstagramService service = InstagramService.CreateFromClientId(clientId);

The InstagramService class then has properties for each supported endpoint. As an example, you can get information about the authenticated user as:

// Make the call to the API
InstagramUserResponse response = service.Users.GetSelf();

// Get the user object from the response
InstagramUser user = response.Body.Data;

You can find a list of all supported endpoints at the the endpoints page.

Error handling

If you make a request to the Instagram API, and the server responds with an error, you can handle this by catching exception of the type InstagramException. An example of an error returned by the API, is if you make a request for a user that doesn't exist. You you make a request with an invalid access token or client ID, this will also trigger an error.

Some errors/exceptions are represented by their own exception class (extending theInstagramException class). These are currently:

  • InstagramNotFoundException
  • InstagramOAuthException
  • InstagramOAuthAccessTokenException