Most clever way to parse a Facebook OAuth 2 access token string

Posted by RyOnLife on Stack Overflow See other posts from Stack Overflow or by RyOnLife
Published on 2010-05-13T08:03:32Z Indexed on 2010/05/13 8:14 UTC
Read the original article Hit count: 453

Filed under:

It's a bit late, but I'm disappointed in myself for not coming up with something more elegant. Anyone have a better way to do this...

When you pass an OAuth code to Facebook, it response with a query string containing access_token and expires values.

access_token=121843224510409|2.V_ei_d_rbJt5iS9Jfjk8_A__.3600.1273741200-569255561|TxQrqFKhiXm40VXVE1OBUtZc3Ks.&expires=4554

Although if you request permission for offline access, there's no expires and the string looks like this:

access_token=121843224510409|2.V_ei_d_rbJt5iS9Jfjk8_A__.3600.1273741200-569255561|TxQrqFKhiXm40VXVE1OBUtZc3Ks.

I attempted to write a regex that would suffice for either condition. No dice. So I ended up with some really ugly Ruby:

s = s.split("=")
@oauth = {}
if s.length == 3
  @oauth[:access_token] = s[1][0, s[1].length - 8]
  @oauth[:expires] = s[2]
else
  @oauth[:access_token] = s[1]
end

I know there must be a better way!

© Stack Overflow or respective owner

Related posts about regex