2012-03-10 6 views
3

ユーザーがログインしてデータを表示できるようにするWebページを作成しようとしています。データは、Parse.com上でホストされ、REST APIとして公開されます。.Net/Parse.comでAPI資格情報とユーザー名/パスワードを送信するにはどうすればよいですか? (PHPからC#へ)

私はasp.net/C#を使用してアクセスしており、APIキーとアプリケーションキーを使用してアクセスできます。しかし、私はこのPHPコードのバージョンをC#のドキュメントから作成する必要があります。

これを行うには、URLエンコードされたパラメータとしてユーザー名とパスワードを使用して/ 1/loginエンドポイントにGETリクエストを送信します:今、私はここで立ち往生しています...私は返しますように、このコードのような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#のコードがある

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 

すべてのダ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

最初のコードサンプルには2つの問題があります。まず、HTTP認証を使用せずに、資格情報をヘッダーとして渡す必要があります。

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

第2に、パラメーターをヘッダーではなく呼び出しとしてデータとして渡す必要があります。そのためには、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 Unauthorizedエラーが発生しました。私は自分のコードで何か間違っているのですか? – DevIntern

+0

ヘッダを設定するために上記のコードをコピーしましたが、間違っていました。私はそれを確認したはずです。上記のコードを更新して、認証ヘッダーを正しく追加する方法を示しました。 – Talljoe

関連する問題