特定のツイートをリツイートしたアカウントの情報を取得する
Oauth認証
スプレットシートのID確認やAPIのCallbackURLの設定などについては前回の記事を参照してください。前回の記事で認証が完了した前提で下記コードを実行しています
参照:【GAS】TwitterAPIを使ってツイート検索結果をスプレットシートに書き出す
スクリプト
// 特定のツイートをリツイートしたアカウント情報IDを取得する
function get_tweet_status(){
const service = getTwitterService();
tweet_id = '1606674852882305024'
if (service.hasAccess()) {
var url = "https://api.twitter.com/1.1/statuses/retweeters/ids.json?id=" + tweet_id + "&stringify_ids=true";
console.log(url)
const response = service.fetch(url, {method: 'get'});
const result = JSON.parse(response.getContentText());
var code = response.getResponseCode();
console.log(code)
console.log(result)
return response
}
else{
Logger.log("認証エラー");
}
}
参考:https://syncer.jp/Web/API/Twitter/REST_API/GET/statuses/oembed/
特定のツイートにいいねしたユーザーを取得
認証方法の注意点
いいねを行ったユーザーの取得はV2のエンドポイントを使用する必要がある点に注意が必要です
v1.1用の認証したトークンで叩くと以下のようなエラーになります
Exception: Request failed for https://api.twitter.com returned code 403. Truncated server response: {“client_id”:”16884419″,”detail”:”When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter develop… (use muteHttpExceptions option to examine full response)
TwitterAPIV2での認証方法は以下の記事を参考にしてください
関連記事:【GAS】TwitterAPIのV2でツイートを定期的に自動投稿する
参照:https://officeforest.org/wp/2021/05/22/gas_twitter_v2/#GAS-2
下記コードが上記記事で認証が完了している前提で動作するスクリプトになります
// 特定のツイートをいいねしたアカウント情報IDを取得する
function get_tweet_status(){
const service = checkOAuth(appname);
tweet_id = '1606674852882305024'
if (service.hasAccess()) {
var url = "https://api.twitter.com/2/tweets/"+ tweet_id + "/liking_users";
console.log(url)
const response = service.fetch(url, {method: 'get'});
const result = JSON.parse(response.getContentText());
var code = response.getResponseCode();
console.log(code)
console.log(result)
return response } else{ Logger.log("認証エラー"); } }
関連記事:【SNS自動化】2か月で1000フォロワー稼げるTwitterフォロー管理自動化プログラム
関連記事:【GAS】TwitterAPIを使ってアカウントのユーザー情報を取得する
関連記事:【GAS】TwitterAPIのV2でツイートを定期的に自動投稿する

コメント
[…] 関連記事:【GAS】TwitterAPIで特定のツイートにリツイート・いいねしたユーザーを取得する […]