views:

273

answers:

3

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/"
  })
@[email protected]_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!

A: 

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.

jonasespelita
A: 

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.

Shadwell
That's what my code is doing right?session[:request_token]=@request_token.tokensession[:request_token_secret]=@request_token.secretIn my callback action I am creating a new token from from the session values.
jonasespelita
You're trying to store the token in the session, which is failing. If you store the strings "token" and "secret" (from your code) you can rebuild the consumer and then token later when you need to reuse them.
Shadwell
+1  A: 

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.

Andy