如何在ESP8266上使用令牌对GitHub进行身份验证?

时间:2020-06-15 00:47:16

标签: github esp8266 arduino-esp8266

我可以从公共存储库进行更新,现在我想使用令牌向GitHub进行身份验证,以下载私有存储库的固件,但是我被拒绝连接(错误代码-1)。

我已经配置了证书和NTP时间,我不会放置它的代码,因为它可以工作。但是这里是code that I'm using as base

这是我的代码

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

由于无法进行身份验证,所以我从未使用过更新功能。

1 个答案:

答案 0 :(得分:2)

看看您的代码。您正在设置.row1 , .row1 td { background:red; } ,但从未使用过。您需要像这样将其传递给client

http.begin()

否则,HTTP不会使用http.begin(client, "https://github.com/user/reponame"); 来解决所有麻烦的设置。