0

I can update from a public repo, now I want to authenticate to GitHub with a token, for download a firmware of a private repo, but I get Connection refused (error code -1).

I had configured certs and NTP time already, I won't put the code of this because it works. But here the code that I'm using as base.

here is my code

    BearSSL::WiFiClientSecure client;
    bool mfln = client.probeMaxFragmentLength("https://api.github.com", 443, 1024);  // server must be the same as in ESPhttpUpdate.update()
    Serial.printf("MFLN supported: %s\n", mfln ? "yes" : "no");
    if (mfln) {
       client.setBufferSizes(1024, 1024);
    }
    client.setCertStore(&certStore);
    HTTPClient http;

    http.begin("https://api.github.com/repos/user/reponame/contents/firmware.bin");
    http.addHeader("Accept", "application/vnd.github.v3+json");
    http.addHeader("authorization", "Bearer token");
    //http.setAuthorization("token");

    Serial.print("[HTTP] GET...\n");
    // start connection and send HTTP header
    int httpCode = http.GET();

    // httpCode will be negative on error
    if (httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] GET... code: %d\n", httpCode);

      // file found at server
      if (httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        Serial.println(payload);
      }
    } else {
      Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      return;
    }

    ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW);

    t_httpUpdate_return ret = ESPhttpUpdate.update(client, "https://api.github.com/repos/user/reponame/contents/firmware.bin");

I never reach the update function, because I can't authenticate.

DrakoPD
  • 161
  • 1
  • 13

1 Answers1

2

Take a look at your code. You're setting up client but you're never using it. You need to pass it to http.begin() like so:

http.begin(client, "https://github.com/user/reponame");

Otherwise HTTP doesn't know to use the BearSSL::WiFiClientSecure that you went to all that trouble to setup.

romkey
  • 6,218
  • 3
  • 16
  • 12
  • Thanks, I connected perfectly to GitHub with `http.GET()`, the problem now is `httpupdate` function, can't find any file, I get the next error: `HTTP_UPDATE_FAILED Error (-102): File Not Found (404)`, aparently, the connection method of httpupdate is diferent from httpclient. I need to transfer the the parameters or the file that I download with GET(). – DrakoPD Jun 15 '20 at 21:57
  • Please post that as a separate question. That allows other people who have a similar problem to the one you originally posted about to find this. You can accept this answer as it solved your problem, post a question about the new problem and then accept the answer that solves that problem. – romkey Jun 16 '20 at 00:04