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
crtest1crtest1 

grant type not supported

Hi,

 

i followed the instruction in http://wiki.developerforce.com/index.php/Digging_Deeper_into_OAuth_2.0_on_Force.com to get refresh_code. I tried two approached. First is in rails code, the other is in a self-submitted html form. The first leads to the second approach because it fails.

 

I don't know what's wrong with the codes in rails that leads to the failure. I really appreciate it if you can help me out.

 

Following are ruby codes to make authorization code request. It returns "{"error":"unsupported_grant_type","error_description":"grant type not supported"}"

 

oauth_url = URI.parse('https://login.salesforce.com/services/oauth2/token')
          
    http = Net::HTTP.new(oauth_url.host, oauth_url.port)
    http.use_ssl = (oauth_url.scheme == 'https')
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    
    req = Net::HTTP::Post.new('https://login.salesforce.com/services/oauth2/token')
    req["Accept"] = '*/*'
    req.content_type = "text/html"
    form_data = {'code' => 'sfdc generated code...', 'grant_type' => 'authorization_code', 'client_id' => "my client id", 'client_secret' =>  "my client secret", 'redirect_uri' => "https://localhost/sfdc/oauth_callback"}
        
    req.set_form_data(form_data, ';')
    
    begin
      logger.info "Make request: #{req.path}"
      response = Timeout::timeout(10){
        http.start do
          http.request req
        end
      }
    rescue Exception => e
      logger.error "make_http_request:  request failed: #{e}"        
      #return issue_http_request(req, tries + 1)
    end
    
    puts response.body

 

Following is the resulf from the self-submitted form.

<html><body onload="document.forms[0].submit()"><p><script language="javascript">document.write("Please wait ...");</script></p>
<noscript><p>Note: Your browser does not support JavaScript or it is turned off. Press the button to proceed.</p></noscript>
<form method="post" action="https://login.salesforce.com/services/oauth2/token">
<input type="hidden" name="code" value="sfdc generated code..."/><input type="hidden" name="grant_type" value="authorization_code"/>
<input type="hidden" name="client_id" value="my client id"/>
<input type="hidden" name="client_secret" value="my client secret"/>
<input type="hidden" name="redirect_uri" value="https://localhost/sfdc/oauth_callback"/><noscript>
<input type="submit" value="Continue"/></noscript></form></body></html>

 

{
    "id":"https://login.salesforce.com/id/00D50000000IZ3ZEAW/00550000001fg5OAAQ",
    "issued_at":"1296458209517",
   "refresh_token":"5Aep862eWO5D.7wJBuW5aaARbbxQ8hssCnY1dw3qi59o1du7ob.lp23ba_3jMRnbFNT5R8X2GUKNA==",
    "instance_url":"https://na1.salesforce.com",
    "signature":"0/1Ldval/TIPf2tTgTKUAxRy44VwEJ7ffsFLMWFcNoA=",
   "access_token":"00D50000000IZ3Z!AQ0AQDpEDKYsn7ioKug2aSmgCjgrPjG9eRLza8jXWoW7uA90V39rvQaIy1FGxjFHN1ZtusBGljncdEi8eRiuit1QdQ1Z2KSV"
}

 

Thanks,

Michael

Best Answer chosen by Admin (Salesforce Developers) 
crtest1crtest1

i got it working! Here are my codes.

 

    form_data = 'code=' << CGI::escape(params[:code]) 
    form_data << '&grant_type=' << CGI::escape('authorization_code') 
    form_data << '&client_id=' << CGI::escape(sfdc_state["client_id"]) 
    form_data << '&client_secret=' << CGI::escape(sfdc_state["client_secret"]) 
    form_data << '&redirect_uri=' << CGI::escape('https://localhost/sfdc/oauth_callback')
    
    oauth_url = URI.parse('https://login.salesforce.com')
          
    http = Net::HTTP.new(oauth_url.host, oauth_url.port)
    http.use_ssl = (oauth_url.scheme == 'https')
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    resp, data = http.post2('/services/oauth2/token', form_data, nil)
    
    logger.info "Data sent to SFDC: #{form_data} for user: #{sfdc_state["user_id"]}"
    puts resp.body

 

All Answers

SuperfellSuperfell

req.content_type = "text/html" is not right, it should be

req.content_type = "application/x-www-form-urlencoded"

 
crtest1crtest1

i thought it could be because of it. It doesn't work even though i changed it to 'text/html'. it returns me same error.

crtest1crtest1

i got it working! Here are my codes.

 

    form_data = 'code=' << CGI::escape(params[:code]) 
    form_data << '&grant_type=' << CGI::escape('authorization_code') 
    form_data << '&client_id=' << CGI::escape(sfdc_state["client_id"]) 
    form_data << '&client_secret=' << CGI::escape(sfdc_state["client_secret"]) 
    form_data << '&redirect_uri=' << CGI::escape('https://localhost/sfdc/oauth_callback')
    
    oauth_url = URI.parse('https://login.salesforce.com')
          
    http = Net::HTTP.new(oauth_url.host, oauth_url.port)
    http.use_ssl = (oauth_url.scheme == 'https')
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    resp, data = http.post2('/services/oauth2/token', form_data, nil)
    
    logger.info "Data sent to SFDC: #{form_data} for user: #{sfdc_state["user_id"]}"
    puts resp.body

 

This was selected as the best answer