allegro / allegro-api

Issue tracker and wiki for Allegro REST API
https://developer.allegro.pl/
210 stars 39 forks source link

401 auth problem #9252

Open agusikprogramusik opened 1 week ago

agusikprogramusik commented 1 week ago

Hey, I searched the forum for solution, but none of them answered to my problem. I want to create a console application that will transcribe some data into a local database as a part of my college task.

I had a problem with authorization at the beginning. This is code that was literally rewritten from the Python script included to the documentation. clientId and clientSecret are 100% valid. Application created on allegro sandbox.

I am getting System.Net.Http.HttpRequestException: 'Response status code does not indicate success: 401 (Unauthorized). in line that is checking success status code for response in GetDeviceCode method. Am I missing something?

private static async Task<dynamic> GetDeviceCode(string clientId, string clientSecret)
{
    var request = new HttpRequestMessage(HttpMethod.Post, "https://allegro.pl/auth/oauth/device");
    request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{clientId}:{clientSecret}")));

    var parameters = new[]
    {
        new KeyValuePair<string, string>("client_id", clientId)
    };

    request.Content = new FormUrlEncodedContent(parameters);

    HttpResponseMessage response = await client.SendAsync(request);
    response.EnsureSuccessStatusCode();

    var responseContent = await response.Content.ReadAsStringAsync();
    return JsonConvert.DeserializeObject(responseContent);
}

private static async Task<string> WaitForAccessToken(string clientId, string clientSecret, string deviceCode)
{
    while (true)
    {
        var tokenResponse = await GetAccessToken(clientId, clientSecret, deviceCode);
        if (tokenResponse != null && tokenResponse.access_token != null)
        {
            return tokenResponse.access_token;
        }
        await Task.Delay(5000);
    }
}

private static async Task<dynamic> GetAccessToken(string clientId, string clientSecret, string deviceCode)
{
    var request = new HttpRequestMessage(HttpMethod.Post, "https://allegro.pl/auth/oauth/token");
    request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{clientId}:{clientSecret}")));

    var parameters = new[]
    {
        new KeyValuePair<string, string>("grant_type", "urn:ietf:params:oauth:grant-type:device_code"),
        new KeyValuePair<string, string>("device_code", deviceCode)
    };

    request.Content = new FormUrlEncodedContent(parameters);

    HttpResponseMessage response = await client.SendAsync(request);
    if (response.IsSuccessStatusCode)
    {
        var responseContent = await response.Content.ReadAsStringAsync();
        return JsonConvert.DeserializeObject(responseContent);
    }
    else
    {
        return null;
    }
}
MartaNowaczyk commented 1 week ago

It is possible that you are using sandbox data, but trying to authorize on production. Try by using https://allegro.pl.allegrosandbox.pl/auth/oauth/device

stale[bot] commented 2 days ago

W tym wątku nie pojawiła się żadna nowa odpowiedź w ciągu 7 dni, dlatego automatycznie oznaczamy go jako przeterminowany. Jeśli w ciągu kolejnych 7 dni nie pojawi się żadna odpowiedź, wątek zostanie zamknięty. Dziękujemy za zaangażowanie w dyskusję i zachęcamy Cię do wypełnienia ankiety dotyczącej naszego wsparcia na forum.


There was no new reply in this thread within 7 days, therefore, we automatically marked it as expired. If no response is received within the next 7 days, this thread will be closed. Thank you for engaging in the discussion. We encourage you to complete our forum support survey.