Stripe Connect - retrieving access token

て烟熏妆下的殇ゞ 提交于 2019-12-21 05:11:34

问题


I am trying to set up Stripe Connect for a rails 3.2.13 app. I have directed the user to Stripe and received back the authorization code from Stripe:

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

The next step involves making a POST request to receive the access_token via the access_token_url, per Stripe documentation:

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

I don't have any experience with curl is a rails app and I couldn't find anything in the Stripe API that seems like this POST request is include in the Stripe Gem:

Gem file:

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

Model

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

Error:

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

Not sure if just the wrong formatting for curl in rails or if need to use something else.


回答1:


Try this recipe:

install the HTTParty gem in your rails application

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

Then you should be able to access response['access_token'] without having to use backticks around the curl code. It worked for me.




回答2:


I got it too work, probably not the prettiest way:

The first thing I learned is that curl can work inside rails if you put backticks around the curl code. This will return JSON which just needs to be formatted. I ended up with the following in my model:

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`)

I then am able to extract the data from the customer hash such as "access_token":

customer['access_token']



回答3:


If you are using the omniauth stripe connect strategy then it gets taken care of for you and is placed in the request.env["omniauth.auth"] in your omniauth callbacks controller.

No need to do any HTTParty, the omniauth stripe connect strategy takes care of all the things. Beauty!



来源:https://stackoverflow.com/questions/17284360/stripe-connect-retrieving-access-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!