public async Task GetTokenRSharp() { //cred var clientId = "Krystian-testDemo-SBX-dbcdef085-a5b59954"; var clientSecret = "SBX-bcdef085ddea-bd1a-4437-80e0-ee08"; var credentials = $"{clientId}:{clientSecret}"; var credentialsByte = System.Text.Encoding.UTF8.GetBytes(credentials); var credentialsB64 = System.Convert.ToBase64String(credentialsByte); //body var scope = "https://api.ebay.com/oauth/api_scope"; var contentDict = new Dictionary <string, string>(); contentDict.Add("grant_type", "client_credentials"); contentDict.Add("scope", HttpUtility.UrlEncode(scope)); var client = new RestClient("https://api.sandbox.ebay.com/identity/v1/oauth2/token"); var request = new RestRequest(Method.POST); request.AddHeader("Authorization", $"Basic {credentialsB64}"); request.AddParameter("application/x-www-form-urlencoded", CreateRequestPayload(contentDict), ParameterType.RequestBody); IRestResponse response = client.Execute(request); OAuthApiResponse apiResponse = JsonConvert.DeserializeObject <OAuthApiResponse>(response.Content); token = apiResponse.AccessToken; }
private OAuthResponse HandleApiResponse(IRestResponse response, TokenType tokenType) { OAuthResponse oAuthResponse = new OAuthResponse(); if (response.StatusCode != HttpStatusCode.OK) { oAuthResponse.ErrorMessage = response.Content; log.Error("Error in fetching the token. Error:" + oAuthResponse.ErrorMessage); } else { OAuthApiResponse apiResponse = JsonConvert.DeserializeObject <OAuthApiResponse>(response.Content); //Set AccessToken OAuthToken accessToken = new OAuthToken { Token = apiResponse.AccessToken, ExpiresOn = DateTime.Now.Add(new TimeSpan(0, 0, apiResponse.ExpiresIn)), TokenType = tokenType }; oAuthResponse.AccessToken = accessToken; //Set Refresh Token if (apiResponse.RefreshToken != null) { OAuthToken refreshToken = new OAuthToken { Token = apiResponse.RefreshToken, ExpiresOn = DateTime.Now.Add(new TimeSpan(0, 0, apiResponse.RefreshTokenExpiresIn)), }; oAuthResponse.RefreshToken = refreshToken; } } log.Info("Fetched the token successfully from API"); return(oAuthResponse); }