从自定义主题中调用Tumblr API

Call Tumblr API from within a custom theme

本文关键字:调用 Tumblr API 自定义      更新时间:2023-09-26

我正在为需要使用该平台的客户编写自定义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();

部分得益于@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);
}