Windows Phone. Can't get session key.

 
    • OskolOG said...
    • User
    • 19 Jul 2012, 13:45

    Windows Phone. Can't get session key.

    this code throws WebExeption: The remote server returned an error:NotFound.

    where did I go wrong?

    private void Auth()
    {
    var authToken= MD5Core.GetHashString("OskolOG"+MD5Core.GetHashString(password));
    var sig = "api_key"+ApiKey+"authToken"+authToken+"methodauth.getMobileSessionusername"+username;
    byte[] sigByted = Encoding.UTF8.GetBytes(sig);
    string sigHashed = MD5Core.GetHashString(sigByted + mySecret);


    var sessionRequest = (HttpWebRequest)WebRequest.Create("http://ws.audioscrobbler.com/2.0/?method=auth.getmobilemession&api_key="; + ApiKey + "&username=" + username + "&authtoken=" + authToken + "&api_sig=" + sigHashed);
    sessionRequest.Method = "POST";
    sessionRequest.AllowAutoRedirect = true;
    sessionRequest.BeginGetResponse(new AsyncCallback(sessionCallback), sessionRequest);

    }


    void sessionCallback(IAsyncResult result)
    {
    var webRequest = result.AsyncState as HttpWebRequest;
    var response = (HttpWebResponse)webRequest.EndGetResponse(result);
    var baseStream = response.GetResponseStream();

    using (var reader = new StreamReader(response.GetResponseStream(),
    Encoding.UTF8))
    {
    sessionResult += reader.ReadToEnd();
    }

    }

    • salfab said...
    • User
    • 21 Jul 2012, 15:56

    Http

    ... you are using http : try using https !

    If you can get it to work, let me know : I got stuck with the following message

    <error code="10">Invalid API key - You must be granted a valid key by last.fm</error>

    • OskolOG said...
    • User
    • 22 Jul 2012, 13:26
    НTTPS doesn't change anything.
    I can get websession and my api key's right. Maybe there're problems with sinchronization

    • edua91 said...
    • User
    • 25 Jul 2012, 18:08
    I'm having the same error =/, did you managed to solve it?
    btw if you surround your callback with a try catch, with a webexception, you will be able to read the response of the server

    • OskolOG said...
    • User
    • 29 Jul 2012, 17:24
    I have rewrote my auth method with RestSharp, and now I have mistake 13:
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<lfm status=\"failed\">\n<error code=\"13\">Invalid method signature supplied</error></lfm>\n"

    Code looks like this now:
    var authToken= MD5Core.GetHashString(username+MD5Core.GetHashString(password));
    var sig = "api_key"+ApiKey+"authToken"+authToken+"methodauth.getmobilesessionusername"+username;
    byte[] sigByted = Encoding.UTF8.GetBytes(sig);
    string sigHashed = MD5Core.GetHashString(sigByted+ mySecret);

    var client = new RestClient("https://ws.audioscrobbler.com/2.0/";);
    var request = new RestRequest("?method=auth.getMobileSession&api_key=" + ApiKey + "&authtoken=" + authToken + "&username=" + username + "&api_sig=" + sigHashed, Method.POST);
    var response = client.ExecuteAsync(request, (resp) =>
    {
    if (resp.StatusCode == HttpStatusCode.OK)
    {

    sessionKey = resp.Content;
    }
    });

    • passick said...
    • User
    • 30 Jul 2012, 15:02

    My solution

    Hello! I am also writing last.fm client for wp7. It is almost done. Here is the way I send requests:

    public async static Task<XDocument> MakeRequest(RequestParameters rParams, bool toSign = false)
    {
    try { rParams.Add("api_key", api_key); }
    catch (ArgumentException) { }
    if (toSign == true)
    {
    StringBuilder sb = new StringBuilder();
    //All arguments must be sorted in signature
    //Because silverlight doesn't support SortedDictionary, I used Linq instead
    var sortedList = from q in rParams orderby q.Key ascending select q.Key;

    foreach (string key in sortedList)
    sb.Append(key.ToString() + rParams[key]);

    sb.Append(secret);
    rParams.Add("api_sig", MD5CryptoServiceProvider.GetMd5String(sb.ToString()));
    }
    string request_string = rParams.ToString();
    WebClient client = new WebClient();
    client.Headers["User-Agent"] = "last.fm scrobbler for WP";
    client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
    client.Headers["Accept-Charset"] = "utf-8";
    client.Encoding = Encoding.UTF8;
    byte[] byteArray = rParams.ToBytes();
    Stream toReadFrom;
    try
    {
    string response = "response string not set";
    response = await client.UploadStringTaskAsync(new Uri(root_url), "POST", rParams.ToString());
    toReadFrom = new MemoryStream(Encoding.UTF8.GetBytes(response));
    }
    catch (WebException ex)
    {
    toReadFrom = ((HttpWebResponse)ex.Response).GetResponseStream();
    }
    return XDocument.Load(new XmlSanitizingStream(toReadFrom));
    }

    And here is how I write auth request:

    public static async Task<Session> authorize(string username, string password)
    {
    string password_hash = MD5CryptoServiceProvider.GetMd5String(password);
    string authToken = MD5CryptoServiceProvider.GetMd5String(username.ToLower() + password_hash);
    RequestParameters rParams = new RequestParameters();
    rParams.Add("username", username);
    rParams.Add("authToken", authToken);
    rParams.Add("method", "auth.getMobileSession");
    XDocument ReturnedXML = await Request.MakeRequest(rParams, true);

    if (Request.CheckStatus(ReturnedXML) == 0)
    {
    string skey = (from el in ReturnedXML.Descendants("key") select el).First().Value;
    string user = (from el in ReturnedXML.Descendants("name") select el).First().Value;
    return new Session(skey, user);
    }
    else
    {
    throw new LastFmAPIException(ReturnedXML);
    return null;
    }
    }

Anonymous users may not post messages. Please log in or create an account to post in the forums.