Spotify:发布到api /令牌提出了错误的请求,但可以在curl中工作

扬托·梅斯

我正在尝试通过授权代码流从Spotify Web API获取访问令牌在curl中进行请求时,它会按预期返回令牌:

curl -H "Authorization: Basic Mj...zk=" -d grant_type=authorization_code -d code=AQ...Ew -d redirect_uri=http://localhost:8081/callback https://accounts.spotify.com/api/token

但是,当我尝试在Java代码中执行相同的请求时,却收到400错误的请求:

    final String url = "https://accounts.spotify.com/api/token";

    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("grant_type", "authorization_code"));
    params.add(new BasicNameValuePair("code", req.getParameter("code")));
    params.add(new BasicNameValuePair("redirect_uri ", "http://localhost:8081/callback"));


    httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    httpPost.setHeader(HttpHeaders.AUTHORIZATION,"Basic " +  Base64.encodeBase64String(("2...0" + ":" + "4...9").getBytes()));
    httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");

    CloseableHttpResponse response = client.execute(httpPost);
    System.out.println(response);

这是我得到的错误响应:

HttpResponseProxy{HTTP/1.1 400 Bad Request [Server: nginx, Date: Fri, 06 Oct 2017 21:20:39 GMT, Content-Type: application/json, Content-Length: 68, Connection: keep-alive, Keep-Alive: timeout=600] ResponseEntityProxy{[Content-Type: application/json,Content-Length: 68,Chunked: false]}}

我想念什么或做错什么?

扬托·梅斯

我自己通过使用StringEntity而不是UrlEncodedFormEntity进行了修复:

    final String url = "https://accounts.spotify.com/api/token";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);

    httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
    httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64.encodeBase64String(("2...0" + ":" + "4...9").getBytes()));
    StringEntity data = new StringEntity("grant_type=authorization_code&&code=" + req.getParameter("code") + "&&redirect_uri=http://localhost:8081/callback");
    httpPost.setEntity(data);

    HttpResponse response = client.execute(httpPost);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

错误的AJAX请求发布到Spring API

如何使用CSRF令牌发布到Laravel API?

检索到Cloud Foundry OAuth令牌,但在发布到REST API时得到{“错误”:“未授权”}

使用Python请求模块将文件发布到API

Spotify api axios反应发布请求403错误

使用curl将数组发布到Godaddy API

PHP流发布请求不起作用,但可以使用curl

无法检索Spotify API令牌

对Discord API的Axios GET请求给出了403禁止响应,但可以在Postman中使用

Axios发布Spotify API请求返回400

发布数据时Webresponse上的内部服务器错误,但可以使用浏览器C#进行工作

我可以使用Spotify API将高频信号嵌入到播放列表中吗

Autograder规范给出了大问题的错误输出,但可以手动工作

使用Graph API将图像发布到回复中

Facebook令牌无效,但可以访问api

Spotify API到Swift

cURL请求Spotify API异常状态:415

发布到API 22中的PHP脚本

将标头发布到API(Wrike)请求

SSH 连接从工作站到服务器失败,但可以从服务器到工作站

Flask 无法从邮递员或网页获取发布数据,但可以在 python 请求中工作

将 Webforms 中的表单发布到 AmazonPay API

Spotify api 檢索到的訪問令牌不起作用

如何使用 JavaScript POST 到 Spotify API 令牌端点?

尝试发布到 Twilio API 时出现 CORS 策略错误

将参数发布到 api 主体返回错误 403

刷新令牌 Spotify API

Spotify API 中的请求授权错误

使用 Python 请求将数据发布到 REST API