简体   繁体   中英

401 Unauthorised Error Received

I am using the habbo api to check if a name is valid. I'm receiving a 401 Unauthorised Error.

Below is the code I'm using. It worked when I copied my Cookie header in chrome and added that as a header. But is there another way and an actual fix?

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            using(WebClient WebClient = new WebClient())
            {
                WebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
                MessageBox.Show(WebClient.DownloadString("https://www.habbo.com/api/user/avatars/check-name?name=123"));
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

The API https://www.habbo.com/api/user/avatars/check-name what you are referring wont load without proper authorization token, since that one is not public available.

To further test, use the public API https://www.habbo.com/api/public/users?name=

You will be able to get response without any issues.

The 401 error indicates that you need to add (basic) authentication to your HTTP request (and remove the cookie that you added):

String username = “username”;
String password = “password”;

String credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + “:” + password));
WebClient.Headers[HttpRequestHeader.Authorization] = “Basic ” + credentials;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM