Search code examples
rubyapirubygemsaccess-tokeninstagram-api

Missing client_id or access_token URL parameter. (InstagramApi::BadRequest)


my code return InstagramApi::BadRequest my client id and acces token his all right!i tri to générate token with web site and the normal process!

require 'instagram_api_client'
require 'dotenv'

Dotenv.load
def login_insta

    client.new = InstagramApi.config do |config|
        config.access_token = ENV["INSTA_ACCESS_TOKEN"]
        config.client_id = ENV["INSTA_CLIENT_ID"]
        config.client_secret = ENV["INSTA_CLIENT_SECRET"]
    end
    return client
end

def auto_follow_test
    #ary = Array.new

      search_user = InstagramApi.user.search('75')

    #ary << search


   # puts ary[0]
   return search_user
end

auto_follow_test

Traceback (most recent call last):
        4: from lib/app.rb:28:in `<main>'
        3: from lib/app.rb:19:in `auto_follow_test'
        2: from /home/mhd/.rvm/gems/ruby-2.5.1/gems/instagram_api_client-0.2.1/lib/instagram_api/common.rb:10:in `search'
        1: from /home/mhd/.rvm/gems/ruby-2.5.1/gems/instagram_api_client-0.2.1/lib/instagram_api/client.rb:37:in `make_request'
/home/mhd/.rvm/gems/ruby-2.5.1/gems/instagram_api_client-0.2.1/lib/instagram_api/client.rb:53:in `parse_failed': Missing client_id or access_token URL parameter. (InstagramApi::BadRequest)

Solution

  • Your code sample is kind of a mess:

    1. You define a method login_insta to configure your credentials but you never call the method to run the configuration.

    2. If you did call login_insta it still wouldn't work properly because of the call to client.new which is not explained in your code sample and contravenes the instructions from the gem author on how to configure the credentials.

    This is a case where reducing your code to the minimal, complete, verifiable example will probably solve the issue:

    require 'instagram_api_client'
    require 'dotenv'
    
    Dotenv.load
    
    # Set the global configuration for the gem per the instructions at:
    # https://github.com/agilie/instagram_api_gem#usage
    InstagramApi.config do |config|
      config.access_token = ENV["INSTA_ACCESS_TOKEN"]
      config.client_id = ENV["INSTA_CLIENT_ID"]
      config.client_secret = ENV["INSTA_CLIENT_SECRET"]
    end
    
    InstagramApi.user.search('75')
    

    I can't independently verify that this resolves the issue because I do not have Instagram API access, but given the instructions in the README for the gem this should be the solution.

    When you go to incorporate this solution into your code, just make sure that the call to InstagramApi.config is run one time only and before you make any API calls. It doesn't need to be wrapped in a method call. (and shouldn't because it only ever needs to be run one time, and after it is run once it will remain in effect for the lifetime of the Ruby process)