Skip to content

Instantly share code, notes, and snippets.

@benbonnet
Created May 28, 2009 14:01
Show Gist options
  • Save benbonnet/119314 to your computer and use it in GitHub Desktop.
Save benbonnet/119314 to your computer and use it in GitHub Desktop.
def lastfm_login
if params[:token]
endpoint = ""
token = params[:token].to_s
api = "xxxxx"
secret = "xxxxx"
method = "auth.getSession"
sign = Digest::MD5.hexdigest('api_key' + api + 'method' + method + 'token' + token + secret)
endpoint += "?method=#{method}&token=#{token}&api_key=#{api}&api_sig=#{sign}"
@http = Net::HTTP.new('ws.audioscrobbler.com')
@http.start() do |http|
req = Net::HTTP::Get.new("/2.0/#{endpoint}")
response = http.request(req)
@doc = Hpricot.parse(response.body)
if @doc.search("lfm").attr("status").to_s == "ok"
key = @doc.search("key").inner_html
name = @doc.search("name").inner_html
subscriber = @doc.search("subscriber").inner_html
puts "key => #{key} // name => #{name} // subscriber => #{subscriber}"
user = User.find session[:user]
api = Api.create( :api_type_id => 5,
:user_id => user.id,
:token => key,
:username => name)
end
puts response.body
end
redirect_to :controller => "veeo", :action => "update_used_apis"
else
endpoint = "http://www.last.fm/api/auth/"
api = "xxxxx"
secret = "xxxxx"
perms = "write"
#sign = Digest::MD5.hexdigest(secret + 'api_key' + api + 'perms' + perms)
endpoint += "?api_key=#{api}"
redirect_to endpoint
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment