Search code examples
rubyapitwittertwitter-oauth

want to get number of tweets of a keyword per a day using Keyword Insights of Twitter api


I want to get number of tweets of a keyword per a day.

I want to use Keyword Insights of Twitter api using Ruby. : https://developer.twitter.com/en/docs/ads/audiences/api-reference/keyword-insights

I have tryed to write code. But I got such following error.

`<main>': undefined method `keywords' for #<Twitter::REST::Client:0x00000000062837b8> (NoMethodError)

The Ruby code is following.

require "twitter"
require 'yaml'

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
Encoding.default_external = 'UTF-8'

  @client = Twitter::REST::Client.new do |config|
    config.consumer_key        = 'xxxx'
    config.consumer_secret     = 'xxxx'
    config.access_token        = 'xxxx-xxxx'
    config.access_token_secret = 'xxxx'
  end

# @client.update('hoge')
# => succes.
@client.insights.keywords.search("DAY","Ruby")

What should I do?


Solution

  • As the other poster mentioned, there's no Ads API support in the twitter gem, but Twitter has a twitter-ads gem you can obtain on Github. Note that you will also need to have your app tokens whitelisted for access to the Ads API. You can apply for that via Twitter's developer site.