ESP8266HTTP Authentication

Hi!

Im trying to communicate with a API through an ESP8266 and im having some troubles.

I am able to connect and parse the data from http://jsonplaceholder.typicode.com/users/1 using ArduinoJson Version 6 without any issues.

Im not able to authenticate myself against the API and get a "max int" return when i try to check the http code.

API Instructions:

HTTP POST or HTTP Basic Auth needs to be used to get the data.
Im trying to use HTTP Basic Auth.

"For HTTP Basic Auth you have to add the Authorization header with the request. The Authorization header is constructed as follows:"
Username and password are combined into a string username:password or if you use the api token it should be combined xxxx:api_token (xxx indicating user's personal token)
The resulting string literal is then encoded using Base64
The authorization method and a space i.e. "Basic " is then put before the encoded string.
For example, if the user agent uses 'Aladdin' as the username and 'open sesame' as the password then the header is formed as follows: Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

curl -u xxxx:api_token -X GET XXX Sex - Free Porn Videos at XXX.com

This is the part of my code that im using to create the request. I am able to make the request using curl in CMD.

//Used packages
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

uint16_t httpCode{};

HTTPClient http;

WiFi.begin(user, pass);


http.begin("URL");
http.addHeader("Authorization:", "Basic MyTokenInBase64", true);
httpCode = http.GET();

//Further down in code

deserializeJson(doc, http.getString(), DeserializationOption::Filter(filter));

I have tried to not Base64 encode the token and get the same problem. Ive also tried to use the ".setAuthorization(Basic MyTokenInBase64);" but it doesn't work.

I would appreciate any help since im new to HTTP requests. :slight_smile:

Also as a side note, whats does the boolean do in the "addHeader" function. The reference isn't really helpful in this case. void HTTPClient::addHeader ( const String & name, const String & value, bool first = false )

Im not sure if i should add this to the original post or make a new comment so let me know if i made the wrong choice!

Ive been reading and testing all day and ive figured out that ive got a bigger problem then just Authenticating/ using the API token.

Whenever i try to make a HTTP GET request to a HTTPS address it always ends up returning "MAX INT" to the GET request. Ive tried to use a HTTPS fingerprint (followed the HTTPS client example) but it doesn't change anything.
I cant find anything on the internet regarding this and this subject is completely new to me.

Below are the added lines ive tried/example request.

#include <WiFiClientSecureBearSSL.h>

const uint8_t fingerprint[20] = {0x40, 0xaf, 0x00, 0x6b, 0xec, 0x90, 0x22, 0x41, 0x8e, 0xa3, 0xad, 0xfa, 0x1a, 0xe8, 0x25, 0x41, 0x1d, 0x1a, 0x54, 0xb3};


std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);

client->setFingerprint(fingerprint);

http.begin(client, "https://jigsaw.w3.org/HTTP/connection.html");

I just want to add that the problem is solved.
I was using the wrong fingerprint the whole time. Added the right fingerprint from the website and it worked just fine.