首页 > 解决方案 > 如何使用 ESP8266 上的令牌向 GitHub 进行身份验证?

问题描述

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

我已经配置了证书和 NTP 时间,我不会放这个代码,因为它可以工作。但这里是我用作 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");

我从来没有达到更新功能,因为我无法验证。

标签: githubesp8266arduino-esp8266

解决方案


看看你的代码。您正在设置,client但您从不使用它。您需要将它传递给http.begin()这样的:

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

否则 HTTP 不知道要使用BearSSL::WiFiClientSecure您费尽心思设置的那个。


推荐阅读