Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从ESP8266(Arduino IDE)控制Spotify_Arduino_Spotify_Esp8266 - Fatal编程技术网

从ESP8266(Arduino IDE)控制Spotify

从ESP8266(Arduino IDE)控制Spotify,arduino,spotify,esp8266,Arduino,Spotify,Esp8266,所以我想用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 p

所以我想用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 putsomerandomStufherethisisanauthToken“
放入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\u AUTHORIZATION\u TOKEN“承载PutsomeRandomStuff在此为NauthToken”
//很容易得到指纹https://www.grc.com/fingerprints.htm
#定义SPOTIFY_指纹“AB:BC:7C:9B:7A:D8:5D:98:8B:B2:72:A4:4C:13:47:9A:00:2F:70:B5”
void spotify_play(){
HTTPClient;
客户端。开始(“https://api.spotify.com/v1/me/player/play,字符串(SPOTIFY_指纹);
addHeader(“Accept”、“application/json”);
addHeader(“内容类型”、“应用程序/json”);
client.addHeader(“内容长度”,“0”);
client.addHeader(“授权”,SPOTIFY\u授权\u令牌);
int httpCode=client.sendRequest(“PUT”);
如果(httpCode==-1){
Serial.printf(“http错误:%s\n”,http.errorToString(httpCode.c_str());
}否则{
Serial.printf(“HTTP状态代码%d\n”,httpCode);
}
}
Spotify API文档应该列出它们在成功或出错时返回的HTTP状态代码。成功的范围应该是200

指纹功能允许您验证您是否真的在与正确的网站交谈。当Spotify更改其SSL证书时,您将需要更新指纹。您不需要使用
curl
或在功能更强大的系统上执行此操作,因为它们能够更好地处理证书。虽然有,但我不确定其可靠性现在还没有


[编辑以添加内容长度标题]

感谢您的回复,我已经运行了代码,我得到了代码411。这不是Spotify代码,通过快速谷歌搜索,这意味着它正在查找内容长度参数…我在Spotify的原始示例命令中找不到它…尝试添加client.addHeader(“内容长度”,“0”);对于其他.addHeader()调用,顺序无关紧要。太棒了!感谢您的帮助!现在我想起来似乎很简单。