2016-01-18 23 views
0

我們通過3腳Oauth2實現將我們的Ruby on Rails 4應用程序與Infusionsoft的XML-RPC API集成在一起。如何使用紅寶石從Infusionsoft的Oauth獲取訪問令牌?

(文檔在這裏 - https://developer.infusionsoft.com/docs/xml-rpc/#authentication-request-permission

我們建立了一個重定向操作給用戶發送到與我們的應用程序的ID的Infusionsoft應用認證頁面。

def connect_inf 
    redirect_to "https://signin.infusionsoft.com/app/oauth/authorize?client_id=XXXXXXX&redirect_uri=https://testappurl.herokuapp.com/websites/1/connect_inf/callback/&response_type=code&scope=full" 
end 

然後,用戶進行身份驗證之後,用戶被髮送回我們的尋找這樣的URL回調動作......

https://testappurl.herokuapp.com/websites/1/connect_inf/callback?scope=full|theirappdomain.infusionsoft.com&code=XXXXXXX

我們得到的代碼,並使用RESTClient實現到將其發送回Infusionsoft以請求訪問令牌,並按照它們在文檔中指定的順序使用參數。

def get_inf 
    response = RestClient.post 'https://api.infusionsoft.com/token', :client_id => 'XXXXXXXXXXXXXX', :client_secret => 'XXXXXXX', :code => params[:code], :grant_type => 'authorization_code', :redirect_uri => 'https://testappurl.herokuapp.com/websites/1/connect_inf/callback/', :accept => 'text/xml' 
end 

這將返回RestClient::BadRequest: 400 Bad Request

我想這已經是與參數是如何被傳遞。

+0

錯誤的請求通常意味着參數錯誤。 「params」的內容是什麼? API真的處理XML嗎? –

回答

0

有關使用Infusionsoft API的ruby開發人員的參考,這是由使用動態重定向和回調URL導致的許多問題引起的。 Infusionsoft將註冊您的應用程序時提供的一個回調URL列入白名單。

我能夠通過使用Infusionsoft的OmniAuth策略並使用OmniAuth訪問令牌來解決此問題。

https://github.com/L1h3r/omniauth-infusionsoft

我們指出默認omniauth回調路徑「/認證/ infusionsoft /回調」,在我們的路線控制器動作。

get '/auth/:provider/callback', to: 'websites#get_inf' 

,我們能夠訪問令牌是這樣的...

def get_inf 
    access_token = env["omniauth.auth"]['credentials']['token'] 
    refresh_token = env["omniauth.auth"]['credentials']['refresh_token'] 
    expires_at = env["omniauth.auth"]['credentials']['expires_at'] 
end 

然後,您可以這些標記保存到例如用戶對象。

相關問題