trying to save access token and user data to database

439 views
Skip to first unread message

Michael Mitchell

unread,
Sep 14, 2012, 7:53:16 PM9/14/12
to koala...@googlegroups.com
Hi, I'm playing around with a clone of this Koala example app https://github.com/banane/sample-koala-rails-app . However, the author of it doesn't save user data to a database. I'm trying to add that in. 

I created this method on the User model which is invoked from the callback method. However, I'm getting this save error

undefined method `save!' for #<Class:0x00000100b51ea8>

Does anyone see what I'm doing wrong?



User.rb

def self.oath(access_token, graphdata)
    
  
    name = graphdata[:name]
    first_name = graphdata[:first_name]
    last_name = graphdata[:last_name]
    username = graphdata[:username]
    gender = graphdata[:gender]
    email = graphdata[:email]
    access_token = access_token
  save!
  end


home_controller.rb (callback method)
def callback
  if params[:code]
  # acknowledge code and get access token from FB
 session[:access_token] = session[:oauth].get_access_token(params[:code])
end

# auth established, now do a graph call:

@api = Koala::Facebook::API.new(session[:access_token])
begin
@user_info = @api.get_object("me")
@graph_data = @api.get_object("/me/statuses", "fields"=>"message")
rescue Exception=>ex
puts ex.message
end

user = User.oath(session[:access_token], @user_info)
session[:user_id] = user.id

  
  respond_to do |format|
format.html {   }  
end


end

banane

unread,
Sep 15, 2012, 11:29:24 PM9/15/12
to koala...@googlegroups.com
Hi- I'm the author :)

I think something's wrong with the way you setup the model - I can check your github code. 

Anna


--
You received this message because you are subscribed to the Google Groups "Koala & The Facebook Graph API" group.
To view this discussion on the web visit https://groups.google.com/d/msg/koala-users/-/winOFSq6T4gJ.
To post to this group, send email to koala...@googlegroups.com.
To unsubscribe from this group, send email to koala-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/koala-users?hl=en.

banane

unread,
Sep 15, 2012, 11:32:18 PM9/15/12
to koala...@googlegroups.com
Oh, you didn't fork it publicly. OK, if you can post here your model info, or offline, or add a github issue we can talk about the code in ref to line #s.

Michael Mitchell

unread,
Sep 16, 2012, 1:11:21 AM9/16/12
to koala...@googlegroups.com
Hi Anna,

Thanks a lot for taking an interest. I just put it on Github  https://github.com/MikeOnRails/koalatestapp

I'm not very experienced and probably lacking in talent for coding, so there might be multiple problems with what I'm doing.


Mike

P.S. Dein domain banane.com gefaellt mir sehr

banane

unread,
Sep 16, 2012, 1:24:17 AM9/16/12
to koala...@googlegroups.com
OK, setup a quick User model and added it into the example. I'll upload a branch called "userdata" to github so you can see it.

Basically:

- on command line "rails generate model User name:string access:string ...." all the fields you want in it.
- rake db:migrate

In HomeController, create a new User with teh following, once you have access_token back:

user = User.new(:access=>access_token)
user.save

Start server, click to auth. View returned graph data.

To verify that that the User save worked, I go into rails console, query all users:
"users = User.all"

You will see the user you just created and saved. 

Michael Mitchell

unread,
Sep 16, 2012, 1:31:44 AM9/16/12
to koala...@googlegroups.com
Hi Anna,

thanks, One question though. In the app, I pushed to github, I tried not only to save the access_token, but also to get information (name, first name, email) from the @graph_data about the user and save it in the model. I think it's trickier to save the access_token together with this user information data...If possible, can you include that in your example

Mike

banane

unread,
Sep 16, 2012, 1:49:51 AM9/16/12
to koala...@googlegroups.com
Branch that saves to a model/db:

https://github.com/banane/sample-koala-rails-app/tree/userdata

Here is an example where I create a scaffolded User model, and save info to it from Facebook when it's available.

For graph info- it's many-to-one, so you should probably create a new model like "feedstream" or "posts" and then save to that.

Cheers
Anna
Reply all
Reply to author
Forward
0 new messages