我正在尝试创建一个允许我的用户登录并查看其数据的网页。数据托管在Parse.com上,将其公开为REST API。
我正在使用asp.net / C#来访问它,并且可以使用他们的API密钥和应用程序密钥来完成所有操作。但是,我需要从C#...
的文档中编写这个PHP代码的版本为此,请使用用户名和密码作为URL编码参数向/ 1 / login端点发送GET请求:
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
现在我被困在这里......我尝试过的任何东西都会返回一个HTTP 400,比如这段代码...
[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;
}
}
这是我的C#代码,它可以获取所有数据...但我只想要尝试登录的用户的数据...
[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;
}
}
任何帮助/指针将不胜感激。谢谢!
答案 0 :(得分:4)
我看到你的第一个代码示例有两个问题。首先,您需要将凭据作为标头传递,而不是使用HTTP身份验证。
httpWebRequest.Headers.Add("X-Parse-Application-Id", applicationId);
httpWebRequest.Headers.Add("X-Parse-REST-API-KEY", apiKey);
其次,您需要将参数作为数据传递给调用,而不是标题。为此,您需要创建一个包含url编码数据的字符串(“username = the_username& password = a%30password”),然后将其写入请求流。
答案 1 :(得分:1)
@Talljoe,@ L.B - 感谢您的帮助/指导。在用代码捣乱了一段时间之后,我终于弄明白了。以下是我的工作代码的样子......
[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;
}
}
}
如果其中的任何内容不正确/可以做得更好,请告诉我。我不是一个.Net的家伙,而且几乎是我的方式。
感谢您的帮助。