Page 1 of 1

HTTPS Authentication with Salesforce

Posted: Tue Aug 18, 2020 2:44 am
by phando
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.

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.
How would I edit the HTTPClient.cpp to dump that buffer data to the terminal?

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");
  }

Re: HTTPS Authentication with Salesforce

Posted: Tue Aug 18, 2020 3:19 am
by boarchuz
You want to get the response for httpCode 400 as well...

Code: Select all

if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY || httpCode ==
HTTP_CODE_BAD_REQUEST)

Re: HTTPS Authentication with Salesforce

Posted: Tue Aug 18, 2020 3:39 am
by phando
Thank you for the quick response and finding my error!

By adding the 400 status to the codes, I was able to see:

Code: Select all

{"error":"unsupported_grant_type","error_description":"grant type not supported"}
A quick trip to google produced the need for a content type specification

Code: Select all

https.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
It works! :D