@drakewurm1
Абсолютно ни в чем не разбираюсь

Ошибка «could not parse error», в чем причина и как исправить?

Нашел я код для esp, который должен в реальном времени отслеживать что я сейчас слушаю в spotify и выводить инфо о играющей сейчас песне. Для того что бы этот код работал мне нужен другой код, который даст мне токен. Беда в том, что код который должен вывести мне токен выдает следующее:

..scandone
state: 0 -> 2 (b0)
.state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 2
cnt 

connected with NewHome, channel 13
dhcp client start...
ip:192.168.1.176,mask:255.255.255.0,gw:192.168.1.1
.
Connected to NewHome
IP address: 192.168.1.176
MDNS responder started
HTTP server started
pm open,type:2 0
grant_type=authorization_code&code=%code%&redirect_uri=http%3A%2F%2F192.168.1.176%2Fcallback%2F&client_id=%id%&client_secret=%secret%
accounts.spotify.com
Connection failed
status Code-1
Could not parse error



В чем причина этой ошибки и как ее исправить?
код прилагаю здесь
/*******************************************************************
    Get Refresh Token from spotify, this is needed for the other
    examples.

    Instructions:

    - Put in your Wifi details, Client ID, Client secret and flash to the board
    - Do one of the following

    --- IF USING IP (ESP32 MDNS does not work for me)
    
    - Get the Ip Address from the serial monitor
    - Add the following to Redirect URI on your Spotify app "http://[ESP_IP]/callback/"
    e.g. "http://192.168.1.20/callback/" (don't forget the last "/")
    - Open browser to esp using the IP 
    
    --- IF USING MDNS

    - Search for "#define USE_IP_ADDRESS" and comment it out 
    - Add the following to Redirect URI on your Spotify app "http://arduino.local/callback/" 
    (don't forget the last "/")
    - Open browser to esp: http://arduino.local 
    
    -----------
    
    - Click the link on the webpage
    - The Refresh Token will be printed to screen, use this
      for SPOTIFY_REFRESH_TOKEN in other examples.

*  * = Affiliate
*******************************************************************/

// ----------------------------
// Standard Libraries
// ----------------------------

#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#endif

#include <WiFiClient.h>
#include <WiFiClientSecure.h>

// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------

#include <SpotifyArduino.h>
// Library for connecting to the Spotify API

// Install from Github
// https://github.com/witnessmenow/spotify-api-arduino

// including a "spotify_server_cert" variable
// header is included as part of the SpotifyArduino libary
#include <SpotifyArduinoCert.h>

#include <ArduinoJson.h>
// Library used for parsing Json from the API responses

// Search for "Arduino Json" in the Arduino Library manager
// https://github.com/bblanchon/ArduinoJson

//------- Replace the following! ------

char ssid[] = "SSID";         // your network SSID (name)
char password[] = "PASSWORD"; // your network password

char clientId[] = "xxx";     // Your client ID of your spotify APP
char clientSecret[] = "xxx"; // Your client Secret of your spotify APP (Do Not share this!)

char scope[] = "user-read-playback-state%20user-modify-playback-state";

#define USE_IP_ADDRESS 1 

#ifdef USE_IP_ADDRESS
char callbackURItemplate[] = "%s%s%s";
char callbackURIProtocol[] = "http%3A%2F%2F"; // "http://"
char callbackURIAddress[] = "%2Fcallback%2F"; // "/callback/"
char callbackURI[100];
#else
char callbackURI[] = "http%3A%2F%2Farduino.local%2Fcallback%2F";
#endif

//------- ---------------------- ------

#if defined(ESP8266)
ESP8266WebServer server(80);
#elif defined(ESP32)
WebServer server(80);
#endif

WiFiClientSecure client;
SpotifyArduino spotify(client, clientId, clientSecret);

const char *webpageTemplate =
    R"(
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  </head>
  <body>
    <div>
     <a href="https://accounts.spotify.com/authorize?client_id=%s&response_type=code&redirect_uri=%s&scope=%s">spotify Auth</a>
    </div>
  </body>
</html>
)";

void handleRoot()
{
  char webpage[800];
  sprintf(webpage, webpageTemplate, clientId, callbackURI, scope);
  server.send(200, "text/html", webpage);
}

void handleCallback()
{
  String code = "";
  const char *refreshToken = NULL;
  for (uint8_t i = 0; i < server.args(); i++)
  {
    if (server.argName(i) == "code")
    {
      code = server.arg(i);
      refreshToken = spotify.requestAccessTokens(code.c_str(), callbackURI);
    }
  }

  if (refreshToken != NULL)
  {
    server.send(200, "text/plain", refreshToken);
  }
  else
  {
    server.send(404, "text/plain", "Failed to load token, check serial monitor");
  }
}

void handleNotFound()
{
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";

  for (uint8_t i = 0; i < server.args(); i++)
  {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }

  Serial.print(message);
  server.send(404, "text/plain", message);
}

void setup()
{

  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  IPAddress ipAddress = WiFi.localIP();
  Serial.println(ipAddress);

  if (MDNS.begin("arduino"))
  {
    Serial.println("MDNS responder started");
  }

  // Handle HTTPS Verification
#if defined(ESP8266)
  client.setFingerprint(SPOTIFY_FINGERPRINT); // These expire every few months
#elif defined(ESP32)
  client.setCACert(spotify_server_cert);
#endif
  // ... or don't!
  //client.setInsecure();

  // If you want to enable some extra debugging
  // uncomment the "#define SPOTIFY_DEBUG" in ArduinoSpotify.h

#ifdef USE_IP_ADDRESS
  // Building up callback URL using IP address.
  sprintf(callbackURI, callbackURItemplate, callbackURIProtocol, ipAddress.toString().c_str(), callbackURIAddress);
#endif

  server.on("/", handleRoot);
  server.on("/callback/", handleCallback);
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
}

void loop()
{
#if defined(ESP8266)
  MDNS.update();
#endif

  server.handleClient();
}
  • Вопрос задан
  • 84 просмотра
Пригласить эксперта
Ответы на вопрос 1
@lonelymyp
Хочу вылезти из минуса по карме.
Ошибка это "Connection failed"
То есть твой код не смог приконнектиться куда-то.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы