WEBデザインの勉強

初心者が1からWEBデザインを勉強するブログ。

InstagramAPI 最新の画像を取得して表示

アカウントの作成

www.instagram.com

アプリケーションの登録を行う

Instagram Developer Documentation

アクセストークンを取得する

https://api.instagram.com/oauth/authorize/?client_id=【CLIENT-ID】&redirect_uri=【REDIRECT-URI】&response_type=token

ユーザーIDを取得する

smashballoon.com

JSON取得

https://api.instagram.com/v1/users/self/media/recent?access_token=【アクセストークン】

画像を表示

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>InstagramAPI</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <p class="insta_load"></p>
  <ul class="instalist"></ul>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <script src="script.js"></script>
</body>
</html>
/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
  display: block;
}
body {
  line-height: 1;
  max-width: 2000px;
  margin: 0 auto;
}

.instalist {
  letter-spacing: -.4em;
}

.instalist .instalist__item {
  position: relative;
  display: inline-block;
  width: 20%;
  padding-top: 20%;
  letter-spacing: 0;
}

.instalist .instalist__item .instaimg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center center;
}

.instalist .instalist__item a {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  width: 100%;
  height: 100%;
}
$(function(){
  $(".insta_load").text("loading...");
  var userId = 【ユーザーIDを入力】;
  $.ajax({
    url: 'https://api.instagram.com/v1/users/' + userId + '/media/recent',
    data: { access_token: '【アクセストークンを入力】' },
    dataType: 'jsonp',
    error: function(jqXHR, textStatus, errorThrown) {
      $(".insta_load").text("error");
    },
    success: function(data, textStatus, jqXHR) {
      $(".insta_load").text("");
      for (i=0;i<=5;i++){
        var d = data.data[i];
        var _html;
        _html = '<li class="instalist__item"><div class="instaimg" style="background-image: url(' + d.images.standard_resolution.url + ');"></div>';
        _html += '<a href="' + d.link + '" target="_blank"></a></li>';
        $(".instalist").append(_html);
      }
    }
  });
})


参考サイト
capotast.co.jp