OAuth2.0 token from external IdP using client_credentials grant_type

While configuring OAuth authentication to call external web services, I cannot find a way to set grant_type=client_credentials. Is there a way to make configuration use client_credentials instead of authorization_code grant_type?

I also tried to configure a web service to directly call the IdP access token endpoint passing the grant_type I need, but I was not able to find a way to send the body content-type in application/x-www-form-urlencoded format.

Do you have any suggestions to get the OAuth token using client_credentials?

Like 1

Like

1 comments
Best reply

Greetings!

There’s no way to get this using the basic method - it doesn’t support sending form requests.
You’ll need to write a task script for that.

It requires writing C# code that uses WebRequest to send the request.
For example:

public bool Execute(UserConnection userConnection)
{
    var tokenUrl = "https://your-creatio-instance.com/connect/token";
    var clientId = "your_client_id";
    var clientSecret = "your_client_secret";
    var request = (HttpWebRequest)WebRequest.Create(tokenUrl);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.Accept = "application/json";
    var postData = $"grant_type=client_credentials&client_id={HttpUtility.UrlEncode(clientId)}&client_secret={HttpUtility.UrlEncode(clientSecret)}";
    var data = Encoding.UTF8.GetBytes(postData);
    try
    {
        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }
        using (var response = (HttpWebResponse)request.GetResponse())
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            var responseText = reader.ReadToEnd();
            var tokenResponse = JsonConvert.DeserializeObject<OAuthTokenResponse>(responseText);
            // if you want you can do something with tokenResponse.access_token            
            return true;
        }
    }
    catch (WebException ex)
    {
        if (ex.Response != null)
        {
            using (var reader = new StreamReader(ex.Response.GetResponseStream()))
            {
                var errorText = reader.ReadToEnd();
                // Here also you can log the error or display it
                Console.WriteLine("OAuth error: " + errorText);
            }
        }
        else
        {
            Console.WriteLine("WebException without response: " + ex.Message);
        }
        return false;
    }
}

Regards, 
Orkhan

Greetings!

There’s no way to get this using the basic method - it doesn’t support sending form requests.
You’ll need to write a task script for that.

It requires writing C# code that uses WebRequest to send the request.
For example:

public bool Execute(UserConnection userConnection)
{
    var tokenUrl = "https://your-creatio-instance.com/connect/token";
    var clientId = "your_client_id";
    var clientSecret = "your_client_secret";
    var request = (HttpWebRequest)WebRequest.Create(tokenUrl);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.Accept = "application/json";
    var postData = $"grant_type=client_credentials&client_id={HttpUtility.UrlEncode(clientId)}&client_secret={HttpUtility.UrlEncode(clientSecret)}";
    var data = Encoding.UTF8.GetBytes(postData);
    try
    {
        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }
        using (var response = (HttpWebResponse)request.GetResponse())
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            var responseText = reader.ReadToEnd();
            var tokenResponse = JsonConvert.DeserializeObject<OAuthTokenResponse>(responseText);
            // if you want you can do something with tokenResponse.access_token            
            return true;
        }
    }
    catch (WebException ex)
    {
        if (ex.Response != null)
        {
            using (var reader = new StreamReader(ex.Response.GetResponseStream()))
            {
                var errorText = reader.ReadToEnd();
                // Here also you can log the error or display it
                Console.WriteLine("OAuth error: " + errorText);
            }
        }
        else
        {
            Console.WriteLine("WebException without response: " + ex.Message);
        }
        return false;
    }
}

Regards, 
Orkhan

Show all comments