2015-02-11 36 views
0

使用Instagram ruby​​gem和sinatra我能夠通過oauth從小型AngularJS應用程序成功進行身份驗證並使用Instagram API。下面是我的末日應用程序相關的OAuth路線:Instagram API返回一個代碼,但是當請求訪問令牌時,說相同的代碼是無效的

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 

然而,試圖使用相同的路線在一個Android應用程序從網頁視圖中進行身份驗證時(取得OAuth /連接,重定向到Instagram的,然後重定向回的OAuth /回調),我收到以下錯誤:

Instagram::BadRequest - POST https://api.instagram.com/oauth/access_token/: 400: OAuthException: No matching code found.: 

我覈實,我使用Instagram的距離,而且REDIRECT_URI返回相同的代碼是一樣的我與Instagram的註冊。

請注意,Oauth在我使用我的Web界面時工作,但不通過Android webview。

我讀了一些,而其他人都看到了這種行爲的帖子,但所提供的解決方案並沒有爲我工作:

  1. ,因爲它認爲這是行爲不端的Instagram可以禁用應用程序。不,因爲oauth通過我的web界面工作,仍然使用相同的客戶端ID和祕密路由。

  2. 您沒有使用與Instagram中列出的相同的redirect_uri。我已經檢查並重新檢查了這一點,它看起來很好。

我在哪裏可以調試下一個問題到底?

回答

0

我知道我的答案有點晚了,但我在這裏發佈,所以其他人(希望)不會像我那樣努力搜索。

它看起來像你使用顯式驗證。我們使用顯式流程來解決這個問題。

問題出在這裏:在您將代碼發送給Instagram(步驟3)之後,該代碼過期。但是,Instagram不會向您發送新代碼(步驟1 & 2)至少5分鐘(重新發布舊代碼)。

確保您沒有使用相同的代碼發送多個請求(Android WebView傾向於隨意發送多個請求)。或者,更好的辦法是在服務器上緩存代碼 - > Instagram_response映射。

+0

是的,Android WebView onPageFinished回調被觸發兩次,我正在發起兩個身份驗證呼叫。 – cacciatc 2015-02-19 18:13:18

相關問題