Not able to get access_token through code from salesforce

Not able to get access_token through code from salesforce. It throws 400 status code error. Below are details:
Code Used:
String clientId = ‘’;
String clientSecret = ‘’;
String refreshToken = ‘’;

     String httpBody = '{' +
            '"data": {' +
            '"grant_type": "refresh_token",' +
            '"client_id": "'+EncodingUtil.urlEncode(clientId,'UTF-8')+'",' +
            '"scope": "default",' +
            '"client_secret": "'+EncodingUtil.urlEncode(clientSecret,'UTF-8')+'",' +
            '"refresh_token": "'+EncodingUtil.urlEncode(refreshToken,'UTF-8')+'"}}';
    System.debug('body: ' + httpBody);
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://app.asana.com/-/oauth_token');
    request.setMethod('POST');
    request.setHeader('Content-Type', 'application/x-www-form-urlencoded');		
	
	Outcome/Error:		
	Status: 400 Bad Request
	
	{
	"error": "unsupported_grant_type",
	"error_description": "The supported grant types are `authorization_code` and `refresh_token`."
	}

Changing body in below format fixed issue.
String payload = ‘client_id=’+EncodingUtil.urlEncode(clientId,‘UTF-8’)+‘&client_secret=’+EncodingUtil.urlEncode(clientSecret,‘UTF-8’)+‘&refresh_token=’+EncodingUtil.urlEncode(refreshToken,‘UTF-8’)+‘&grant_type=refresh_token’;
request.setBody(payload);

1 Like