我想创建一个网页,让我的用户登录和查看他们的数据。数据托管在Parse.com它公开它作为一个REST API。
我使用asp.net / C#进行访问,并可以得到这一切通过使用他们的API密钥和应用的关键。不过,我需要用C编写一个版本的这个PHP code,从他们的文档#...
要做到这一点,发送GET请求来与用户名和密码的URL连接codeD参数/ 1 /登录端点:
卷曲-X GET \
-HX-解析,应用ID:$ {APPLICATION_ID}\
-HX-解析-REST的API密钥:$ {REST_API_KEY}\
-G \
--data-urlen code'用户名= cooldude6'\
--data-urlen code'密码= p_n7!-e8\
https://api.parse.com/1/login
现在我在这里停留......什么,我试图返回一个HTTP 400,像这样code ...
[WebMethod的]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)
公共静态字符串ParseAuthenticate(字符串strUserName中,字符串strPassword)
{
VAR HttpWebRequest的=(HttpWebRequest的)WebRequest.Create(https://api.parse.com/1/login);
httpWebRequest.ContentType =应用/的X WWW的形式urlen codeD;
httpWebRequest.Headers.Add(用户名:+ strUserName中);
httpWebRequest.Headers.Add(密码:+ strPassword);
//通过基本身份验证凭据
httpWebRequest.Credentials =新的NetworkCredential(我的解析应用ID,解析API其余密钥);
httpWebRequest.Method =GET;
VAR HTT presponse =(HttpWebResponse)httpWebRequest.GetResponse();
使用(VAR的StreamReader =新的StreamReader(HTT presponse.GetResponseStream()))
{
VAR的responseText = streamReader.ReadToEnd();
返回的responseText;
}
}
下面是我的C#code,它让我所有的数据...但我只希望数据的用户正在尝试登录...
[WebMethod的]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)
公共静态字符串GetParseData()
{
VAR HttpWebRequest的=(HttpWebRequest的)WebRequest.Create(https://api.parse.com/1/classes/Visit);
//通过基本身份验证凭据
httpWebRequest.Credentials =新的NetworkCredential(我的解析应用ID,我的解析REST API密钥);
httpWebRequest.Method =GET;
VAR HTT presponse =(HttpWebResponse)httpWebRequest.GetResponse();
使用(VAR的StreamReader =新的StreamReader(HTT presponse.GetResponseStream()))
{
VAR的responseText = streamReader.ReadToEnd();
返回的responseText;
}
}
任何帮助/指针将大大AP preciated。谢谢!
解决方案@Talljoe,@LB - 感谢您的帮助/指导。摆弄周围code一段时间后,我终于理解了它。下面是我的工作code样子...
[WebMethod的]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)
公共静态字符串ParseAuthenticate(字符串strUserName中,字符串strPassword)
{
字符串的URL =https://api.parse.com/1/login?username=+ HttpUtility.UrlEn code(strUserName中)+&放大器;密码=+ HttpUtility.UrlEn code(strPassword) ;
VAR HttpWebRequest的=(HttpWebRequest的)WebRequest.Create(URL);
httpWebRequest.ContentType =应用/的X WWW的形式urlen codeD;
//通过基本身份验证凭据
httpWebRequest.Credentials =新的NetworkCredential(我的解析应用程序ID,我的解析REST API密钥);
httpWebRequest.Method =GET;
VAR HTT presponse =(HttpWebResponse)httpWebRequest.GetResponse();
使用(VAR的StreamReader =新的StreamReader(HTT presponse.GetResponseStream()))
{
VAR的responseText = streamReader.ReadToEnd();
//现在你有你的回应。
//或false取决于信息的响应
//返回true;
返回的responseText;
}
}
}
请让我知道,如果这事是不正确/可以做的更好。我没有太大的净家伙,pretty的多砍死我去这家。
感谢您帮助我。
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!
解决方案@Talljoe, @L.B - 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.