Android Instagram API返回一个代码,但在请求访问令牌时,该代码无效

Android Instagram API返回一个代码,但在请求访问令牌时,该代码无效,android,ruby,oauth,instagram,Android,Ruby,Oauth,Instagram,使用Instagram rubygem和sinatra,我能够通过oauth从一个小型AngularJS应用程序和Instagram API成功进行身份验证。以下是我的sinatra应用程序中的相关oauth路线: get '/oauth/connect' do logger.info "going here " + (Instagram.authorize_url :redirect_uri => CALLBACK) redirect Instagram.authoriz

使用Instagram rubygem和sinatra,我能够通过oauth从一个小型AngularJS应用程序和Instagram API成功进行身份验证。以下是我的sinatra应用程序中的相关oauth路线:

get '/oauth/connect' do
    logger.info "going here " + (Instagram.authorize_url :redirect_uri => CALLBACK)
    redirect Instagram.authorize_url :redirect_uri => CALLBACK
end

get "/oauth/callback" do
    logger.info params[:code]
    response = Instagram.get_access_token params[:code], :redirect_uri => CALLBACK
    session[:access_token] = response.access_token

    users = DB[:users]
    current_user = users[:instagram_id => response.user.id.to_i]

    if current_user.nil?
        new_user = response.user
        users.insert :instagram_id => new_user.id.to_i, 
                             :username => new_user.username, 
                             :profile_picture => new_user.profile_picture,
                             :created_at => DateTime.now

        current_user = users[:instagram_id => new_user.id]
    end
    session[:current_user] = current_user
    redirect "/app"
end
但是,当尝试使用相同的路径(获取oauth/connect,重定向到Instagram,然后重定向回oauth/callback)在Android应用程序的webview中进行身份验证时,我遇到以下错误:

Instagram::BadRequest - POST https://api.instagram.com/oauth/access_token/: 400: OAuthException: No matching code found.:
我已验证我使用的代码与Instagram返回的代码相同,重定向uri与我在Instagram注册的相同

注意,Oauth在我使用web界面时可以工作,但不能通过Android webview

我读过很多其他人看到这种行为的帖子,但提供的解决方案对我不起作用:

  • Instagram可以禁用你的应用程序,因为它认为它行为不端。不,因为oauth通过我的web界面工作,仍然使用相同的客户机ID和密码以及相同的路由

  • 您使用的重定向uri与Instagram中列出的不同。我检查了又检查了这个,看起来很好


  • 下一步我在哪里调试才能找到问题的根源?

    我知道我的答案有点晚了,但我将它发布在这里,这样其他人(希望)就不必像我那样努力搜索了

    看起来您正在使用显式身份验证。我们在使用显式流时遇到了这个问题

    问题是:在您将代码发送到Instagram(第3步)的瞬间,该代码就会过期。但是,Instagram在至少5分钟内不会向您发送新代码(步骤1和步骤2)(它会重新发送旧代码)


    确保您没有使用相同的代码发送多个请求(Android WebView倾向于任意发送多个请求)。或者,最好在服务器上缓存代码->Instagram\u响应映射

    是的,Android WebView onPageFinished回调启动了两次,我发起了两次身份验证调用。请注意:@user2028,对不起,我已经有近5年没有接触Instagram API了。