function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lonedeveloperlonedeveloper 

OAuth server returning unsupported_grant_type

Hi,

 

We implemented OAuth 2.0 using Web Server Authentication Flow. It was working fine in October/November but all of a sudden it has stopped working. Whenever we try authorising a new client the server return (400) Bad Request with the body

 

{"error":"unsupported_grant_type","error_description":"grant type not supported"}

 

grant_type is set as authorization_code which is definitely valid.

 

Is there any reason why OAuth would suddenly stop working?

 


This is how we have implemented OAuth:

 

First user is directed to: https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=blah.id&redirect_uri=https://domain.com/Web/Salesforce/Callback.aspx&scope=api%20refresh_token

 

User is prompted by Salesforce to login to their account.

 

Once user is authenticated Salesforce calls Callback.aspx, Callback.aspx requests refresh token on behalf of the client by making a POST request to: https://login.salesforce.com/services/oauth2/token with the payload:

grant_type=authorization_code&code=blah.code&client_id=blah.Id&client_secret=11111111&redirect_uri=https://domain.com/Web/Salesforce/Callback.aspx

 

Content type is definitely: application/x-www-form-urlencoded

 

Many thanks,

Best Answer chosen by Admin (Salesforce Developers) 
lonedeveloperlonedeveloper

I forgot to provide update but after lot of fiddling around with fiddler figured out there was a space before grant_type=authorization_code in HTTP POST payload that was causing the issue.

 

Interestingly that space has been there in code base since July and this issue was first noticed on 14th Jan. Have no idea why it was working all this time and stopped working last week. 

All Answers

willywuwillywu

Are you still seeing this issue?  Do you have a way to reproduce it (i.e. does it only happen for users on a certain instance) or maybe it only happens on sandbox?

lonedeveloperlonedeveloper

I forgot to provide update but after lot of fiddling around with fiddler figured out there was a space before grant_type=authorization_code in HTTP POST payload that was causing the issue.

 

Interestingly that space has been there in code base since July and this issue was first noticed on 14th Jan. Have no idea why it was working all this time and stopped working last week. 

This was selected as the best answer
gitugitu
Hi LoneDeveloper,

I am trying to implement web server authentication flow as well but it is giving me bad url error. Are you able to share your sample code? 
This is what I have and it works for password flow but not for server flow. So commented line gives me bad url error but the first one works.

string username = 'a@b.com';
                string password = 'xyz';
                string ConsumerKey = '6767MVG98dostKihXN6WNfkGnEXURWKSDa3uuCOB1.ZivLC5t';
                string clientId = '11111';
                string URI = 'https://cs1.salesforce.com/services/oauth2/token?grant_type=password&' + 'client_id=' + ConsumerKey + '&' + 'client_secret=' + clientId + '&' + 'username=' + username + '&' + 'password=' + password;
              // string URI = 'https://cs1.salesforce.com/services/oauth2/authorize?response_type=token&' + 'client_id=' + ConsumerKey + '&redirect_uri=https://na11.salesforce.com';
               

                HttpRequest req = new HttpRequest();
                req.setEndpoint(URI);
                req.setMethod('POST');
                Http http = new Http();
                HTTPResponse res = http.send(req);
                System.debug('*** After Authentication...'+res.getBody());