【问题标题】:How do I send API credentials and username / password with .Net / Parse.com? (PHP to C#)如何使用 .Net / Parse.com 发送 API 凭据和用户名/密码? (PHP 到 C#)
【发布时间】:2012-03-11 15:51:29
【问题描述】:

我正在尝试创建一个网页,允许我的用户登录并查看他们的数据。数据托管在 Parse.com 上,将其公开为 REST API。

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

为此,向 /1/login 端点发送一个 GET 请求,并将用户名和密码作为 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# 代码...但我只想要尝试登录的用户的数据...

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

}

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

【问题讨论】:

    标签: c# asp.net .net httpwebrequest


    【解决方案1】:

    我发现您的第一个代码示例有两个问题。首先,您需要将凭据作为标头传递,而不是使用 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”),然后将其写入请求流。

    【讨论】:

    • 感谢 Talljoe 和 L.B...这是我尝试过的... string url = "api.parse.com/1/login?" + HttpUtility.UrlEncode("用户名=a&密码=a"); var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.ContentType = "应用程序/x-www-form-urlencoded"; httpWebRequest.Headers.Add("X-Parse-Application-Id:App Id"); httpWebRequest.Headers.Add("X-Parse-REST-API-Key:Rest Key");我收到 401 未经授权的错误。从你们俩所说的来看,我在代码中做错了吗?
    • 我复制了上面的代码来设置标题,但它不正确。我应该检查一下。我已经更新了上面的代码,以展示如何正确添加身份验证标头。
    【解决方案2】:

    @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 的人,而且几乎是用我的方式来解决这个问题的。

    谢谢你帮助我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多