缺少 client_id 或 access_token URL 参数。 (InstagramApi::BadRequest)

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

我的代码 return InstagramApi::BadRequest 我的客户端 ID 和访问令牌他没问题!我尝试通过网站和正常流程生成令牌!

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)

您的代码示例有点乱:

  1. 您定义了一个方法 login_insta 来配置您的凭据,但您从未调用该方法来 运行 配置。

  2. 如果您确实调用了 login_insta 它仍然无法正常工作,因为对 client.new 的调用在您的代码示例中没有解释并且违反了 the instructions from the gem author 关于如何配置凭据。

在这种情况下,将您的代码减少到 minimal, complete, verifiable example 可能会解决问题:

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')

我无法独立验证这是否解决了问题,因为我没有 Instagram API 访问权限,但根据 gem 自述文件中的说明,这应该是解决方案。

当您将此解决方案合并到您的代码中时,只需确保对 InstagramApi.config 的调用仅 运行 一次,然后再进行任何 API 调用。它不需要包装在方法调用中。 (而且不应该,因为它只需要 运行 一次,而在 运行 之后它将在 Ruby 进程的生命周期内保持有效)