witnessmenow / spotify-api-arduino

Arduino library for integrating with the Spotify Web-API (Does not play music)
MIT License
178 stars 33 forks source link

Can't get access token on ESP8266 status Code -1? #23

Closed PacuFTW closed 3 years ago

PacuFTW commented 3 years ago

What the title says basically . I' can't really get my access token. It worked fine like 2 days ago, and yesterday I was unable to refresh it, so I went to generate a new, and I can't even get one. Is there any fix for this?

Serial output:

WiFi connected
IP address: 
192.168.0.21
MDNS responder started
HTTP server started
File Not Found

URI: /favicon.ico
Method: GET
Arguments: 0
grant_type=authorization_code&code=AQDM7SVuLkCdS5UQjXNcxMeaBLRMLol8_YMaoTqsFcA3oB04fKJ9juLIbJj9PVxfsFsfT6r9ya3WlM6mGXdjwWlwLUnBKDYjFoMBM8QnUh4F45cu2Cm9eSpq_JoB9fh0zZlOhvtKZZnlE5Qs4BO_LZesAv1Vjjk41Jcl_RcqZ_wOWRXtAQYono9tcEiE4JztdHeIqiVuAIc2jkbL9BuCFoeI6vzCRGfi4EnCuGbpd4aUIIZsj9bV_g&redirect_uri=http%3A%2F%2Farduino.local%2Fcallback%2F&client_id=(my correct clientid)&client_secret=(my corret client secret)
Connection failed
status Code-1

Edit: Also Here's my code for the rpoject so far

#pragma region PINEK
#define ROTARY_PINA D2
#define ROTARY_PINB D1
#define GOMB_PIN D3
#define MIN 0 //rotary min ertek
#define MAX 25 //rotary max ertek
#define TFT_CS 15
#define TFT_RST 0
#define TFT_DC 2
#pragma endregion

#pragma region KONYVTARAK
//ESP8266hoz
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <HttpClient.h>

//A kep letoltesehez
//#include <ESPDownload.h>
#include "FS.h"

//kijelzohoz
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>

//Rotary encoderhez
#include <ClickEncoder.h>
#define CLIK_PER_LEPES 4 //Rotary encoderhez alapertelmezett ertek

//Spotifyhez
#include <ArduinoJson.h>
#include <ArduinoSpotify.h>
#pragma endregion

#pragma region PRIVATADATOK
char ssid[] = "ssid";   // SSID
char jelszo[] = "password"; // Jelszo

char clientId[] = "myclientid"; // Spotify ClientID
char clientSecret[] = "myclientpassword"; // Spotify ClientSecret

#define SPOTIFY_MARKET "HU"
#define SPOTIFY_REFRESH_TOKEN "AQAZjXFmjhjU0_5P-sksQJcV32WqIZj2t70FBiRuTJsebfmUf6zDyyTx15Tk9aloEqN68fmvCPJBPpDnZ3VXJqA8QEnRhumt4x3HJX7hS2JExcaf4-0hxCKeS5Ds_9gVjmB_sL0Ry-FwjwKWjbwXtjsjDeOp1i8rRwtA4-ZjJu63hQ"
#pragma endregion

#pragma region objektumok
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

ClickEncoder encoder = ClickEncoder(ROTARY_PINA,ROTARY_PINB,GOMB_PIN,CLIK_PER_LEPES);

WiFiClientSecure wificlient;

ArduinoSpotify *spotify;

//ESPDownload downloader = ESPDownload();
#pragma endregion

#pragma region kijelzo tisztitas
//letisztitja a kijelzot
void clearScreen(){
  tft.fillScreen(ST77XX_BLACK);
}
#pragma endregion

#pragma region startup
//csatlakozik a wifihez
void csatlakozasWifihez(){
  WiFi.mode(WIFI_STA); // station-mód
  WiFi.disconnect();
  delay(100);

  Serial.print("Csatlakozas WIFI-hez: "); // csatlakozas wifihez
  Serial.println(ssid);
  WiFi.begin(ssid, jelszo);
  while (WiFi.status() != WL_CONNECTED) // amig nem csatlakozik
  {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("");
  Serial.println("Csatlakozva!");
  Serial.println("IP cim: ");
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);
}

//spotify alapbeallitasai
void spotifyAlap(){
  //wificlient.setFingerprint(SPOTIFY_FINGERPRINT); // ssl fingerprint beallitasa
  Serial.println(
      "Access Tokenek frissitese"); // ez altal ferunk hozza a Spotify API-hoz
  /*if (!spotify->refreshAccessToken()) {
    Serial.println("Nem sikerült lekerdezni az Access Tokeneket");
  }*/
}

//encoder alapbeallitasait
void encoderAlap(){
  encoder.setDoubleClickEnabled(true);
  encoder.setAccelerationEnabled(false);
}

//kijelzo alapbeallitasai
void kijelzoAlap(){
  tft.initR(INITR_BLACKTAB);
  tft.setRotation(2);
  tft.setTextWrap(false);
  tft.setTextColor(ST77XX_WHITE);
  clearScreen();
  printBar(0);
}
#pragma endregion

#pragma region Spotify lekerdezes es kiiras
//kiirja hogy jelenleg hol tart a zene
void printProgress(CurrentlyPlaying jelenlegJatszott){
  float eltelt = jelenlegJatszott.progressMs;
  float idotartam = jelenlegJatszott.duraitonMs;
  float szazalek = (eltelt / idotartam) * 100;
  int egeszszazalek = (int)szazalek;
  tft.fillRect(14, 152, 99, 6, ST77XX_BLACK);
  tft.fillRect(14, 152, egeszszazalek, 6, ST77XX_WHITE);
}

//itt tarolodik a zene es az eloado neve
char* zeneneve= "Burning Bridges / Long Day (feat. IDK)";
char* eloadoneve= "King Gizzard and The Lizard Wizard";

//lekerdezi az eloado es a zene nevet
void getArtistandMusic(CurrentlyPlaying jelenlegJatszott){
  eloadoneve = jelenlegJatszott.firstArtistName;
  zeneneve= jelenlegJatszott.trackName;
}

//kiirja az eloadot es a zene nevet
void printArtistandMusic(){
  tft.fillRect(13, 107, 115, 20, ST77XX_BLACK);
  tft.setCursor(13, 107);
  tft.print(eloadoneve); //dinamikusan megjelenítendő
  tft.setCursor(13, 117);
  tft.print(zeneneve); //dinamikusan megjelenítendő
}

//attol fuggoen hogy jelenleg jatszodik e le zene rajzol ki egy szimbolumot
void printPlayPause(CurrentlyPlaying jelenlegJatszott){
  tft.fillRect(56, 145, 16, -16, ST77XX_BLACK);
  if(jelenlegJatszott.isPlaying){
    tft.fillRect(58, 145, 4, -14, ST77XX_WHITE);
    tft.fillRect(66, 145, 4, -14, ST77XX_WHITE);
  }
  else{
    tft.fillTriangle(58, 145, 58, 131, 69, 138, ST77XX_WHITE);
  }
}
#pragma endregion

#pragma region Iranyito menu
//valtozok a menuk iranyitasahoz
int menuszam = 0;
int kivalasztottelem = 0;

//a visszalepo gomb kiirasa
void printBack(int kivalasztottelem){
  if (kivalasztottelem == 0){
    tft.fillRect(17, 14, -7, 4, ST77XX_GREEN);
    tft.fillTriangle(10, 20, 10, 12, 6, 16, ST77XX_GREEN);
  }
  else {
    tft.fillRect(17, 14, -7, 4, ST77XX_WHITE);
    tft.fillTriangle(10, 20, 10, 12, 6, 16, ST77XX_WHITE);
  }
}

//Ismetlodes be van e kapcsolva
//0 - off
//1 - album/playlist ismetles
//2 - zene ismetlese
int repeatcounter = 0;

//ismetles gomb kiirasa
void printRepeat(int kivalasztottelem){
  if (kivalasztottelem == 1){
    if (repeatcounter == 0){
      tft.drawCircle(112, 48, 5, ST77XX_GREEN);
    }
    else if (repeatcounter == 1){
      tft.fillCircle(112, 48, 5, ST77XX_GREEN);
    }
    else{
      tft.drawCircle(112, 48, 5, ST77XX_GREEN);
      tft.drawChar(110, 45, '1', ST77XX_GREEN, ST77XX_BLACK, 1);
    }
  }
  else{
    if (repeatcounter == 0){
      tft.drawCircle(112, 48, 5, ST77XX_WHITE);
    }
    else if (repeatcounter == 1){
      tft.fillCircle(112, 48, 5, ST77XX_WHITE);
    }
    else{
      tft.drawCircle(112, 48, 5, ST77XX_WHITE);
      tft.drawChar(110, 45, '1', ST77XX_WHITE, ST77XX_BLACK, 1);
    }
  }
}

//shuffle be van e kapcsolva
bool shuffletoggle = false;

//shuffle gomb kiirasa
void printShuffle(int kivalasztottelem){
  if (kivalasztottelem == 2){
    if (!shuffletoggle) tft.drawChar(110, 61, 'S', ST77XX_GREEN, ST77XX_BLACK, 1);
    else tft.drawChar(110, 61, 'S', ST77XX_RED, ST77XX_BLACK, 1);
  }
  else{
    if (!shuffletoggle) tft.drawChar(110, 61, 'S', ST77XX_WHITE, ST77XX_BLACK, 1);
    else tft.drawChar(110, 61, 'S', ST77XX_GREEN, ST77XX_BLACK, 1);
  }
}

//elozo zene gomb kiirasa
void printBackward(int kivalasztottelem){
  if (kivalasztottelem == 3){
    tft.fillTriangle(34, 145, 34, 131, 23, 138, ST77XX_GREEN);
    tft.fillTriangle(29, 145, 29, 131, 18, 138, ST77XX_GREEN);
  }
  else{
    tft.fillTriangle(34, 145, 34, 131, 23, 138, ST77XX_WHITE);
    tft.fillTriangle(29, 145, 29, 131, 18, 138, ST77XX_WHITE);
  }
}

//szunet gomb kiirasa
void printPause(int kivalasztottelem){
  if (kivalasztottelem == 4){
    tft.fillRect(46, 145, 4, -14, ST77XX_GREEN);
    tft.fillRect(54, 145, 4, -14, ST77XX_GREEN);
  }
  else{
    tft.fillRect(46, 145, 4, -14, ST77XX_WHITE);
    tft.fillRect(54, 145, 4, -14, ST77XX_WHITE);
  }
}

//lejatszas gomb kiirasa
void printPlay(int kivalasztottelem){
  if (kivalasztottelem == 5){
    tft.fillTriangle(72, 145, 72, 131, 81, 138, ST77XX_GREEN);
  }
  else{
    tft.fillTriangle(72, 145, 72, 131, 81, 138, ST77XX_WHITE);
  }
}

//kovetkezo zene gomb kiirasa
void printForward(int kivalasztottelem){
  if (kivalasztottelem == 6){
    tft.fillTriangle(94, 145, 94, 131, 105, 138, ST77XX_GREEN);
    tft.fillTriangle(99, 145, 99, 131, 110, 138, ST77XX_GREEN);
  }
  else{
    tft.fillTriangle(94, 145, 94, 131, 105, 138, ST77XX_WHITE);
    tft.fillTriangle(99, 145, 99, 131, 110, 138, ST77XX_WHITE);
  }
}

//az also progressbar/hangerosav kiirasa
void printBar(int menuszam){
  if (kivalasztottelem == 7){
    tft.drawRect(14, 151, 101, 8, ST77XX_GREEN);
  }
  else tft.drawRect(14, 151, 101, 8, ST77XX_WHITE);
}

//a hangerot kiirja a hangerosavra
void printVolume(int vol){
  tft.fillRect(14, 152, vol, 6, ST77XX_WHITE);
  tft.fillRect(114, 152, -(100-vol), 6, ST77XX_BLACK);
}

#pragma endregion

#pragma region setup
void setup() {
  Serial.begin(115200);
  spotify = new ArduinoSpotify(wificlient, clientId, clientSecret, SPOTIFY_REFRESH_TOKEN); 
  csatlakozasWifihez();
  spotifyAlap();
  encoderAlap();
  kijelzoAlap();
}
#pragma endregion

#pragma region loop valtozok
//Spotify jelenleg lejatszott zene
CurrentlyPlaying jelenlegJatszott;

//Jelenleg lejatszott zene URL-je
String jelenlegikepUrl = "";

//seged valtozo a belso menuhoz
bool kiirtamar = false;

//Spotify lekeresek kozotti ido
unsigned long kesleltetes = 2000; // Lekeres kozotti kesleltetesi ido
unsigned long kesleltetesseged;

//Nem kell ide magyarazat
int hangero = 100;
int newhangero = 100;
#pragma endregion

#pragma region loop
void loop() { 
  static uint32_t lastService = 0;
  if (lastService + 1000 < micros()) {
    lastService = micros();                
    encoder.service();  
  }

  static int16_t last, value;
  value += encoder.getValue();
  if (value != last) {
    last = value;
    if (menuszam == 2){
      newhangero = abs((last*5) % 101);
      Serial.println(newhangero);
    }
    else if (menuszam == 1){
      kivalasztottelem = abs(last % 8);
      Serial.println(kivalasztottelem);
    }
  }

  ClickEncoder::Button b = encoder.getButton();
  if (b != ClickEncoder::Open) {
    Serial.print("Button: ");
    switch (b) {
      case(ClickEncoder::Clicked): 
        if(menuszam == 0) {
          menuszam = 1; 
          clearScreen();
          printBar(0);
          kiirtamar = false;
        } 
        else if (menuszam == 1) {
          if(kivalasztottelem == 0){
            menuszam = 0;
            clearScreen();
            printBar(0);
          }
          else if(kivalasztottelem == 1){
            repeatcounter++;
            if (repeatcounter > 2) repeatcounter = 0;
            tft.fillRect(107, 43, 10, 10, ST77XX_BLACK);
            /*if (repeatcounter == 0) spotify->setRepeatMode(repeat_off);
            else if (repeatcounter == 1) spotify->setRepeatMode(repeat_context);
            else spotify->setRepeatMode(repeat_track);*/
          }
          else if(kivalasztottelem == 2){
            if (!shuffletoggle) shuffletoggle = true;
            else shuffletoggle = false;
            //spotify->toggleShuffle(shuffletoggle);
          }
          else if(kivalasztottelem == 3){
           // spotify->previousTrack();
          }
          else if(kivalasztottelem == 4){
           // spotify->pause();
          }
          else if(kivalasztottelem == 5){
            //spotify->play();
          }
          else if(kivalasztottelem == 6){
            //spotify->nextTrack();
          }
          else if(kivalasztottelem == 7){
            tft.fillRect(14, 152, 99, 6, ST77XX_BLACK);
            menuszam = 2;
          }
        }
        else if (menuszam == 2){
          if (newhangero != hangero){
            //spotify->setVolume(newhangero);
            hangero = newhangero;
          }
          value = 7;
          menuszam = 1;
        } 
        break;
      case(ClickEncoder::DoubleClicked): break; 
      default: break;
    }
  }    
  if (millis() > kesleltetesseged) {

    /*String curHREF = jelenlegJatszott.albumImages[2].url;
    curHREF.replace("https", "http");
    if (curHREF != jelenlegikepUrl){

    }*/
    if(menuszam == 0){
      jelenlegJatszott = spotify->getCurrentlyPlaying(SPOTIFY_MARKET);
      printProgress(jelenlegJatszott);
      //getArtistandMusic(jelenlegJatszott);
      printArtistandMusic(); 
      printPlayPause(jelenlegJatszott);
    }
    kesleltetesseged = millis() + kesleltetes;
  }
  if (menuszam == 1){
    printForward(kivalasztottelem);
    printBackward(kivalasztottelem);
    printPause(kivalasztottelem);
    printPlay(kivalasztottelem);
    printBack(kivalasztottelem);
    printBar(kivalasztottelem);
    printRepeat(kivalasztottelem);
    printShuffle(kivalasztottelem);
    if(!kiirtamar){
      printArtistandMusic();
      kiirtamar = true;
    }
  }
  if (menuszam == 2){
    printBar(7);
    printVolume(newhangero);
  }
}
#pragma endregion
witnessmenow commented 3 years ago

22 seems like the server fingerprint has changed.

On Wed 5 May 2021, 16:20 PacuFTW, @.***> wrote:

What the title says basically . I' can't really get my access token. It worked fine like 2 days ago, and yesterday I was unable to refresh it, so I went to generate a new, and I can't even get one. Is there any fix for this?

Serial output:

WiFi connected IP address: 192.168.0.21 MDNS responder started HTTP server started File Not Found

URI: /favicon.ico Method: GET Arguments: 0 grant_type=authorization_code&code=AQDM7SVuLkCdS5UQjXNcxMeaBLRMLol8_YMaoTqsFcA3oB04fKJ9juLIbJj9PVxfsFsfT6r9ya3WlM6mGXdjwWlwLUnBKDYjFoMBM8QnUh4F45cu2Cm9eSpq_JoB9fh0zZlOhvtKZZnlE5Qs4BO_LZesAv1Vjjk41Jcl_RcqZ_wOWRXtAQYono9tcEiE4JztdHeIqiVuAIc2jkbL9BuCFoeI6vzCRGfi4EnCuGbpd4aUIIZsj9bV_g&redirect_uri=http%3A%2F%2Farduino.local%2Fcallback%2F&client_id=(my correct clientid)&client_secret=(my corret client secret) Connection failed status Code-1

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/witnessmenow/arduino-spotify-api/issues/23, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAL5PQROVSMP3PFN575GGDTTMFO3FANCNFSM44FFAYIA .

PacuFTW commented 3 years ago

22 seems like the server fingerprint has changed.

On Wed 5 May 2021, 16:20 PacuFTW, @.***> wrote: What the title says basically . I' can't really get my access token. It worked fine like 2 days ago, and yesterday I was unable to refresh it, so I went to generate a new, and I can't even get one. Is there any fix for this? Serial output: WiFi connected IP address: 192.168.0.21 MDNS responder started HTTP server started File Not Found URI: /favicon.ico Method: GET Arguments: 0 grant_type=authorization_code&code=AQDM7SVuLkCdS5UQjXNcxMeaBLRMLol8_YMaoTqsFcA3oB04fKJ9juLIbJj9PVxfsFsfT6r9ya3WlM6mGXdjwWlwLUnBKDYjFoMBM8QnUh4F45cu2Cm9eSpq_JoB9fh0zZlOhvtKZZnlE5Qs4BO_LZesAv1Vjjk41Jcl_RcqZ_wOWRXtAQYono9tcEiE4JztdHeIqiVuAIc2jkbL9BuCFoeI6vzCRGfi4EnCuGbpd4aUIIZsj9bV_g&redirect_uri=http%3A%2F%2Farduino.local%2Fcallback%2F&client_id=(my correct clientid)&client_secret=(my corret client secret) Connection failed status Code-1 — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#23>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAL5PQROVSMP3PFN575GGDTTMFO3FANCNFSM44FFAYIA .

Is there any way for me to fix that? I commented it out too, but it doesn't work with it in anyways. Holy fuck I'm so dumb. Sorry.

foogadgets commented 3 years ago

Same problem here. The example code for getRefreshToken do not work any longer. I get connection failed when trying to connect to accounts.spotify.com.

PacuFTW commented 3 years ago

Same problem here. The example code for getRefreshToken do not work any longer. I get connection failed when trying to connect to accounts.spotify.com.

Replace the SPOTIFY_FINGERPRINT in ArduinoSpotify.h with this: "8d33e76114a061ef6f5fd53ccb1fc76cb86769ba"

foogadgets commented 3 years ago

Thanks! I just tested this as I saw your post #22. It worked of course. Thanks for the quick answer!

witnessmenow commented 3 years ago

Updated fingerprint in master branch now