2012-03-10 49 views
3

我正在尝试创建一个允许我的用户登录并查看其数据的网页。数据在Parse.com上托管,并将其公开为REST API。如何使用.Net/Parse.com发送API凭证和用户名/密码? (PHP到C#)

我使用asp.net/C#来访问它,并可以通过使用它们的API密钥和应用程序密钥来获取它。不过,我需要写一个版本,从他们在C#中的文档此PHP代码...

要做到这一点,发送GET请求/ 1 /登录端点的用户名和密码以URL编码参数:

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#代码,让我所有的da TA ......但我只想要正试图登录的用户数据...

[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; 
    } 

} 

任何帮助/指针将不胜感激。谢谢!

回答

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小伙子,并且几乎是黑了我的方式。

感谢您的帮助。

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”),然后将其写入请求流。

+0

感谢Talljoe和L.B ...这是我试过的.. string url =“https://api.parse.com/1/login?” + HttpUtility.UrlEncode(“username = a&password = a”); var httpWebRequest =(HttpWebRequest)WebRequest.Create(url); httpWebRequest.ContentType =“application/x-www-form-urlencoded”; httpWebRequest.Headers.Add(“X-Parse-Application-Id:App Id”); httpWebRequest.Headers.Add(“X-Parse-REST-API-Key:Rest Key”); 我得到了401未经授权的错误。我在代码中从你们两个人的意见中做错了什么? – DevIntern 2012-03-11 01:44:23

+0

我复制了上面的代码来设置标题,但它不正确。我应该检查它。我更新了上面的代码,以显示如何正确添加身份验证标头。 – Talljoe 2012-03-11 03:00:35

相关问题