2010-12-10 26 views
0

私は窓からのアクセストークンを取得しようとしているは、リモートサーバーがエラーを返しました:(401)無許可

文字列requestUrl =「https://consent.live.comこのコードを使用してAPIを接続して生きます/AccessToken.aspx "; (401)無許可:

 // Request the access token. 
     string postData = string.Format("{0}?wrap_client_id={1}&wrap_client_secret={2}&wrap_callback={3}&wrap_verification_code={4}&idtype={5}", 
       requestUrl, 
       "000000004C039809", 
       "l4VJekL1vFL1iFVmcP5qLkWv9ukY4mdl", 
       "http://ewshops.com", 
       "dac5d71d-d640-30d1-ebed-3576b132b3ec", 
       "cid"); 
     byte[] postDataEncoded = System.Text.Encoding.UTF8.GetBytes(postData); 

     WebRequest req = HttpWebRequest.Create(requestUrl); 
     req.Method = "POST"; 
     // req. 
     req.ContentType = "application/x-www-form-urlencoded"; 
     req.ContentLength = postDataEncoded.Length; 


     Stream requestStream = req.GetRequestStream(); 
     requestStream.Write(postDataEncoded, 0, postDataEncoded.Length); 

     WebResponse res = req.GetResponse(); 

     string responseBody = null; 

     using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8)) 
     { 
      responseBody = sr.ReadToEnd(); 
     } 

     // Process FORM POST. 
     NameValueCollection responseCollection = System.Web.HttpUtility.ParseQueryString(responseBody); 

     return responseCollection["wrap_access_token"]; 

が、私は次のエラー

リモートサーバーを受け取ってきましたがエラーを返しました。

+4

この質問のコードブロックの書式設定を少し修正して(たとえば、不要な空白行をすべて削除するなど)、読みやすくすることをお勧めしますか? [フォーマットのヘルプ](http://stackoverflow.com/editing-help)を参照してください – stakx

+1

私は投稿を再フォーマットしました – Hadad

答えて

2

レスポンスボディを表示するには、通常、より多くの情報が含まれています。また、urlに追加する前にurlencode http://ewshops.comを追加する必要があります。

+0

応答を取得しようとして例外が返されました。これはスタックトレースです: – Hadad

2

私は同じ問題を抱え、次のように修正しました。 requestUrl( "https://consent.live.com/AccessToken.aspx")とそれに続く "?"あなたのpostDataから。 POSTデータはx-www-form-urlencoded形式であり、要求URLは含まれていません。また、すべてのパラメータをHttpUtility.UrlEncode()します。

関連する問題