【问题标题】:Stripe Connect - retrieving access tokenStripe Connect - 检索访问令牌
【发布时间】:2013-06-21 11:37:24
【问题描述】:

我正在尝试为 rails 3.2.13 应用程序设置 Stripe Connect。我已将用户定向到 Stripe 并从 Stripe 收到了授权码:

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

根据 Stripe 文档,下一步涉及发出 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 '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 '('

不确定是否只是rails中curl的格式错误,或者是否需要使用其他内容。

【问题讨论】:

  • 我不知道 Stripe API,但你知道:curl 是一个命令行工具,因此你不能像 curl 一样复制粘贴Ruby 代码中的命令。至少您需要执行system call in Ruby。在实践中,我建议您使用 Stripe gem 本身(可能包括您需要的),或者 - 如果您需要自己执行自定义 HTTP 请求,请使用 Ruby HTTP 库,例如使用 libcurl 或 Net::HTTP 官方的 curb图书馆。

标签: ruby-on-rails curl stripe-payments


【解决方案1】:

试试这个食谱:

在您的 Rails 应用程序中安装 HTTParty gem

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

那么您应该能够访问 response['access_token'] 而无需在 curl 代码周围使用反引号。它对我有用。

【讨论】:

  • 在打印响应时,我得到了所有值,但我无法将 access_token 作为响应 ['access_token']。
【解决方案2】:

我也搞定了,可能不是最漂亮的方式:

我学到的第一件事是,如果你在 curl 代码周围加上反引号,curl 可以在 rails 内工作。这将返回只需要格式化的 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']

【讨论】:

    【解决方案3】:

    如果您使用的是omniauth stripe connect strategy,那么它会为您处理好并放置在您的omniauth 回调控制器中的request.env["omniauth.auth"] 中。

    不需要做任何 HTTParty,omniauth 条带连接策略会处理所有事情。美女!

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2021-09-05
      • 2015-09-08
      • 2020-10-12
      • 2015-07-13
      • 1970-01-01
      相关资源
      最近更新 更多