Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Steve Christie
Steve Christie
9,509 Points

Stripe Connect - curl issue

I'm working on a marketplace app and I want to use Stripe Connect but I'm stuck on the following step in the process:

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

I can get the code to work in the terminal but not in irb or the rails console. Is there a formatting for rails, or a different way to handle the POST rerequest.

Per the docs, it should respond with:

{
  "token_type": "bearer",
  "stripe_publishable_key": PUBLISHABLE_KEY,
  "scope": "read_write",
  "livemode": "false",
  "stripe_user_id": USER_ID,
  "refresh_token": REFRESH_TOKEN,
  "access_token": ACCESS_TOKEN
}

2 Answers

Steve Christie
Steve Christie
9,509 Points

Here how I ended solving the issue. Curl works in rails but need to surround the curl command with backticks. This returns json, just like if used the curb gem.

Here's what I ended up using:

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

Then I could extract the stripe values using for each field I needed to create to create the client id for Stripe Connect.

customer['access_token']
Brandon Flade
Brandon Flade
2,769 Points

That's great news that you got it working! What lead you to use the backtick in the decode method? Did you dig into ActiveSupport or pick it up from another site?

Brandon Flade
Brandon Flade
2,769 Points

I probably shouldn't answer this because I know next to nothing about Rails, but on the off chance I do happen to guess correctly... Have you required any kind of libcurl gem in your project? i would think that the formatting of the request would be dependent on the gem you chose to use.

Steve Christie
Steve Christie
9,509 Points

Initially no, then I tried curb. It returned a hash in a format I didn't recognize. Although later found it out to be in json.

Steve Christie
Steve Christie
9,509 Points

The backtick I picked up from a friend that knows ruby, but not rails.

The ActiveSupport::JSON.decode piece I picked up from Stripe customer service. I wish I had thought of contacting them first. The Stripe team seems very willing to help developers get up and running with Stripe. The Stripe documentation is really good for the regular Stripe product. It's a little lacking on Stripe Connect(at least with my skill level) but they helped where I was confused.