void sendTokenRequest(String clientId, String clientSecret, String code) { // Replace with your Strava API endpoint const char* host = "www.strava.com"; const int httpsPort = 80; // Establish a connection to the server WiFiClientSecure client; if (!client.connect(host, httpsPort)) { Serial.println("Connection failed!"); return; } // Create the GET request String url = "/oauth/token?client_id=" + clientId + "&client_secret=" + clientSecret + "&code=" + code + "&grant_type=authorization_code"; // Send the HTTP request client.print("GET " + url + " HTTP/1.1\r\n"); client.print("Host: " + String(host) + "\r\n"); client.print("Connection: close\r\n\r\n"); Serial.println("Request sent!"); // Wait for the response while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { Serial.println("Headers received"); break; } } // Print the response Serial.println("Response:"); while (client.available()) { char c = client.read(); Serial.print(c); } Serial.println(); // Close the connection client.stop(); } /* //###########POST REQUEST to refresh token################# void requestRefreshToken(String clientId, String clientSecret, String refreshToken) { const char* host = "www.strava.com"; const int httpsPort = 80; WiFiClientSecure client; if (!client.connect(host, httpsPort)) { Serial.println("Connection failed!"); return; } // Create the POST request String url = "/oauth/token?client_id=" + clientId + "&client_secret=" + clientSecret + "&refresh_token=" + refreshToken + "&grant_type=refresh_token"; HTTPClient http; http.begin(client, host, httpsPort, url); // Send the POST request int httpCode = http.POST(""); if (httpCode > 0) { if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println("Response: " + payload); // Parse JSON response and store tokens if (parseJsonResponse(payload)) { Serial.println("Tokens retrieved successfully!"); Serial.println("Access Token: " + accessToken); Serial.println("Refresh Token: " + refreshToken); } else { Serial.println("Failed to parse JSON response!"); } } else { Serial.println("HTTP request failed with error code: " + String(httpCode)); } } else { Serial.println("HTTP request failed!"); } // Close the connection http.end(); } bool parseJsonResponse(String json) { // Parse JSON and extract tokens if (json.indexOf("access_token") == -1 || json.indexOf("refresh_token") == -1) { Serial.println("JSON does not contain access_token and refresh_token!"); return false; } accessToken = getValueFromJson(json, "access_token"); refreshToken = getValueFromJson(json, "refresh_token"); return true; } String getValueFromJson(String json, String key) { int keyIndex = json.indexOf(key); if (keyIndex != -1) { int startIndex = json.indexOf('"', keyIndex); int endIndex = json.indexOf('"', startIndex + 1); return json.substring(startIndex + 1, endIndex); } return ""; } //###########POST REQUEST to upload the file################# void uploadActivity(String filename, String access_token) { const char* host = "www.strava.com"; const int httpsPort = 443; WiFiClientSecure client; if (!client.connect(host, httpsPort)) { Serial.println("Connection failed!"); return; } // Set the form data String formData = "--boundary\r\n" "Content-Disposition: form-data; name=\"activity_type\"\r\n\r\nride\r\n" "--boundary\r\n" "Content-Disposition: form-data; name=\"name\"\r\n\r\nTest Ride\r\n" "--boundary\r\n" "Content-Disposition: form-data; name=\"description\"\r\n\r\nTest description\r\n" "--boundary\r\n" "Content-Disposition: form-data; name=\"trainer\"\r\n\r\n0\r\n" "--boundary\r\n" "Content-Disposition: form-data; name=\"commute\"\r\n\r\n0\r\n" "--boundary\r\n" "Content-Disposition: form-data; name=\"data_type\"\r\n\r\ngpx\r\n" "--boundary\r\n" "Content-Disposition: form-data; name=\"file\"; filename=\"INPUT_RIDE_NUMBER_HERE.gpx\"\r\n" "Content-Type: application/octet-stream\r\n\r\n"; String endData = "\r\n--boundary--\r\n"; // Open a connection to the server client.println("POST /api/v3/uploads HTTP/1.1"); client.println("Host: www.strava.com"); client.println("Authorization: Bearer " + access_token); client.println("Content-Type: multipart/form-data; boundary=boundary"); client.print("Content-Length: "); client.println(formData.length() + endData.length()); // Send the request client.print(formData); // Open the file and send its contents File file = SD.open(filename, FILE_READ); if (file) { while (file.available()) { client.write(file.read()); } file.close(); } client.print(endData); // Read and print the response while (client.connected()) { String line = client.readStringUntil('\n'); Serial.println(line); } // Close the connection client.stop(); } */