How to send API credentials and username / password using .Net / Parse.com? (PHP to C #)

I am trying to create a web page that will allow my users to login and view their details. The data is hosted on Parse.com, which provides it as a REST API.

I am using asp.net/C# to access it and can get everything using their API key and application key. However, I need to write a version of this PHP code from my documentation in C # ...

To do this, send a GET request to the / 1 / login endpoint with the username and password as URL-encoded parameters:

curl -X GET \
-H "X-Parse-Application-Id: ${APPLICATION_ID}" \
-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
-G \
--data-urlencode 'username=cooldude6' \
--data-urlencode 'password=p_n7!-e8' \
https://api.parse.com/1/login

      

Now i'm stuck here ... everything i tried returns HTTP 400 like this code ...

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string ParseAuthenticate(string strUserName, string strPassword )
{
    var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.parse.com/1/login");
    httpWebRequest.ContentType = "application/x-www-form-urlencoded";

    httpWebRequest.Headers.Add("username:" + strUserName);
    httpWebRequest.Headers.Add("password:" + strPassword);

    //pass basic authentication credentials
    httpWebRequest.Credentials = new NetworkCredential("My Parse Application Id", "Parse API Rest Key");

    httpWebRequest.Method = "GET";

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var responseText = streamReader.ReadToEnd();
        return responseText;
    }

}

      

Here is my C # code that will get me all the data ... but I only want the data for the user trying to login ...

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetParseData()
{
    var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.parse.com/1/classes/Visit");

    //pass basic authentication credentials
    httpWebRequest.Credentials = new NetworkCredential("My Parse Application Id", "My Parse REST API Key");
    httpWebRequest.Method = "GET";

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var responseText = streamReader.ReadToEnd();
        return responseText;
    }

}

      

Any help / pointers would be appreciated. Thank!

+3


source to share


2 answers


@Talljoe, @LB - Thanks for your help / guidance. After doing a bit of tinkering with the code, I finally figured it out. This is how my working code looks like ...

    [WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string ParseAuthenticate( string strUsername, string strPassword)
{

    string url = "https://api.parse.com/1/login?username=" + HttpUtility.UrlEncode(strUsername) + "&password=" + HttpUtility.UrlEncode(strPassword);

    var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

    httpWebRequest.ContentType = "application/x-www-form-urlencoded";

    //pass basic authentication credentials
    httpWebRequest.Credentials = new NetworkCredential("My Parse App Id", "My Parse REST API Key");

    httpWebRequest.Method = "GET";

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var responseText = streamReader.ReadToEnd();

        //Now you have your response.
        //or false depending on information in the response
        // return true;
        return responseText;
    }
}
}

      



Please let me know if anything is wrong / can be done better. I'm not much of a .Net guy and have pretty hard hacked my way to this.

Thanks for helping me.

+1


source


I see two errors in the first code example. First, you need to pass your credentials as headers and not use HTTP authentication.

httpWebRequest.Headers.Add("X-Parse-Application-Id", applicationId); 
httpWebRequest.Headers.Add("X-Parse-REST-API-KEY", apiKey); 

      



Second, you need to pass parameters as data to call, not as headers. To do this, you need to create a string with url-encoded data ("username = the_username & password = a% 30password"), and then write it to the request stream.

+4


source







All Articles