我已经在我的 Rails 应用程序中成功构建了一个控制器,该控制器连接到Constant Contact v3 API,并将当前用户的电子邮件地址添加到 Constant Contact 的列表中。但是,这仅在用户登录并通过单击浏览器中的链接触发该过程时才有效。
我不知道如何重构它(after_create
例如,在用户模型中使用回调)或后台作业。当新用户注册时,我希望这发生在幕后。我正在关注 Constant Contact API 文档中的OAuth2.0 服务器流程。
以下是它目前的工作方式:
- 用户注册并访问
constant_contact#index
页面 (/constant-contact),他们单击链接将我的 Constant Contact API 密钥发送给 Constant Contact。 - Constant Contact 使用授权代码响应我在
constant_contact#callback
(/constant-contact/callback)设置的重定向 URL - 在我的
constant_contact#callback
操作中,我从参数中获取授权代码并使用它来构建一个 JSON 帖子,然后将授权代码发送回 Constant Contact API,以便 Constant Contact 知道它正在与正确的域通信。 - 然后,Constant Contact 在我的 /constant-contact/callback url 处返回一个令牌作为响应,然后我可以使用此令牌与 Constant Contact API 进行交互。
这是我用来完成所有这些操作的控制器(如果更易于阅读,则为gist 版本):
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
现在所有这些都在浏览器中手动运行,但是我将如何重构它以便触发一个后台作业,当用户注册时自动完成所有这些工作?
恒定联系帐户永远不会改变。我只想在他们注册时将新用户添加到我的恒定联系人帐户中的特定列表中。