title: Invalid method signature when sending HTTP POST request
body: <p>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.</p>
  
  <p>But when I run the <a href="http://www.last.fm/api/show/playlist.create" rel="nofollow"><code>playlist.create</code></a>, the playlist is not created.</p>
  
  <p>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.</p>
  
  <pre><code>private static void buildPlaylist() {
  
      String mood = "Happy";
      System.out.println("\nMood is " + mood + "\n\n");
  
      String title = URLEncoder.encode(mood + " " + new Date().getTime(), "UTF-8");
      String description = URLEncoder.encode("For when you are " + mood + ". Created by MoodicPlayer.", "UTF-8");
  
      MessageDigest md = MessageDigest.getInstance("MD5");
  
      String apiSig = "api_key" + key + "description" + description + "methodplaylist.createsk" + sessionKey + "title" + title + secret;
      md.update(apiSig.getBytes());
      byte byteData[] = md.digest();
      //convert the byte to hex format
      StringBuffer sb = new StringBuffer();
      for (int i = 0; i &lt; byteData.length; i++) {
          sb.append(Integer.toString((byteData[i] &amp; 0xff) + 0x100, 16).substring(1));
      }
      String hashedSig = sb.toString();
  
      // FOR DEBUGGING
      System.out.println("api_key = " + key);
      System.out.println("api_sig = " + hashedSig);
      System.out.println("session key = " + sessionKey);
      // FOR DEBUGGING
  
      String urlParameters = "method=playlist.create&amp;api_key="+ key + "&amp;api_sig=" + hashedSig + "&amp;description=" + description + "&amp;sk=" + sessionKey + "&amp;title=" + title;
      String request = "http://ws.audioscrobbler.com/2.0/";
  
      URL url = new URL(request);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();     
      connection.setDoOutput(true);
      connection.setDoInput(true);
      connection.setInstanceFollowRedirects(false); 
      connection.setRequestMethod("POST"); 
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      connection.setRequestProperty("charset", "utf-8");
      connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
      connection.setUseCaches(false);
  
      DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
      wr.writeBytes(urlParameters);
      wr.flush();
      wr.close();
  
      InputStream is = null;
      Scanner s = null;
      try {
          if (connection.getResponseCode() != 200) {
              s = new Scanner(connection.getErrorStream());
          } else {
              is = connection.getInputStream();
              s = new Scanner(is);
          }
          s.useDelimiter("\\Z");
          String response = s.next();
          System.out.println("\nResponse: " + response + "\n\n");
          BufferedWriter out = new BufferedWriter(new FileWriter("requestCreate.xml"));
          out.write(response);
          out.close();
      } catch (IOException e2) {
          e2.printStackTrace();
      }
  
      // FOR DEBUGGING
      try {
          System.out.println("Response Code: " + connection.getResponseCode());
          System.out.println("Response Message: " + connection.getResponseMessage()); 
      } catch (IOException e) {
          e.printStackTrace();
      }
      // FOR DEBUGGING
      connection.disconnect();
  }
  </code></pre>
  
  <p>And the output:</p>
  
  <pre><code>Starting test for creating playlist...
  
  Mood is Happy
  api_key = xxxxx
  api_sig = xxxxxx
  session key = xxxxx
  
  Response: &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;lfm status="failed"&gt;
  &lt;error code="13"&gt;
      Invalid method signature supplied
  &lt;/error&gt;
  &lt;/lfm&gt;
  
  
  Response Code: 403
  Response Message: Forbidden
  </code></pre>
  
  <p>I have checked the code, but cannot find any error. I mean the code was fine 16 hours ago, and suddenly it's not working!</p>
  
  <hr>
  
  <p>For security purposes, I cannot show you my security keys. However, I did write a quick program to find the last.fm session key: <a href="http://github.com/thekarangoel/LastFMSessionKeyFinder" rel="nofollow">http://github.com/thekarangoel/LastFMSessionKeyFinder</a> 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.</p>