Skip to content

Commit 13227c2

Browse files
committed
Fixing refresh token examples to work with the library. Fixes #8 and also Reported in #7
1 parent 36a9ea6 commit 13227c2

File tree

2 files changed

+62
-45
lines changed

2 files changed

+62
-45
lines changed

examples/esp32/getRefreshToken/getRefreshToken.ino

+32-23
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
Twitter: https://twitter.com/witnessmenow
3434
*******************************************************************/
3535

36-
3736
// ----------------------------
3837
// Standard Libraries
3938
// ----------------------------
@@ -63,9 +62,9 @@
6362

6463
//------- Replace the following! ------
6564

66-
char ssid[] = "SSID"; // your network SSID (name)
67-
char password[] = "password"; // your network password
68-
char clientId[] = "56t4373258u3405u43u543"; // Your client ID of your spotify APP
65+
char ssid[] = "SSID"; // your network SSID (name)
66+
char password[] = "password"; // your network password
67+
char clientId[] = "56t4373258u3405u43u543"; // Your client ID of your spotify APP
6968
char clientSecret[] = "56t4373258u3405u43u543"; // Your client Secret of your spotify APP (Do Not share this!)
7069

7170
char scope[] = "user-read-playback-state%20user-modify-playback-state";
@@ -82,12 +81,11 @@ char callbackURI[100];
8281

8382
WebServer server(80);
8483

85-
8684
WiFiClientSecure client;
8785
ArduinoSpotify spotify(client, clientId, clientSecret);
8886

8987
const char *webpageTemplate =
90-
R"(
88+
R"(
9189
<!DOCTYPE html>
9290
<html>
9391
<head>
@@ -103,30 +101,38 @@ const char *webpageTemplate =
103101
</html>
104102
)";
105103

106-
void handleRoot() {
104+
void handleRoot()
105+
{
107106
char webpage[800];
108107
sprintf(webpage, webpageTemplate, clientId, callbackURI, scope);
109108
server.send(200, "text/html", webpage);
110109
}
111110

112-
void handleCallback() {
111+
void handleCallback()
112+
{
113113
String code = "";
114-
char *refreshToken = NULL;
115-
for (uint8_t i = 0; i < server.args(); i++) {
116-
if (server.argName(i) == "code") {
114+
const char *refreshToken = NULL;
115+
for (uint8_t i = 0; i < server.args(); i++)
116+
{
117+
if (server.argName(i) == "code")
118+
{
117119
code = server.arg(i);
118-
refreshToken = spotify.requestAccessTokens((char*) code.c_str(), callbackURI);
120+
refreshToken = spotify.requestAccessTokens(code.c_str(), callbackURI);
119121
}
120122
}
121123

122-
if(refreshToken != NULL){
124+
if (refreshToken != NULL)
125+
{
123126
server.send(200, "text/plain", refreshToken);
124-
} else {
125-
server.send(404, "text/plain", "Failed to load token, check serial monitor");
127+
}
128+
else
129+
{
130+
server.send(404, "text/plain", "Failed to load token, check serial monitor");
126131
}
127132
}
128133

129-
void handleNotFound() {
134+
void handleNotFound()
135+
{
130136
String message = "File Not Found\n\n";
131137
message += "URI: ";
132138
message += server.uri();
@@ -136,15 +142,17 @@ void handleNotFound() {
136142
message += server.args();
137143
message += "\n";
138144

139-
for (uint8_t i = 0; i < server.args(); i++) {
145+
for (uint8_t i = 0; i < server.args(); i++)
146+
{
140147
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
141148
}
142149

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

147-
void setup() {
154+
void setup()
155+
{
148156

149157
Serial.begin(115200);
150158

@@ -153,7 +161,8 @@ void setup() {
153161
Serial.println("");
154162

155163
// Wait for connection
156-
while (WiFi.status() != WL_CONNECTED) {
164+
while (WiFi.status() != WL_CONNECTED)
165+
{
157166
delay(500);
158167
Serial.print(".");
159168
}
@@ -164,7 +173,8 @@ void setup() {
164173
IPAddress ipAddress = WiFi.localIP();
165174
Serial.println(ipAddress);
166175

167-
if (MDNS.begin("arduino")) {
176+
if (MDNS.begin("arduino"))
177+
{
168178
Serial.println("MDNS responder started");
169179
}
170180

@@ -182,8 +192,7 @@ void setup() {
182192
Serial.println("HTTP server started");
183193
}
184194

185-
186-
187-
void loop() {
195+
void loop()
196+
{
188197
server.handleClient();
189198
}

examples/esp8266/getRefreshToken/getRefreshToken.ino

+30-22
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
Twitter: https://twitter.com/witnessmenow
3535
*******************************************************************/
3636

37-
3837
// ----------------------------
3938
// Standard Libraries
4039
// ----------------------------
@@ -64,9 +63,9 @@
6463

6564
//------- Replace the following! ------
6665

67-
char ssid[] = "SSID"; // your network SSID (name)
68-
char password[] = "password"; // your network password
69-
char clientId[] = "56t4373258u3405u43u543"; // Your client ID of your spotify APP
66+
char ssid[] = "SSID"; // your network SSID (name)
67+
char password[] = "password"; // your network password
68+
char clientId[] = "56t4373258u3405u43u543"; // Your client ID of your spotify APP
7069
char clientSecret[] = "56t4373258u3405u43u543"; // Your client Secret of your spotify APP (Do Not share this!)
7170

7271
char scope[] = "user-read-playback-state%20user-modify-playback-state";
@@ -76,12 +75,11 @@ char callbackURI[] = "http%3A%2F%2Farduino.local%2Fcallback%2F";
7675

7776
ESP8266WebServer server(80);
7877

79-
8078
WiFiClientSecure client;
8179
ArduinoSpotify spotify(client, clientId, clientSecret);
8280

8381
const char *webpageTemplate =
84-
R"(
82+
R"(
8583
<!DOCTYPE html>
8684
<html>
8785
<head>
@@ -97,30 +95,38 @@ const char *webpageTemplate =
9795
</html>
9896
)";
9997

100-
void handleRoot() {
98+
void handleRoot()
99+
{
101100
char webpage[800];
102101
sprintf(webpage, webpageTemplate, clientId, callbackURI, scope);
103102
server.send(200, "text/html", webpage);
104103
}
105104

106-
void handleCallback() {
105+
void handleCallback()
106+
{
107107
String code = "";
108-
char *refreshToken = NULL;
109-
for (uint8_t i = 0; i < server.args(); i++) {
110-
if (server.argName(i) == "code") {
108+
const char *refreshToken = NULL;
109+
for (uint8_t i = 0; i < server.args(); i++)
110+
{
111+
if (server.argName(i) == "code")
112+
{
111113
code = server.arg(i);
112-
refreshToken = spotify.requestAccessTokens((char*) code.c_str(), callbackURI);
114+
refreshToken = spotify.requestAccessTokens(code.c_str(), callbackURI);
113115
}
114116
}
115117

116-
if(refreshToken != NULL){
118+
if (refreshToken != NULL)
119+
{
117120
server.send(200, "text/plain", refreshToken);
118-
} else {
119-
server.send(404, "text/plain", "Failed to load token, check serial monitor");
121+
}
122+
else
123+
{
124+
server.send(404, "text/plain", "Failed to load token, check serial monitor");
120125
}
121126
}
122127

123-
void handleNotFound() {
128+
void handleNotFound()
129+
{
124130
String message = "File Not Found\n\n";
125131
message += "URI: ";
126132
message += server.uri();
@@ -130,15 +136,17 @@ void handleNotFound() {
130136
message += server.args();
131137
message += "\n";
132138

133-
for (uint8_t i = 0; i < server.args(); i++) {
139+
for (uint8_t i = 0; i < server.args(); i++)
140+
{
134141
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
135142
}
136143

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

141-
void setup() {
148+
void setup()
149+
{
142150

143151
Serial.begin(115200);
144152

@@ -163,7 +171,8 @@ void setup() {
163171
IPAddress ip = WiFi.localIP();
164172
Serial.println(ip);
165173

166-
if (MDNS.begin("arduino")) {
174+
if (MDNS.begin("arduino"))
175+
{
167176
Serial.println("MDNS responder started");
168177
}
169178

@@ -178,9 +187,8 @@ void setup() {
178187
Serial.println("HTTP server started");
179188
}
180189

181-
182-
183-
void loop() {
190+
void loop()
191+
{
184192
server.handleClient();
185193
MDNS.update();
186194
}

0 commit comments

Comments
 (0)