0

I need to implement last.fm authentication in my Windows Phone 8 application. Last.fm requires a POST request with https for successful login. Here is what I'm doing.

public async void GetMobileSession(string userName, string password, Action<LastFmAuthResponse> onCompletion)
    {
        string CORE_URL = "https://ws.audioscrobbler.com/2.0/";
        var parameters = new Dictionary<string, string>();
        parameters.Add("username", userName);
        parameters.Add("password", password);
        parameters.Add("method", "auth.getMobileSession");
        parameters.Add("api_key", api_key);

        string signature = GetSignature(parameters);

        string comboUrl = string.Concat("method=auth.getMobileSession", "&api_key=", api_key,
            "&username=", userName, "&password=", password, "&api_sig=", signature);

        LastFmAuthResponse response = null;

        byte[] pendingPostContent = Encoding.UTF8.GetBytes(comboUrl);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(CORE_URL);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

        using (Stream requestStream = await request.GetRequestStreamAsync())
        {
            await requestStream.WriteAsync(pendingPostContent, 0, pendingPostContent.Length);
        }

        request.BeginGetResponse(new AsyncCallback(n =>
        {
            HttpWebResponse rawResponse = (HttpWebResponse)request.EndGetResponse(n);

            string rawData = string.Empty;
            using (StreamReader reader = new StreamReader(rawResponse.GetResponseStream()))
            {
                rawData = reader.ReadToEnd();
            }

            try
            {
                if (!string.IsNullOrEmpty(rawData))
                {
                    response = CommonFunctions.GetObjectFromString<LastFmAuthResponse>(rawData);
                }
            }
            catch
            {
            }

            onCompletion(response);

        }), null);
    }

However the code is failing at request.BeginGetResponse It returns an error saying remote server could not be found. Can someone please point out what I'm doing wrong here?

thatrohit
  • 177
  • 8
  • If you are still having this issue, you can refer to my [Last.fm C# SDK](https://github.com/inflatablefriends/lastfm). The file you want is [LastAuth.cs](https://github.com/inflatablefriends/lastfm/blob/master/src/IF.Lastfm.Core/Api/LastAuth.cs) – rikkit Oct 29 '14 at 11:14
  • I tried downloading your SDK via nuget but I'm unable to authenticate still, the error I'm getting is "Could not load file or assembly 'xBrainLab.Security.Cryptography" – thatrohit Oct 30 '14 at 17:36
  • My sdk isn't on nuget, I haven't got round to that yet - you can use git to download the repo or click 'download zip' on the page. xBrainLab.Security.Cryptography is included in the /lib folder. If you are copying the dll into your project, then you need to copy that dll too. – rikkit Oct 30 '14 at 17:44
  • I'm assuming I have to reference IF.Lastfm.Core.dll in my project which I'll get by building your repo. Unfortunately I cannot do that since my VS2012 Express Edition is incompatible with your solution. Is it possible to obtain the .dll file from somewhere else? – thatrohit Oct 31 '14 at 05:03
  • Ah, so it turns out that VS2012 express can't open portable class libraries :/. I put a release on GitHub https://github.com/inflatablefriends/lastfm/releases/tag/v0.1.1. – rikkit Oct 31 '14 at 11:43

0 Answers0