user23803035
user23803035

Reputation: 11

How to get access token correctly from SAP Successfactors api?

I keep receiving errors:

The requested operation is not available. If you continue to experience problems, please contact your administrator

I was provided following details

  1. SF host: https://example.com/learning
  2. Client id and secret
  3. Api: /example/odatav4/v1/$metadata

I want to retrieve data from given api, but I think I need an access token. Based on this solution, I write this C# code:

static async Task<string> GetAccessToken()
{
    var TokenUrl = "https://example.com/learning/oauth/token"; // Token URL

    var ClientID = "..."; // Client ID
    var ClientSecret = "..."; // Client Secret
    var Encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ClientID}:{ClientSecret}"));
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Encoded);
        var content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");
        var response = await client.PostAsync(TokenUrl, content);
        if (!response.IsSuccessStatusCode)
        {
            string errorContent = await response.Content.ReadAsStringAsync();
            Console.WriteLine(errorContent);
            throw new Exception($"HTTP error! status: {response.StatusCode}");
        }

        var Response = await response.Content.ReadAsStringAsync();
        return Response;
    }

Upvotes: 1

Views: 325

Answers (0)

Related Questions