【问题标题】:Spotify “Unexpected status: 400” when /api/token - Java/api/token - Java 时 Spotify “意外状态:400”
【发布时间】:2017-08-05 05:34:13
【问题描述】:

当我使用 Postman 向 Spotify Swap 端点发出 HTTP POST 请求时,服务器给了我一个包含刷新令牌的正确响应,但我在使用我的 Java 应用程序访问端点时无法获得相同的响应。

当直接点击 Spotify 端点时,我让我的 Java 应用程序生成与通过 Postman 生成的请求相同的请求。当我侦听 TCP 连接时,我看不到两个请求之间的差异超过身份验证代码,无法重用。

使用 Postman 和 a_code 的请求如下所示:

    bash-3.2$ nc -l 127.0.0.1 1234
    POST /api/token HTTP/1.1
    Host: localhost:1234
    Connection: keep-alive
    Content-Length: 460
    Accept: */*
    Cache-Control: no-cache
    Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
    Authorization: Basic <base64 encoded client_id:client_secret>
    Content-Type: application/x-www-form-urlencoded
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
    Postman-Token: b089ac57-7dcd-f94a-f315-9f28f74ac9eb
    Accept-Encoding: gzip, deflate, br
    Accept-Language: en-GB,en-US;q=0.8,en;q=0.6

code=a_code&grant_type=authorization_code&redirect_uri=my-spotifyoauth%3A%2F%2Fspotifylogincallback%2Ffree

使用我的 Java 应用程序和another_code 的请求(UPDATE):

POST /api/token HTTP/1.1
Authorization: Basic <base64 encoded client_id:client_secret>
Accept: */*
Connection: keep-alive
Cache-Control: no-cache
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Encoding: gzip, deflate, br
Content-Type: application/x-www-form-urlencoded
Content-Length: 460
Host: localhost:1234
User-Agent: Apache-HttpClient/4.3.5 (java 1.5)

code=another_code&grant_type=authorization_code&redirect_uri=my-spotifyoauth%3A%2F%2Fspotifylogincallback%2Ffree

当我将端点从 localhost:1234 切换到 accounts.spotify.com(非假端点)时,我从服务器收到 400 响应,但我不知道为什么。

POST https://accounts.spotify.com/api/token responded with status 400
> Accept-Encoding: gzip
< Server: nginx
< Date: Tue, 14 Mar 2017 17:08:34 GMT
< Content-Type: application/json
< Content-Length: 69
< Connection: keep-alive
< Keep-Alive: timeout=600
< {"error":"server_error","error_description":"Unexpected status: 400"}

您知道为什么即使通过 TCP 看到相同的请求,Spotify 服务器的响应也会不同吗?如何进一步调试此问题?

更新:代码不能重复使用,因此每次发出新请求时它们都应该不同。我的端点正在使我通过它发送的任何代码无效。我第二次使用相同的代码到达端点时收到错误:{"error":"invalid_grant","error_description":"Invalid authorization code"}

【问题讨论】:

    标签: java http post spotify


    【解决方案1】:

    我认为魔鬼在细节中。上面的请求肯定来自 Postman,看到它有一个 Postman-Token 标头。如果您改为发布您的 Java 应用程序发送到您的 netcat 的内容,我们可以弄清楚。

    我可以使用上面的示例使用 curl 重新创建请求,它对我有用,但我看到了更具描述性的错误消息:

    HTTP/1.1 400 Bad Request
    {"error":"invalid_grant","error_description":"Authorization code expired"}
    

    这也意味着您需要丢弃该帐户或更改您的 client_secret,因为您已将其发布到 stackoverflow,但我想您已经知道了。

    【讨论】:

    • 我忘记添加通过我的 Java 应用程序生成的请求,现在可以在更新中看到。你提到的那个错误如果我第二次通过 Postman 使用相同的代码点击 Spotify,我可以看到它。如果我通过端点重用相同的代码,响应将是:{"error":"invalid_grant","error_description":"Invalid authentication code"}
    猜你喜欢
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2021-09-19
    相关资源
    最近更新 更多