通过Applescript / Shell进行简单的cURL POST请求

时间:2015-11-08 22:54:49

标签: shell post curl applescript spotify

我想向Spotify发送一个简单的cURL POST请求来交换我的授权代码以获取我可以在我正在构建的小应用程序中使用的令牌。我是用Applescript写的。

我之前已经想出如何使用GET进行搜索,GET的语法看起来非常简单。

另一方面,我无法让Spotify接受我发送数据的格式。

这是他们的developer guide

他们的示例请求是:

curl -H "Authorization: Basic ZjM...zE=" -d grant_type=authorization_code -d code=MQCbtKe...44KN -d redirect_uri=https%3A%2F%2Fwww.foo.com%2Fauth https://accounts.spotify.com/api/token

根据他们的授权指南,我应该能够丢弃上面示例中的标题,并在POST主体中包含client_id和client_secret作为参数。

(但我注意到他们在示例中使用的语法与我在其他地方找到的语法不同,包括cURL manual page)。

遵循Spotify的指南给我:

curl -d grant_type=authorization_code -d code=<authcodehere> -d redirect_uri=<redirect_uri> -d client_id=<clientid> -d client_secret=<clientsecret> https://accounts.spotify.com/api/token

添加-i -v进行调试..

在终端中运行后的输出是:

* Connected to accounts.spotify.com
* Server certificate: *.spotify.com
* Server certificate: DigiCert SHA2 Secure Server CA
* Server certificate: DigiCert Global Root CA
 POST /api/token HTTP/1.1
 Host: accounts.spotify.com
 User-Agent: curl/7.43.0
 Accept: */*
 Content-Length: 410
 Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 410 out of 410 bytes
< HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
< Server: nginx
Server: nginx
< Content-Type: application/json
Content-Type: application/json
< Content-Length: 68
Content-Length: 68
< Connection: keep-alive
Connection: keep-alive
< Keep-Alive: timeout=10
Keep-Alive: timeout=10

< 
* Connection #0 to host accounts.spotify.com left intact
{"error":"invalid_grant","error_description":"Invalid redirect URI"}

所以我尝试了上述POST请求的变体,但很少或没有用。

我最成功的尝试:

curl -i -v -X POST "https://api.spotify.com/api/token" 
--data "
{\"grant_type\":\"authorization_code\",\"code\":\"<authcode>\",\"redirect_uri\":\"
<redirecturi>\",\"client_id\":\"<clientid>\",\"client_secret\":\"<clientsecret>\"}"
显然所有人都在一条线上。结果:

* Connected to api.spotify.com
* Server certificate: *.spotify.com
* Server certificate: DigiCert SHA2 Secure Server CA
* Server certificate: DigiCert Global Root CA
> POST /api/token HTTP/1.1
> Host: api.spotify.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 420
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 420 out of 420 bytes
< HTTP/1.1 401 Unauthorized
HTTP/1.1 401 Unauthorized
< Server: nginx
Server: nginx
< Content-Type: application/json
Content-Type: application/json
< Content-Length: 74
Content-Length: 74
< Connection: keep-alive
Connection: keep-alive
< Keep-Alive: timeout=600
Keep-Alive: timeout=600
< WWW-Authenticate: Bearer realm="spotify"
WWW-Authenticate: Bearer realm="spotify"
< Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
< Access-Control-Allow-Credentials: true
Access-Control-Allow-Credentials: true
< Access-Control-Max-Age: 604800
Access-Control-Max-Age: 604800
< Access-Control-Allow-Headers: Accept, Authorization, Origin, Content-Type
Access-Control-Allow-Headers: Accept, Authorization, Origin, Content-Type

< 
{
  "error": {
    "status": 401,
    "message": "No token provided"
  }
* Connection #0 to host api.spotify.com left intact

在此阶段不确定它对我的要求,我们非常感谢您的帮助。

谢谢!

0 个答案:

没有答案