2013-06-24 52 views
5

我想爲rails 3.2.13應用程序設置Stripe Connect。我已指示用戶條紋與從條接收回授權代碼:條紋連接 - 檢索訪問令牌

HTTP/1.1 302 Found 
Location: http://localhost:3000/yourshop/stripe?scope=read_write&state=1234&code=AUTHORIZATION_CODE 

下一步涉及使POST請求經由access_token_url以接收所述的access_token,每條紋文檔:

curl -X POST https://connect.stripe.com/oauth/token \ 
    -d client_secret=sk_test_code \ 
    -d code=AUTHORIZATION_CODE \ 
    -d grant_type=authorization_code 

我沒有任何curl的經驗是一個Rails應用程序,我無法找到Stripe API中的任何東西,看起來像這個POST請求包含在Stripe Gem中:

Gem file:

gem 'omniauth-stripe-connect' 
gem 'stripe' 

型號

def save_with_stripe_account 
    code = self.stripe_code 
    customer = curl -X POST https://connect.stripe.com/oauth/token \ 
    -d "client_secret=ENV['STRIPE_SECRET_KEY']" \ 
    -d "code=code" \ 
    -d "grant_type=authorization_code" 
    raise customer.inspect 
end 

錯誤:

syntax error, unexpected tCONSTANT, expecting keyword_do or '{' or '(' 

不知道如果只是誤格式化爲捲曲的軌道,或者如果需要使用別的東西。

+1

我不知道條紋API,但只是讓你知道:[捲曲](http://curl.haxx.se/docs/manpage.html)是一個命令行工具,所以你**不能**在你的Ruby代碼中複製粘貼爲一個curl命令。至少你需要在Ruby中執行[系統調用](http://martinhauser.com/wiki/Ruby_System_Calls)。在實踐中,我建議您使用Stripe gem本身(可能包括您需要的),或者 - 如果您需要自己執行自定義HTTP請求,請使用Ruby HTTP庫,例如[curb](https://rubygems.org/gems/curb),它使用libcurl或'Net :: HTTP'官方庫。 – deltheil

回答

3

我得到它工作過,可能不是最漂亮的方式:

我學到的第一件事是捲曲可以在裏面工作,如果你把周圍的捲曲代碼反引號軌道。這將返回只需格式化的JSON。我結束了在我的模型如下:

customer = ActiveSupport::JSON.decode(`curl -X POST https://connect.stripe.com/oauth/token -d client_secret=#{ENV['STRIPE_SECRET_KEY']} -d code=#{self.stripe_code} -d grant_type=authorization_code`) 

我然後我能提取客戶的散列數據如「的access_token」:

customer['access_token'] 
+0

其運作良好。thanx –

5

試試這幾招:

在您的Rails應用程序中安裝HTTParty gem

response = HTTParty.post(「https://connect.stripe.com/oauth/token」,:query => {client_secret:「#{ENV ['STRIPE_SECRET_KEY']}」,code:response_碼,grant_type:「authorization_code」})

那麼你應該能夠訪問響應[「ACCESS_TOKEN」],而不必使用周圍的捲曲代碼反引號。它爲我工作。

+0

在打印響應時,我獲得了所有的值,但是我無法將access_token作爲響應['access_token']。 – logesh

+0

這爲我工作,謝謝 – Tom

0

如果您使用的是omniauth stripe connect strategy,那麼它會得到照顧,並放入您的omniauth回調控制器中的request.env["omniauth.auth"]

不需要做任何HTTParty的omniauth條連接戰略需要照顧所有的事情。美女!