英文:
Get Twitter Account followers using API and Google Apps Script
问题
我正在尝试使用Google Apps Script获取特定Twitter用户的Twitter粉丝列表。我找到了许多来源:
但无法创建符合我的要求的链接。我有以下代码片段:
function getFollowers(){
var twitterKeys = {
TWITTER_CONSUMER_KEY: '###',
TWITTER_CONSUMER_SECRET: '###',
TWITTER_ACCESS_TOKEN: '###',
TWITTER_ACCESS_SECRET: '###',
};
var username = '示例名称';
var props = PropertiesService.getUserProperties();
props.setProperties(twitterKeys);
var url = `https://api.twitter.com/2/users/${username}/followers`
var options = {
method: 'GET',
headers: {
api:twitterKeys
},
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());
console.log(json)
}
运行时,它显示以下输出:
{ "title": "未授权", "type": "about:blank", "status": 401,
"detail": "未授权" }
非常感谢您提供解决此问题并获取特定Twitter用户的Twitter粉丝的任何建议。
英文:
I am trying to get the list of Twitter followers of a specific Twitter user using Google Apps Script. I found many sources:
but unable to create a link with my requirement. I have following code snippet:
function getFollowers(){
var twitterKeys = {
TWITTER_CONSUMER_KEY: '###',
TWITTER_CONSUMER_SECRET: '###',
TWITTER_ACCESS_TOKEN: '###',
TWITTER_ACCESS_SECRET: '###',
};
var username = 'Example name'
var props = PropertiesService.getUserProperties();
props.setProperties(twitterKeys);
var url = `https://api.twitter.com/2/users/${username}/followers`
var options = {
method: 'GET',
headers: {
api:twitterKeys
},
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());
console.log(json)
}
On runs, it shows following output:
> { "title": "Unauthorized", "type": "about:blank", "status": 401,
> "detail": "Unauthorized" }
Any advice would be much appreciated to resolve it and get Twitter followers of a specific Twitter user.
答案1
得分: 1
Twitter API 相对较近的时候从版本 1.1 升级到了版本 2,因此你在网上找到的很多示例可能已经过时。
根据 URL,看起来你正在使用 API v2,而且似乎在免费版本的 API v2 上只能创建/删除推文以及获取有关自己帐户的信息。你可以查看你可以访问的端点(请展开卡片以查看完整端点)此处。
如果你选择付费层级(如果你只需要个人用户指标,基本层级可能足够了),并访问你有授权的端点,以下代码应该适用于你:
function getFollowers(){
var username = '示例名称'
var url = `https://api.twitter.com/2/users/by/${username}`
var options = {
method: 'GET',
headers: {
'Authorization': `Bearer: ${BEARER_TOKEN}` // 从 Twitter 开发门户获取此令牌
},
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());
var userData = json.data;
console.log(userData)
}
(我尚未尝试过这段代码,只是从这里和这里的文档中拼凑而成。)
英文:
The Twitter API relatively recently upgraded from version 1.1 to version 2, so a lot of the examples you'll find online are probably outdated.
Based on the URL, looks like you are using API v2, and it looks like the only thing you can do on the free version of API v2 is create/delete tweets and get information about your own account. You can see the endpoints that you have access to across tiers here (expand the cards to see the full endpoint).
If you do opt into the paid tiers (Basic is probably sufficient if all you need is individual user metrics), and access an endpoint that you are authorized to, the following code should work for you:
function getFollowers(){
var username = 'Example name'
var url = `https://api.twitter.com/2/users/by/${username}`
var options = {
method: 'GET',
headers: {
'Authorization': `Bearer: ${BEARER_TOKEN}` // Get this token from the twitter dev portal
},
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());
var userData = json.data;
console.log(userData)
}
(I haven't tried this code, just cobbled it together from the docs here and here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论