HTTPS Authentication with Salesforce
Posted: Tue Aug 18, 2020 2:44 am
Have you been able to authenticate with Salesforce OAuth?
I have been working with the sample code and can reliably pass a certificate and make HTTPS requests. The request I need to make is a to get an auth token from a Salesforce Org. The request has additional data not shown in any examples I can find. I pass four variables as the end of the url. It seems like a GET to me, but POST is the only command that takes arguments. Please look at the code below at let me know if it looks right to you. My requests are successfully connecting but resulting in 400 errors.
How would I edit the HTTPClient.cpp to dump that buffer data to the terminal?
I have been working with the sample code and can reliably pass a certificate and make HTTPS requests. The request I need to make is a to get an auth token from a Salesforce Org. The request has additional data not shown in any examples I can find. I pass four variables as the end of the url. It seems like a GET to me, but POST is the only command that takes arguments. Please look at the code below at let me know if it looks right to you. My requests are successfully connecting but resulting in 400 errors.
Code: Select all
D][HTTPClient.cpp:1025] connect(): connected to login.salesforce.com:443
[D][HTTPClient.cpp:1158] handleHeaderResponse(): code: 400
[D][HTTPClient.cpp:1165] handleHeaderResponse(): Transfer-Encoding: chunked
[HTTPS] POST... code: 400
[D][HTTPClient.cpp:361] disconnect(): still data in buffer (92), clean up.
Code: Select all
WiFiClientSecure *client = new WiFiClientSecure;
if(client) {
client -> setCACert(rootCACertificate);
{
// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is
HTTPClient https;
Serial.print("[HTTPS] begin...\n");
if(https.begin(*client,"login.salesforce.com",443,"/services/oauth2/token",true)){
Serial.print("[HTTPS] POST...\n");
String httpRequestData = "grant_type=password&client_id="+ clientID +"&client_secret="+ clientSS +"&username="+ userID +"&password="+ userPW;
Serial.println(httpRequestData);
int httpCode = https.POST(httpRequestData);
}
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] POST... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = https.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
// End extra scoping block
}
delete client;
} else {
Serial.println("Unable to create client");
}