简体   繁体   中英

sending POST request to API endpoint to receive stripe_user_id

After a user signs up for a Stripe account with my app, they are redirected to my localhost, and an authorization_code is added to the url. I am then supposed to make a POST request with my client_secret and authorization_code to their API endpoint. The code provided by the docs says to do something like this:

curl https://connect.stripe.com/oauth/token \
 -d client_secret=blahblah \
 -d code="{AUTHORIZATION_CODE}" \
 -d grant_type=authorization_code

But...where do I do this, exactly? In the controller? Like this?

def post_to_endpoint(endpoint)
 require 'json'

 begin
  uri = URI.parse(endpoint)

  post_params = {
    client_secret: "client_secret",
    code: "{AUTHORIZATION_CODE}",
    grant_type: authorization_code
  }

  req = Net::HTTP::Post.new(uri.path)
  req.body = JSON.generate(post_params)
  req["Content-Type"] = "application/json"
  http = Net::HTTP.new(uri.host, uri.port)
  response = http.start { |htt| htt.request(req) }
 rescue => e
  puts "failed #{e}"
 end
end

The user gets redirected to a GET route on my app at the end of step 3, and then my app is supposed to make a POST to the Stripe endpoint. Do I need to set up a route? Can I have this action happen in the background?

The call to /oauth/token is something you make on the backend/controller to get an authorization token from Stripe that you'll need in order to make call on behalf of the connected account. Your user doesn't need to be involved at all with that call once they've authorized your platform to connect to their account.

Since you're using Ruby, I'd recommend using stripe-ruby (the official library). That has built-in methods for using Oauth with Stripe Connect.

solution! write it as a module. it doesn't need to be persisted to the db. also helpful was using the stripe ruby library

i wrote a stripe_oauth.rb that looks like this:

module StripeOauth
 def self.connect(code)
  Stripe.api_key = ENV["STRIPE_SECRET_KEY"]
  Stripe::OAuth.token( {code: code, grant_type: "authorization_code" } )
 end
end

and then i called that from the controller action Stripe redirected me to:

def welcome
 StripeOauth.connect(params[:code])
end

params[:code] is sent along as part of the url, so

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM