使用Instagram的d3?

时间:2015-03-18 21:31:27

标签: javascript d3.js instagram oembed

使用python -m SimpleHTTPServer 8080来测试js,我已经构建了一个d3 viz,但是使用以下代码访问instagram's api using oembed时遇到了问题:

d3.json("http://api.instagram.com/oembed?url=https://instagram.com/p/rigacvhhTe/", function(error, json) {
  if (error) return console.warn(error);
  console.log(json);
});

错误:

XMLHttpRequest cannot load http://api.instagram.com/oembed?url=https://instagram.com/p/rigacvhhTe/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

成功的回复应该返回.json:

{
provider_url: "http://instagram.com/",
media_id: "784331840172725470_179100388",
author_name: "simplyclimb",
height: null,
thumbnail_url: "http://photos-d.ak.instagram.com/hphotos-ak-xpa1/t51.2885-15/10523538_897068253642371_552234973_n.jpg",
thumbnail_width: 640,
thumbnail_height: 640,
provider_name: "Instagram",
title: "Big day.",
html: "<blockquote class="instagram-media" data-instgrm-captioned data-instgrm-version="4" style=" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);"><div style="padding:8px;"> <div style=" background:#F8F8F8; line-height:0; margin-top:40px; padding:50% 0; text-align:center; width:100%;"> <div style=" background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAAGFBMVEUiIiI9PT0eHh4gIB4hIBkcHBwcHBwcHBydr+JQAAAACHRSTlMABA4YHyQsM5jtaMwAAADfSURBVDjL7ZVBEgMhCAQBAf//42xcNbpAqakcM0ftUmFAAIBE81IqBJdS3lS6zs3bIpB9WED3YYXFPmHRfT8sgyrCP1x8uEUxLMzNWElFOYCV6mHWWwMzdPEKHlhLw7NWJqkHc4uIZphavDzA2JPzUDsBZziNae2S6owH8xPmX8G7zzgKEOPUoYHvGz1TBCxMkd3kwNVbU0gKHkx+iZILf77IofhrY1nYFnB/lQPb79drWOyJVa/DAvg9B/rLB4cC+Nqgdz/TvBbBnr6GBReqn/nRmDgaQEej7WhonozjF+Y2I/fZou/qAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;"></div></div> <p style=" margin:8px 0 0 0; padding:0 4px;"> <a href="https://instagram.com/p/rigacvhhTe/" style=" color:#000; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none; word-wrap:break-word;" target="_top">Big day.</a></p> <p style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;">A photo posted by @simplyclimb on <time style=" font-family:Arial,sans-serif; font-size:14px; line-height:17px;" datetime="2014-08-11T01:14:23+00:00">Aug 10, 2014 at 6:14pm PDT</time></p></div></blockquote> <script async defer src="//platform.instagram.com/en_US/embeds.js"></script>",
width: 658,
version: "1.0",
author_url: "http://instagram.com/simplyclimb",
author_id: 179100388,
type: "rich"
}

对于我在这里做错了什么建议?

1 个答案:

答案 0 :(得分:0)

我尝试使用this plugin作为JSONP格式,但照片没有渲染(我正在运行Chrome),只显示了标题。

d3.jsonp = function (url, callback) {
  function rand() {
    var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
        c = '', i = -1;
    while (++i < 15) c += chars.charAt(Math.floor(Math.random() * 52));
    return c;
  }

  function create(url) {
    var e = url.match(/callback=d3.jsonp.(\w+)/),
        c = e ? e[1] : rand();
    d3.jsonp[c] = function(data) {
      callback(data);
      delete d3.jsonp[c];
      script.remove();
    };
    return 'd3.jsonp.' + c;
  }

  var cb = create(url),
      script = d3.select('head')
        .append('script')
        .attr('type', 'text/javascript')
        .attr('src', url.replace(/(\{|%7B)callback(\}|%7D)/, cb));
};

function wtf_jsonp(instaG) {
  d3.select('.photo').remove();

  d3.select("body")
    .append('div')
    .attr('id','photo')
    .html(instaG.html);
};

var circle = d3.select('svg')
.append('circle')
.attr("cx",100)
.attr("cy",50)
.attr("r",30)
.attr("fill","red")
.attr("stroke","black");

circle.on("mouseover",function(event){
    d3.jsonp("http://api.instagram.com/oembed?url=" + "https://instagram.com/p/rigacvhhTe/" + "&callback=wtf_jsonp");
});

请参阅以下错误:enter image description here

作为替代方案,我只使用仅渲染图像的shortcode

var circle = d3.select('svg')
.append('circle')
.attr("cx",100)
.attr("cy",50)
.attr("r",30)
.attr("fill","red")
.attr("stroke","black");

circle.on("mouseover",function(event){
    d3.select('body').append('img')
      .attr('src','http://instagram.com/p/' + "rigacvhhTe" +'/media/?size=l');
});

请参阅下面的成功呈现: enter image description here 这个解决方案适用于我的目的。

知道我在d3.jsonp()电话中犯了什么错误吗?