简体   繁体   中英

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

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

I am using asp.net / C# to access it and can get it all by using their API Key and Application Key. However, I need to write a version of this PHP code from their documentation in C#...

To do this, send a GET request to the /1/login endpoint with 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 am stuck here...anything that I have tried returns an HTTP 400, such as 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 gets me all the data...but I only want data for the user that is 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 will be greatly appreciated. Thanks!

I see two things wrong with your first code sample. First, you need to pass your credentials as headers, not using HTTP authentication.

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

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

@Talljoe, @LB - Thanks for your help / guidance. After mucking around with code for a while, I finally figured it out. Here 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 in it is incorrect / could be done better. I am not much of a .Net guy and pretty much hacked my way to this.

Thanks for helping me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM