簡體   English   中英

遠程服務器返回錯誤:(401)未經授權

[英]The remote server returned an error: (401) Unauthorized

我正在嘗試通過使用此代碼從Windows Live Connect API獲取訪問令牌

字符串requestUrl =“ https://consent.live.com/AccessToken.aspx”;

        // 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"];

但我收到以下錯誤

遠程服務器返回錯誤:(401)未經授權。

向我們顯示響應正文,它通常包含更多信息。 在將其添加到uri之前,您還應該urlencode http://ewshops.com

我遇到了同樣的問題,並按以下步驟進行修復。 刪除requestUrl(“ https://consent.live.com/AccessToken.aspx”)和后續的“?” 從您的postData中。 POST數據應采用x-www-form-urlencoded格式,並且不包含請求URL。 也是HttpUtility.UrlEncode()的所有參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM