forked from witnessmenow/spotify-api-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetRefreshToken.ino
188 lines (144 loc) · 5.2 KB
/
getRefreshToken.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*******************************************************************
Get Refresh Token from spotify, this is needed for the other
examples.
Note: MDNS does not seem to work well on the ESP32 so we will have to use
the IP address for the redirect URL
Instrucitons:
- Put in your Wifi details, Client ID, Client secret and flash to the board
- 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 address
- Click the link
- The Refresh Token will be printed to screen, use this
for SPOTIFY_REFRESH_TOKEN in other examples.
Parts:
ESP32 D1 Mini stlye Dev board* - http://s.click.aliexpress.com/e/C6ds4my
* * = Affilate
If you find what I do usefuland would like to support me,
please consider becoming a sponsor on Github
https://github.com/sponsors/witnessmenow/
Written by Brian Lough
YouTube: https://www.youtube.com/brianlough
Tindie: https://www.tindie.com/stores/brianlough/
Twitter: https://twitter.com/witnessmenow
*******************************************************************/
// ----------------------------
// Standard Libraries
// ----------------------------
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <WiFiClientSecure.h>
// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------
#include <ArduinoSpotify.h>
// Library for connecting to the Spotify API
// Install from Github
// https://github.com/witnessmenow/arduino-spotify-api
#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[] = "56t4373258u3405u43u543"; // Your client ID of your spotify APP
char clientSecret[] = "56t4373258u3405u43u543"; // Your client Secret of your spotify APP (Do Not share this!)
char scope[] = "user-read-playback-state%20user-modify-playback-state";
char callbackURItemplate[] = "%s%s%s";
char callbackURIProtocol[] = "http%3A%2F%2F"; // "http://"
char callbackURIAddress[] = "%2Fcallback%2F"; // "/callback/"
char callbackURI[100];
//------- ---------------------- ------
// including a "spotify_server_cert" variable
// header is included as part of the ArduinoSpotify libary
#include <ArduinoSpotifyCert.h>
WebServer server(80);
WiFiClientSecure client;
ArduinoSpotify 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 = "";
char *refreshToken = NULL;
for (uint8_t i = 0; i < server.args(); i++) {
if (server.argName(i) == "code") {
code = server.arg(i);
refreshToken = spotify.requestAccessTokens((char*) 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");
}
spotify._debug = true;
client.setCACert(spotify_server_cert);
// Building up callback URL using IP address.
sprintf(callbackURI, callbackURItemplate, callbackURIProtocol, ipAddress.toString().c_str(), callbackURIAddress);
server.on("/", handleRoot);
server.on("/callback/", handleCallback);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}