简体   繁体   中英

OAauth gem in rails problem

I'm trying out OAuth for twitter api authentication and ran into a brick wall when my code spits out the message:

 (instance of OAuth::Consumer needs to have method `marshal_load')

My code:

@consumer=OAuth::Consumer.new( "token","secret", {
    :site=>"http://mysite.com/"
  })
@request_token=@consumer.get_request_token
session[:request_token]=@request_token.token
session[:request_token_secret]=@request_token.secret
redirect_to @request_token.authorize_url

Errors are in the session assignment part. Clearing the session store doesn't correct the problem.

rake tmp:clear  

Code works perfectly in irb but running it app-wise doesn't work. What could be the problem and solution to this?

Thanks!

The same thing happened to me. It was because I had FIRST tried storing the entire token in my session:

session[:request_token]=@request_token

Then, I changed my code to only store the token(string) in the session:

session[:request_token]=@request_token.token

However, I was getting the same error as you

(instance of OAuth::Consumer needs to have method `marshal_load')

Because Rails was trying to look at what I have previously stored in my session (the entire token). Simply go into your browser's cookies and remove all of your cookies for your development host (mine was localhost). After that, try it again and everything should work fine.

After some googling around I came up with my own solution.

I don't know if this is the best solution for this but this is how I got around it:

I aded the following code to my environment.rb:

class OAuth::Consumer
     def marshal_load(*args)
      self
    end

More of a hack, This would definitely fix the marshal load error. I don't know though if this would cause other problems.

I think it is the same problem as this question .

The answers there suggest that because the token isn't serializable it can't be retrieved from the session and that you should store the key in the session and create a new token from that and the secret.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM