Skip to content

Instantly share code, notes, and snippets.

@vaiorabbit
Created April 8, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaiorabbit/7562f244c4fc49d90a17cf0371ad5413 to your computer and use it in GitHub Desktop.
Save vaiorabbit/7562f244c4fc49d90a17cf0371ad5413 to your computer and use it in GitHub Desktop.
Twitter 'POST favorites/destroy' API
#! /usr/bin/env ruby
# coding: utf-8
# Usage:
# - Set environment variables 'TWITTER_CONSUMER_KEY', 'TWITTER_CONSUMER_SECRET', 'TWITTER_ACCESS_TOKEN' and 'TWITTER_ACCESS_TOKEN_SECRET' beforehand
# - $ ruby destroy_like.rb [tweet id]
# Ref.: https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-favorites-destroy
require 'json'
require 'oauth'
consumer_key = ENV['TWITTER_CONSUMER_KEY']
consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
access_token = ENV['TWITTER_ACCESS_TOKEN']
access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET']
consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(consumer, access_token, access_token_secret)
destroy_id=ARGV[0]
response = endpoint.delete("https://api.twitter.com/1.1/favorites/destroy.json?id=#{destroy_id}")
json = JSON.parse(response.body)
File.open("result.json", "w") do |f|
f.write(JSON.pretty_generate(json))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment