3

Ok this is really frustrating. I'm using my API key perfectly fine. Authentication does just fine, token is generated fine, session key is also OK.

But when I run the playlist.create, the playlist is not created.

Addendum (6 April 2013): It turned out I had to generate a new md5 hash for my sig, but even after doing so, the method does not work.

Addendum 2 (9 April 2013): I started from almost scratch today, and redid most of my code. I created 16 playlists successfully, no errors. But today, the same code's not working. I didn't touch it at all and yet the error.

nvm


For security purposes, I cannot show you my security keys. However, I did write a quick program to find the last.fm session key: http://github.com/thekarangoel/LastFMSessionKeyFinder Simply register at last.fm/api, enter your API key and secret in the program and you'll have everything you need to test the API call.

Karan Goel
  • 1,117
  • 1
  • 12
  • 24
  • 2
    Um, they send a response and error code. You should probably read it. – djechlin Mar 29 '13 at 15:42
  • The error I get when manually triggering the POST request (using a FF addon Poster) is this: ` Invalid Method - No method with that name in this package ` – Karan Goel Mar 29 '13 at 15:55
  • My method is correct, parameters are correct, but still the error. http://i.imgur.com/y6A4r0z.png – Karan Goel Mar 29 '13 at 15:57
  • Hmmm, this is tricky. It looks right. Though I can't understand why you're using the parseUrl method. Surely you just need to dump the params string into the call to wr.writeBytes without any further processing? – Bobulous Mar 29 '13 at 16:27
  • Downvote was me and that was because at the time you were ignoring the response and error code when the link you included clearly implied you should check it, which is research that is expected as part of posting a good question. I haven't had time to check your updated research since. – djechlin Mar 29 '13 at 16:46
  • @Arkanon `parseUrl` just encodes the URL so all special characters including spaces are taken care of. It's just a precautionary measure. – Karan Goel Mar 29 '13 at 16:59
  • @djechlin I wasn't ignoring it, I just didn't find posting the part about manual request necessary. – Karan Goel Mar 29 '13 at 17:00
  • I'd pixelate the sig and sk more, I can still make those out. – Taylor Mar 29 '13 at 17:04
  • If you can't get the post to succeed outside of Java - just sending a raw POST request - then this isn't a Java question. You should remove all code from this question and retag and specifically ask regarding difficulty you have getting a POST request to last.fm to succeed in the first place. – djechlin Mar 29 '13 at 17:17
  • @djechlin Good suggestion. You got anything to help with this? – Karan Goel Mar 30 '13 at 01:58
  • @user2059238 What is the exact output if you dump the value of `urlParameters` to the console? Your screenshot doesn't show half of the variables that should be contained in that variable. – Bobulous Mar 30 '13 at 11:24
  • For security purposes, I cannot show you my security keys. However, I did write a quick program to find the last.fm session key: https://github.com/thekarangoel/LastFMSessionKeyFinder Simply register at last.fm/api, enter your API key and secret in the program and bam. – Karan Goel Mar 30 '13 at 13:57
  • Which encoding does the "Poster" plugin use? Can you set it? And can you please run [HttpFox](https://addons.mozilla.org/de/firefox/addon/httpfox/) when you execute the POST, and provide the result of the "Post Data (RAW)"? – skirsch Apr 01 '13 at 19:31
  • @skirsch utf-8. in httpFox, Post Data tab says "(This request contained no POST data)" – Karan Goel Apr 01 '13 at 23:08
  • Oh well, perfectly reasonable that it doesn't work then: the [last.fm API](http://www.lastfm.de/api/rest) says _If you are accessing a write service, you will need to submit your request as an HTTP POST request. All POST requests should be made to the root url: `http://ws.audioscrobbler.com/2.0/` With all parameters (including the 'method') sent in the POST body._ Your "Poster" obvioulsy puts the methods onto the URL itself, and therefore fails. – skirsch Apr 02 '13 at 06:07
  • @skirsch Yes, but when I fix that and send the params as POST body, I get "Invalid method parameters" error. – Karan Goel Apr 02 '13 at 16:15
  • Okay, which platform are you using? Which java version? And, when you said "I actually fixed the code on my machine long ago" what code is listed above? Does it still differ from the one on your machine? Can you provide a runnable piece of code? – skirsch Apr 02 '13 at 17:43
  • I'm on Win 8, Java v1.6. Here's the current code without my api key and secret: https://github.com/thekarangoel/MoodicPlayer/blob/master/src/im/goel/MoodicPlayer/MoodicPlayer.java – Karan Goel Apr 02 '13 at 20:42
  • Why does `parseURL` return a `URL` instead of a `URI`? – Buhake Sindi Apr 04 '13 at 07:52
  • Because I need to open a `URLConnection` on a `URL` and not a `URI`. – Karan Goel Apr 04 '13 at 16:21
  • what is the content of mood ? does it contain any special characters, or spaces, or &, accents, ?, anything that could puzzle the parameters reading ? – njzk2 Apr 07 '13 at 22:42
  • @njzk2 Not in the configuration I have right now. – Karan Goel Apr 08 '13 at 01:13
  • what is your locale encoding? are you certain it is utf-8? – njzk2 Apr 08 '13 at 07:44
  • I don't quite get what is the purpose of this part `(byteData[i] & 0xff) + 0x100` (or is it to make sure you have 2 digits?) – njzk2 Apr 08 '13 at 07:47
  • Yes, it is UTF=8. The part does the md5 hash conversion. I'm not entirely sure if the particular part is needed, though. – Karan Goel Apr 08 '13 at 17:50
  • I updated the OP with fresh details. – Karan Goel Apr 08 '13 at 19:13
  • You should close this question and open up another one. Most comments and answers (upvotes as well) don't make sense anymore, as there's barely anything left of your original question. This is not the idea behind stackoverflow, as it renders most of these posts useless for "the afterworld". – skirsch Apr 08 '13 at 20:53
  • God why is there this new error. My program was just fine till last night, and today suddenly the fresh errors. – Karan Goel Apr 09 '13 at 16:39
  • Open up a new question, please. – skirsch Apr 09 '13 at 17:08

1 Answers1

3

My gazillionth answer

You did not include the description in your hashed signature.

My second answer

As mentioned before in one of my comments, you're code is causing a MalformedURLException. You don't see this because you're doing no exception handling within your SwingWorker subclass. If you add exception handling as suggested here, you'll see that.

My first answer

You're missing an & in your parameter list:

String params = "method=playlist.create&title=" + title+ "api_key=" + ...

Should be

String params = "method=playlist.create&title=" + title+ "&api_key=" + ...

It's probably a good idea to read the answer of the request (connection.getInputStream() etc.) and check what the issue is.

Community
  • 1
  • 1
skirsch
  • 1,640
  • 12
  • 24
  • Also, when I try `new java.net.URL("method=playlist.create&title=foo")`, I get an `java.net.MalformedURLException` - so I wonder how this works for you? – skirsch Apr 01 '13 at 11:03
  • See the manual request I tried. Even that doesn't work. – Karan Goel Apr 01 '13 at 17:35
  • Well, the fact that the manual request fails shouldn't prevent you from correcting your code. Sure it's kind of funny that it fails, but why not try to make your code run? – skirsch Apr 01 '13 at 19:28
  • I actually fixed the code on my machine long ago, will correct here as well. – Karan Goel Apr 01 '13 at 23:09
  • Okay, so we found some reasons why the manual execution fails. So if you want to get this fixed, enhance your code to read the answer, and post the results here. And with the code above I still don't see how you circumvent the `MalformedURLException`... – skirsch Apr 02 '13 at 06:09
  • When I fix that and send the params as POST body, I get "Invalid method parameters" error. I'm 100% positive the parameters are correct. – Karan Goel Apr 02 '13 at 16:15
  • `java.net.MalformedURLException: no protocol: method=playlist.create&title=Happy&api_key=5b9e1ad78d831a259ce90cecae93cf05&description=--XXXXXXXX--&api_sig=--XXXXXXXX--&sk=--XXXXXXXX--`. How do I fix this? – Karan Goel Apr 04 '13 at 19:22
  • Also tried `https` protocol, but same error. – Karan Goel Apr 04 '13 at 19:30
  • 1
    E.g. change to `String params = "http://foo.bar?method=playlist.create&title=" + ...` and in `parseUrl`, just return `new URL(s).getQuery()`. Or you can probably just use the `params` value as is, I'm not sure if this whole URL-conversion-thing adds anything. – skirsch Apr 04 '13 at 20:12
  • Ok so I'm not getting any exceptions now, but the method still isn't working. Not even with the manual POST request. – Karan Goel Apr 05 '13 at 00:56
  • Now comes the part where you read the HTTP response to find out what the problem is. You need to do that anyway, as you certainly want to add some tracks - and therefore you need the playlist's ID. – skirsch Apr 05 '13 at 05:52
  • Hmm.. the playlist isn't being created.. I get this error: `Invalid method signature supplied`. I'm not sure what's wrong with the method signature here. – Karan Goel Apr 05 '13 at 18:23
  • Would you please push your changes to github? – skirsch Apr 05 '13 at 20:51
  • Oh boy. Now you completely screwed your POST request. You don't seem to do half of what I suggest, or I'm just not capable of making myself clear. The POST address is `http://ws.audioscrobbler.com/2.0/` and you don't put the URL into the body, but the parameters. That's not be mixed up... – skirsch Apr 06 '13 at 12:13
  • I've updated the OP with more details, updated code. – Karan Goel Apr 06 '13 at 19:00
  • Well, it runs for me now. I mean, I adapted the github code with the changes you posted and now have a couple of new "Happy" playlists :) The thing is, I don't see why it's not working for you... so push your changes to github, then I can check better. – skirsch Apr 06 '13 at 20:51
  • Still not working for me. I also am encoding the URL now for better compatibility. GH repo updated – Karan Goel Apr 06 '13 at 22:38
  • Again, your bad exception handling bites you in the ass. The URL encoding you do is wrong, causing last.fm to answer with a HTTP 400. You need to encode the parameter values (encoding the title & description is sufficient, the API-stuff is only ASCII anyway), not the whole parameter string. Also, there seems to be a problem with fetching some tracks; don't know why this started to happen. Therefore, in `doInBackground()`, replace `currentProgress = allTracks.size();` with `currentProgress++;`, otherwise your stuck in an endless loop. – skirsch Apr 07 '13 at 10:06
  • Ok so I fixed exception handling throughout the code, and then I started to see the infinite loop bug. Fixed that as well. But now the program is not even able to complete finding the tracks. Playlist is not being created either. I've updated the GH repo. – Karan Goel Apr 07 '13 at 16:23
  • Yeah, well. Your exception handling is still biting you. Now there's a `NullpointerException`. Again: http://stackoverflow.com/questions/6523623/gracefull-exception-handling-in-swing-worker#6524300 – skirsch Apr 07 '13 at 17:47
  • Alright so I'm catching the exception in `done()` method now. But I'm unable to replicate the `NullPointerException`. Where exactly does it occur? And thy is my code not able to get info for all tracks just because I messed with the exceptions? – Karan Goel Apr 07 '13 at 18:10
  • I don't know what you did, I don't know what your issue is. I can only guess. Show me your code. – skirsch Apr 07 '13 at 20:28
  • 1
    `try {...} catch (Exception e) { e.printStacktrace(); }` is the second-worst thing you can do. It's fine for prototyping & getting things done fast, but if something goes wrong, you screw the expectations of the code being executed afterwards - most of the time resulting in NPE (that's when you're lucky). – skirsch Apr 07 '13 at 20:29
  • You told me that the code worked for you, and playlists were being created. I'll try to work on the code this week, clean it up, make exceptions better and get back here. – Karan Goel Apr 07 '13 at 21:48
  • I updated the OP with fresh details. – Karan Goel Apr 08 '13 at 19:11