从 ESP8266 控制 Spotify (Arduino IDE)
Controlling Spotify from ESP8266 (Arduino IDE)
所以我想通过 ESP8266 控制我的 Spotify。我想从基础开始,这样我就能对整个事情有所了解,并能够将其集成到其他项目中。 Spotify 提供了这些 curl 命令供使用,但 ESP8266 不是 curl ...我不确定如何将此:curl -X "PUT" "https://api.spotify.com/v1/me/player/play" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer putsomerandomstuffherethisisanauthtoken"
变成 ESP 可以使用的东西...我已经有了基本的 ESP 库,例如 ESP8266WiFi.h
等。如果有人能指出我正确的方向,那就太好了。谢谢!
您需要使用 HTTPClient
库。
您的代码将如下所示:
#include <ESP8266HTTPClient.h>
#define SPOTIFY_AUTHORIZATION_TOKEN "Bearer putsomerandomstuffherethisisanauthtoken"
// easily get fingerprints with https://www.grc.com/fingerprints.htm
#define SPOTIFY_FINGERPRINT "AB:BC:7C:9B:7A:D8:5D:98:8B:B2:72:A4:4C:13:47:9A:00:2F:70:B5"
void spotify_play() {
HTTPClient client;
client.begin("https://api.spotify.com/v1/me/player/play", String(SPOTIFY_FINGERPRINT);
client.addHeader("Accept", "application/json"");
client.addHeader("Content-Type", "application/json");
client.addHeader("Content-Length", "0");
client.addHeader("Authorization", SPOTIFY_AUTHORIZATION_TOKEN);
int httpCode = client.sendRequest("PUT");
if(httpCode == -1) {
Serial.printf("http error: %s\n", http.errorToString(httpCode).c_str());
} else {
Serial.printf("HTTP status code %d\n", httpCode);
}
}
Spotify API 文档应该列出 return 成功或错误时的 HTTP 状态代码。它应该在 200 范围内才能成功。
指纹的作用是让您验证您确实在与正确的网站通话。当 Spotify 更改其 SSL 证书时,您需要更新指纹。您不需要在 curl
或更强大的系统上执行此操作,因为它们能够更好地获得证书。有 been some work to make this easier on the ESP8266,但我不确定它的可靠性如何。
[编辑添加 Content-length header]
所以我想通过 ESP8266 控制我的 Spotify。我想从基础开始,这样我就能对整个事情有所了解,并能够将其集成到其他项目中。 Spotify 提供了这些 curl 命令供使用,但 ESP8266 不是 curl ...我不确定如何将此:curl -X "PUT" "https://api.spotify.com/v1/me/player/play" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer putsomerandomstuffherethisisanauthtoken"
变成 ESP 可以使用的东西...我已经有了基本的 ESP 库,例如 ESP8266WiFi.h
等。如果有人能指出我正确的方向,那就太好了。谢谢!
您需要使用 HTTPClient
库。
您的代码将如下所示:
#include <ESP8266HTTPClient.h>
#define SPOTIFY_AUTHORIZATION_TOKEN "Bearer putsomerandomstuffherethisisanauthtoken"
// easily get fingerprints with https://www.grc.com/fingerprints.htm
#define SPOTIFY_FINGERPRINT "AB:BC:7C:9B:7A:D8:5D:98:8B:B2:72:A4:4C:13:47:9A:00:2F:70:B5"
void spotify_play() {
HTTPClient client;
client.begin("https://api.spotify.com/v1/me/player/play", String(SPOTIFY_FINGERPRINT);
client.addHeader("Accept", "application/json"");
client.addHeader("Content-Type", "application/json");
client.addHeader("Content-Length", "0");
client.addHeader("Authorization", SPOTIFY_AUTHORIZATION_TOKEN);
int httpCode = client.sendRequest("PUT");
if(httpCode == -1) {
Serial.printf("http error: %s\n", http.errorToString(httpCode).c_str());
} else {
Serial.printf("HTTP status code %d\n", httpCode);
}
}
Spotify API 文档应该列出 return 成功或错误时的 HTTP 状态代码。它应该在 200 范围内才能成功。
指纹的作用是让您验证您确实在与正确的网站通话。当 Spotify 更改其 SSL 证书时,您需要更新指纹。您不需要在 curl
或更强大的系统上执行此操作,因为它们能够更好地获得证书。有 been some work to make this easier on the ESP8266,但我不确定它的可靠性如何。
[编辑添加 Content-length header]