Skip to content

Instantly share code, notes, and snippets.

@BRMatt
Last active August 29, 2015 14:10
Show Gist options
  • Save BRMatt/8e3cca6a942aca0fc489 to your computer and use it in GitHub Desktop.
Save BRMatt/8e3cca6a942aca0fc489 to your computer and use it in GitHub Desktop.
require 'oauth2'
client = OAuth2::Client.new(
'client_id',
'client_secret',
:site => 'https://workshop.memberful.com',
:authorize_url => '/oauth'
)
redirect_to client.auth_code.authorize_url
# Member has been sent to Memberful by above redirect, then Memberful has sent the user back to the
# callback action below:
# I'd recommend playing around with this call and using obviously wrong values for params[:code]
# e.g. "foobar" so that you can get an idea of how the OAuth2 gem will fail if it can't fetch
# a valid token from Memberful.
token = client.auth_code.get_token(params[:code])
# Get the member's account info. This will be the info described here
# https://memberful.com/help/integrate/advanced/sign-members-in/
account_info = token.get('/account.json')
member_info = account_info[:member]
# http://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-find_or_create_by
member = User.find_or_create_by(
:id => member[:id],
:name => member[:full_name],
# ....
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment