微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将手动添加新用户到 Constant Contact 的 Rails 控制器重构为后台作业?

如何解决如何将手动添加新用户到 Constant Contact 的 Rails 控制器重构为后台作业?

我已在我的 Rails 应用程序中成功构建了一个控制器,该控制器连接到 Constant Contact v3 API 并将当前用户的电子邮件地址添加到 Constant Contact 中的列表中。但是,这仅在用户登录并通过单击浏览器中的链接触发该过程时才有效。

我不知道如何重构它(例如,在用户模型中使用 after_create 回调)或后台作业。当新用户注册时,我更希望这发生在幕后。我正在关注 Constant Contact API 文档中的 OAuth2.0 Server Flow

这是它目前的工作方式:

  1. 用户注册并访问 constant_contact#index 页面 (/constant-contact),他们在此点击链接将我的 Constant Contact API 密钥发送给 Constant Contact。
  2. Constant Contact 会向我在 constant_contact#callback (/constant-contact/callback) 上设置的重定向网址提供一个授权代码
  3. 在我的 constant_contact#callback 操作中,我从参数中获取授权代码并使用它来构建一个 JSON 帖子,然后将授权代码发送回 Constant Contact API,以便 Constant Contact 知道它正在与正确的域通话.
  4. Constant Contact 然后在我的 /constant-contact/callback url 返回一个令牌进行响应,然后我可以使用这个令牌与 Constant Contact API 进行交互。

这是我用来完成所有这些操作的控制器(gist version,如果它更容易阅读):

require 'oauth2'

class ConstantContactController < ApplicationController

  before_action :set_constant_contact

  def index

    # Build the authorization url to request the authorization code

    auth_request_base_url = "https://api.cc.email/v3/idfed"
    auth_url = auth_request_base_url + "?client_id=" + @client_id + "&scope=account_read+contact_data&response_type=code" + "&redirect_uri=" + @redirect_uri

    # Send the authorization url by clicking link. Could also use HTTParty.get(auth_url)

    @send_auth_url = auth_url

  end

  def callback

    # Receive the initial response from Constant Contact at /constant-contact

    auth_code = params[:code]
    @auth_code = auth_code

    # Now build the authorization code url that we'll use to request a token

    auth_code_base_url = "https://idfed.constantcontact.com/as/token.oauth2"
    auth_code_request_url = auth_code_base_url + '?code=' + auth_code + '&redirect_uri=' + @redirect_uri + '&grant_type=authorization_code&scope=contact_data'

    # Build the token request with the authorization code request url
    # First,set up the Header (make string of "API_KEY:SECRET")

    auth = @client_id + ':' + @secret

    # Base64 encode it

    credentials = Base64.strict_encode64(auth)

    # Build and set the Authorization header to use the encoded credentials

    authorization = "Basic " + credentials

    # Send the request for a token

    token_response = HTTParty.post("#{auth_code_request_url}",:headers => {
                 "Content-Type" => 'application/json',"Authorization" => "#{authorization}"
                }
    )

    # Parse the token response

    token_body = JSON.parse(token_response.body)
    token = token_body["access_token"]
    token_auth = "Bearer " + token

    @account_summary = HTTParty.get("https://api.cc.email/v3/account/summary",:headers => {
                 "Accept" => 'application/json',"Authorization" => "#{token_auth}"
                }
    )

    # Add the current user's email to Constant Contact list

    list_uuid = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"

    @successful_response = HTTParty.post("https://api.cc.email/v3/contacts/sign_up_form",:headers => {
                 'Content-Type' => 'application/json',"Authorization" => "#{token_auth}",},:body => {
        "email_address": "#{current_user.email}","list_memberships": [ "#{list_uuid}" ],}.to_json
    )

  end

  private

  def set_constant_contact
    # set the constant contact account
    if Rails.env == "development"
      @redirect_uri = "https://xxxxxxxxx.ngrok.io/constant-contact/callback"
    elsif Rails.env == "production" # => "production"
      @redirect_uri = "https://xxxxxxx.herokuapp.com/constant-contact/callback"
    end

    @client_id = Rails.application.credentials.dig(:constant_contact,:api_key)
    @secret  = Rails.application.credentials.dig(:constant_contact,:app_secret)
  end
end

现在所有这些都在浏览器中手动工作,但我将如何重构它以触发后台作业,在用户注册自动完成所有这些工作?

Constant Contact 帐户永远不会改变。我只想在他们注册时将新用户添加到我的 Constant Contact 帐户中的特定列表中。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。