从自定义主题中调用Tumblr API

4
我正在为需要使用Tumblr平台的客户编写自定义主题。所需功能超出了自定义主题标签提供的范围,因此我需要通过API调用来添加更好的用户体验。
虽然我是一名经验丰富的开发人员,但对Tumblr完全陌生。是否有一种方法可以在我的主题中从API进行调用,而不需要OAuth回调?
目前,我的主题包括一个HTML文件和一个外部JS文件,该文件执行许多其他操作(非Ajax类型的操作)。
理想情况下,如下所示:
  request = new XMLHttpRequest();
  request.open('GET', 'http://api.tumblr.com/v2/blog/fdsfdsf.tumblr.com/posts', true);
  request.onerror = _handleError;
  request.onload = _handleResponse;
  request.send();

2
注册一个新应用并使用您的应用程序公共API密钥,您可以在http://www.tumblr.com/oauth/apps上进行注册。 - Adam Azad
1个回答

6

在 @Adam 的回答的部分帮助下,我的最终解决方案是在这里创建一个应用程序 tumblr.com/oauth/apps,然后在我的 API 请求中使用我的消费者密钥,如下所示:

http://api.tumblr.com/v2/blog/<MY_BLOG>/posts/photo?api_key=<MY_KEY>&jsonp=callback" 并使用 JSONP 处理 callback 函数。

我的最终代码:

var apiKey = "gdfgdfgdfgdsfgdfhgsdfghsfdh";
var requestURL = "http://api.tumblr.com/v2/blog/dfsdf.tumblr.com/posts/photo?api_key=" + apiKey + "&jsonp=callback";

var script = document.createElement('script');
script.src = requestURL;

document.getElementsByTagName('head')[0].appendChild(script);
// or document.head.appendChild(script) in modern browsers

window.callback = function (data) {
    console.log(data);
}

是的,这就是正确的方法!感谢您发布您的解决方案。 - mikedidthis
这非常强大...现在你可以在主题中访问整个API,而不仅仅是主题API提供的内容。能否透露一下你打算用它做什么? - kai
@Cyrin,没错,我使用这种技术为RayBan的品牌Persol构建了一个主题http://persol.tumblr.com/。 - Alex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接