Search code examples
phpinstagraminstagram-api

Find a Instagram Users ID for API Access (As of 2018)


The Problem

I have built an Instagram Feed on a friend's WordPress website to display the eight latest images from a specified feed.

I hit this endpoint to achieve this:

https://api.instagram.com/v1/users/{$ig_user_id}/media/recent/?access_token={$ig_access_token}

Up until now, I have used my own test instagram client to populate the feed.

We're gearing up to go live now, and I've attempted to replace the Instagram User ID and Access Token with that of my friend's instagram account. Whilst I can still generate an access token (using this tool @ PixelUnion), I can no longer find a way to access the User ID.

There is an existing SO thread & solution here, which no longer seems to be valid:

Instagram how to get my user id from username?

The Question

How does one go about finding an Instagram accounts User ID since apparent restrictions to the Instagram API? (~ mid 2018)

My Instagram Feed Code (PHP)

<?php

  function prettify_instagram_image($image_data) {
    // Plucks just what we need from the data returned from the Instagram API
    $pretty_data= (object)[
      "id" => $image_data->id  ? $image_data->id : false,
      "src_url" => $image_data->images->standard_resolution->url ? $image_data->images->standard_resolution->url : false,
      "ig_link" => $image_data->link ? $image_data->link : false,
      "likes" => $image_data->likes->count ? $image_data->likes->count : false,
      "comments" => $image_data->comments->count ? $image_data->comments->count : false

    ];
    return $pretty_data;
  }
  $ig_user_id = get_field('instagram_user_id', 'option'); // Pulling IG account info from WP Admin Options
  $ig_access_token = get_field('instagram_access_token', 'option');
  $ig_api_root = 'https://api.instagram.com/v1/users/';
  $ig_api_method = '/media/recent/';
  $images_to_display = 8;
  // https://api.instagram.com/v1/users/{user-id}/media/recent/?access_token=ACCESS-TOKEN


  // Instagram API connection
  $response = wp_remote_get( $ig_api_root . $ig_user_id . $ig_api_method . "?access_token=" . $ig_access_token );

  // Instagram response is JSON encoded, let's convert it to an object
  $instagram_response = !is_wp_error($response) ? json_decode( $response['body'] ) : false;

  $instagram_images = ($instagram_response && $instagram_response->data) ? array_map("prettify_instagram_image", $instagram_response->data) : false;
?>

Solution

  • I've found that, providing you have an access token, you can perform the following request in your browser:

    https://api.instagram.com/v1/users/self?access_token=[VALUE]

    In fact, access token contain the User ID (the first segment of the token):

    <user-id>.1677aaa.aaa042540a2345d29d11110545e2499