Instagram: Like and Unlike

Step 1: Register A New Client here.

Fill in:

Instagram fill form

Copy the CLIENT ID you get after you submit the form.


Step 2: Prompt to get Access Token from Users, and Request for Likes with PHP

So the onclick event I have:

$('#social').on 'click', '.instagram .likes', ->
  mediaId = $(this).data('media-id')
  if !$(this).hasClass('liked')
    Instagram.likePost mediaId, => console.log 'liked'
    Instagram.unlikePost mediaId, => console.log 'unliked'

And inside

Instagram =
  hasAccessToken: ->
  getAccessToken: ->
    clientId = "4axxxxa41axxxxda9cxxxx54axxxxb00"
    redirectUri = "http://localhost/redirect.html" # redirect to this page once the authentication is done
    url = "{ clientId }&response_type=token&scope=likes&redirect_uri=#{ redirectUri }" url, 'authenticate', "width=800,height=700"
   likeActions: (mediaId, like, done) ->
     if !Modernizr.localstorage
       console.log "doesn't support localstorage!"
     if !@hasAccessToken()
       console.log 'no access token'
       console.log 'has access token'
       accessToken = localStorage.getItem('accessToken')
       url = "./instagram/index.php?like=#{ like }&mediaId=#{ mediaId }&token=#{ accessToken }"
       $.ajax(url: url, type: 'GET', complete: done)
  likePost: (mediaId, done) ->
    @likeActions(mediaId, true, done)

  unlikePost: (mediaId, done) ->
    @likeActions(mediaId, false, done)

And redirect.html is where the page goes when the authentication is done:

<!DOCTYPE html>
    accessToken = location.href.replace(/(.*)access\_token\=/, '')
    localStorage.setItem('accessToken', accessToken)
    window.close() // close window after storing accessToken in localStorage


Step 3: Edit index.php

This is where the requests are wrapped and handled:


require 'instagram.class.php';
// get the file here:

// initialize class
$instagram = new Instagram(array(
  'apiKey' => '4axxxxa41axxxxda9cxxxx54axxxxb00',
  'apiSecret' => '8c0xxxxxx44845xxxxxxxe43axxxfeaf'

$like = $_GET['like'];
$token = $_GET['token'];
$media_id = $_GET['mediaId'];


if ($like === "true") {
} else {