Skip to content

Instantly share code, notes, and snippets.

@BRMatt
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save BRMatt/8e3cca6a942aca0fc489 to your computer and use it in GitHub Desktop.

Select an option

Save BRMatt/8e3cca6a942aca0fc489 to your computer and use it in GitHub Desktop.

Revisions

  1. BRMatt revised this gist Dec 2, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions example.rb
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,9 @@

    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.
  2. BRMatt created this gist Dec 2, 2014.
    26 changes: 26 additions & 0 deletions example.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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

    # 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],
    # ....
    )