Please Help: SmartApp Synchronous External HTTP to SalesForce - Bad Request


(Lawrence Lutton) #1

SalesForce.com is returning “Bad Request” for RESTful login requests from a SmartApp.
The same request works from curl.
I have tried many variations and carefully compared the curl request to corresponding SmartApp request using RequestBin.

What am I doing wrong?

def login() {

log.debug "login called."

/* 
This code is based upon documentation at
		http://docs.smartthings.com/en/latest/smartapp-developers-guide/calling-web-services-in-smartapps.html
	and answer from Mohith Shrivastava at
		http://salesforce.stackexchange.com/questions/5925/oauth-2-0-username-password-flow-problem-unsupported-grant-type.
*/

String reqbody = 'grant_type=password' +
	'&client_id=' + appSettings.client_id +
	'&client_secret=' + appSettings.client_secret +
    '&username=' + appSettings.username +
    '&password=' + appSettings.password + appSettings.security_token

// Send request to RequestBin for inspection.

def params = [
    uri: 'http://requestb.in/',
    path: appSettings.requestBin,
    requestContentType: 'application/x-www-form-urlencoded',
    body: reqbody
    ]

log.debug "params: $params"

try {
	httpPostJson (params) {resp ->
    	log.debug "RequestBin succesful."
		}            
} catch (e) {
	log.error "RequestBin error: $e"
}

// Send the request to SalesForce.

params = [
    uri: 'https://login.salesforce.com/',
    path: 'services/oauth2/token',
    requestContentType: 'application/x-www-form-urlencoded',
    body: reqbody
    ]

log.debug "params: $params"

try {
	httpPostJson (params) {resp ->
        log.debug "todo: Parse SalesForce response and return access token."
		}            
} catch (e) {
	log.error "login error: $e"
}

}

b5dfd35d-9491-4c85-b923-ab4ed9dba888 10:08:12 AM: error login error: groovyx.net.http.HttpResponseException: Bad Request
b5dfd35d-9491-4c85-b923-ab4ed9dba888 10:08:12 AM: debug params: [uri:https://login.salesforce.com/, path:services/oauth2/token, requestContentType:application/x-www-form-urlencoded, body:grant_type=password&client_id=<client_id>&client_secret=<client_secret>&username=&password=4RXqSl3bdyFsjZqZSC2Fls60]
b5dfd35d-9491-4c85-b923-ab4ed9dba888 10:08:12 AM: debug RequestBin succesful.
b5dfd35d-9491-4c85-b923-ab4ed9dba888 10:08:08 AM: debug params: [uri:http://requestb.in/, path:15wvsgu1, requestContentType:application/x-www-form-urlencoded, body:grant_type=password&client_id=<client_id>&client_secret=<client_secret>&username=&password=4RXqSl3bdyFsjZqZSC2Fls60]
b5dfd35d-9491-4c85-b923-ab4ed9dba888 10:08:08 AM: debug login called.


(Andreas Skotner) #2

Were you ever able to solve this?