我对 JavaScript 比较陌生,因此决定尝试通过 API 访问 Twitter 以获取我的 5 条最新推文,但是我遇到了困难,我将不胜感激。
这是 tweets.js
代码。我仔细查看了 API 来形成这个,但不确定它是否正确。
tweets = {
loaddata: function() {
$.ajax({
url: 'https://api.twitter.com/1.1/statuses/user_timeline.json',
type: 'GET',
dataType: 'json',
data: {
screen_name: 'techybox',
include_rts: false,
count: 5,
include_entities: true
},
success: function(data, text) {
var html = '<li class="tweet">TWEET</li>';
$('#timeline').append(html.replace('TWEET', tweets(data.text)));
}
});
};
}
$(document).ready(function() {
tweets.loaddata();
});
理论上应该加载 5 条最新的推文并将它们附加到带有 id 时间线的 UL。我已经看到您可能需要 OAuth 来执行此操作,但我不确定我将如何实现它?我对么? Twitter 的旧 API 似乎没有这个但 1.1 也能工作。可能已经改变了吗?
最后这是我的 html 页面,它只包含 UL:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tweets</title>
</head>
<body>
<h1>Tweets</h1>
<ul id='timeline'></ul>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="tweets.js"></script>
</body>
</html>
最佳答案
Twitter 的更新 API 需要 oAuth,这将要求您编写一个由 javascript 使用的服务器端组件。
所以并不是说你的方法有问题。 Twitter 只是不希望您再以这种方式获取信息。
请注意,如果您在浏览器中访问 API,您只会收到一个错误。 https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=techybox&count=3
这里有一篇关于该主题的非常好的帖子:Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
关于javascript - 使用 JavaScript 和 Twitter API 加载推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36484816/