WItam , moze mi ktos pomoze , serwer otomoto zwraca mi blad 403 .Podsylam kod skromnego programu
using (WebClient client = new WebClient())
{
var querystring = new System.Collections.Specialized.NameValueCollection();
querystring.Add("client_id:", "****");
querystring.Add("client_secret", "******");
querystring.Add("grant_type", "password");
querystring.Add("username", "*****");
querystring.Add("password", "*****");
byte[] responsebytes = client.UploadValues("https://www.otomoto.pl/api/open/oauth/token", "POST", querystring);
string result = System.Text.Encoding.UTF8.GetString(responsebytes);
}
dodaj nagłówek:
User-Agent
Przykładowe wywołanie u mnie:
client = new RestClient(this.otomotoConfig.ApiUrl);
client.AddDefaultHeader("User-Agent", this.otomotoConfig.UserEmail);
var request = new RestRequest("oauth/token", Method.Post);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", this.otomotoConfig.GrantType);
request.AddParameter("username", this.otomotoConfig.UserName);
request.AddParameter("password", this.otomotoConfig.Password);
var resp = await Post<OtomotoResponse>(request);
private async Task<T?> Post<T>(RestRequest request)
{
if (!(accessToken).Equals(string.Empty))
client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(accessToken, "Bearer");
else
client.Authenticator = new HttpBasicAuthenticator(this.otomotoConfig.ApiUserName, this.otomotoConfig.ApiKey);
var result = await client.ExecuteAsync<T>(request);
var content = result.Content;
var res = JsonConvert.DeserializeObject<T>(content!);
return res;
}
(ja u siebie uzywam RestSharpa, więc sprawdź jak to zrobić w WebCliencie).