DrakoPD
DrakoPD

Reputation: 172

How to Authenticate to GitHub with a token on ESP8266?

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.

Upvotes: 0

Views: 837

Answers (1)

romkey
romkey

Reputation: 7069

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.

Upvotes: 2

Related Questions