OAuth2对看门人的无效授权

tkqqtvp1  于 2022-10-15  发布在  Ruby
关注(0)|答案(1)|浏览(202)

当我尝试从我的API获取资源时,我得到了一个无效的授权。
invalid_grant: The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request,or was issued to another client.
这可能是什么问题呢?

require 'sinatra'
  require 'httparty'
  require 'oauth2'

  enable :sessions

  configure do
    set :callback, 'http://localhost:4567/auth/doorkeeper/callback'
    set :app_id, '7cd423ef68bdc938372d8e290475ea5a85feb550004b77481f99ff0dcba133b1'
    set :app_secret, '46bafd07b5a485240d7fdaedbdbac2a675afabe470f5433b13395f8dcff4e473'
  end

  get '/' do
    oauth_client.auth_code.authorize_url(redirect_uri: settings.callback)
  end

  get '/auth/doorkeeper/callback' do
    if params[:code].nil?
      redirect to('/')
    end

    session[:code] = params[:code]

    redirect to('/get_token')
  end

  get '/get_token' do
    access = oauth_client.auth_code.get_token session[:code], :redirect_uri => settings.callback
    # session[:code]
    access.get('/api/v1/me')
  end

  def oauth_client
    @oauth_client ||= OAuth2::Client.new(settings.app_id, settings.app_secret, site: "http://localhost:3000")
  end
b4lqfgs4

b4lqfgs41#

如果授权已经第一次生效,那么突然出现错误,这只是意味着令牌已经过期或被吊销。
当您以前收到有效令牌但错误再次出现时,只需转到您的Documents文件夹,删除.Credentials文件夹(您以前的令牌所在的文件夹),然后再次运行应用程序以接收新令牌。

相关问题