【问题标题】:Error 400 with Client Credentials Flow客户端凭据流出现错误 400
【发布时间】:2017-05-06 15:54:15
【问题描述】:

我正在尝试对客户端凭据流进行身份验证,但一直返回错误 400。我查看了可用的 API,但看不出我做错了什么。如果有人可以在正确的方向上推动我,那就太好了。谢谢

package com.elliott.lyric.io;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static org.apache.http.protocol.HTTP.USER_AGENT;

/**
 * Created by elliott on 05/05/2017.
 */
public class SpotifyLoader {

    String nowPlayingURL = "https://api.spotify.com/v1/me/player/currently-playing";
    String authURL = "https://accounts.spotify.com/api/token?grant_type=client_credentials";
    String clientID = "";
    String secretID = "";
    String authScope = "user-read-currently-playing user-read-playback-state";

    public SpotifyLoader() {
        authorize();
        //getRawPlaying();
    }

    void authorize() {
        try {

            HttpClient client = HttpClientBuilder.create().build();
            System.out.println(authURL);
            HttpPost post = new HttpPost(authURL);

// add header
            post.setHeader("User-Agent", USER_AGENT);
            post.setHeader("Content-Type", "application/x-www-form-urlencoded");

            List<NameValuePair> urlParameters = new ArrayList<>();
            String idSecret = clientID + ":" + secretID;
            String idSecretEncoded = new String(Base64.encodeBase64(idSecret.getBytes()));
            urlParameters.add(new BasicNameValuePair("Authorization", "Basic " + idSecretEncoded));

            post.setEntity(new UrlEncodedFormEntity(urlParameters));

            HttpResponse response = client.execute(post);
            System.out.println("Response Code : "
                    + response.getStatusLine().getStatusCode());

            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));

            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {

        }
    }

    void getRawPlaying() {
    }
}

【问题讨论】:

    标签: java spotify


    【解决方案1】:

    尝试以下更改的请求

    1. 将查询字符串从 url 移动到请求正文,因此 URL 为 https://accounts.spotify.com/api/token
    2. 在标题中发送授权。我刚刚修改了authorize(),如下所示。

      void authorize() {
      try {
      
          authURL = "https://accounts.spotify.com/api/token";
          String idSecret = clientID + ":" + secretID;
          String idSecretEncoded = new String(Base64.encodeBase64(idSecret.getBytes()));
      
          HttpClient client = HttpClientBuilder.create().build();
          HttpPost post = new HttpPost(authURL);
      
          post.setHeader("User-Agent", USER_AGENT);
          post.setHeader("Content-Type", "application/x-www-form-urlencoded");
          post.setHeader("Authorization", "Basic " + idSecretEncoded);
      
          List<NameValuePair> urlParameters = new ArrayList<>();
          urlParameters.add(new BasicNameValuePair("grant_type", "client_credentials"));
      
          post.setEntity(new UrlEncodedFormEntity(urlParameters));
      
          HttpResponse response = client.execute(post);
          System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
      
          BufferedReader rd = new BufferedReader(
                  new InputStreamReader(response.getEntity().getContent()));
      
          StringBuffer result = new StringBuffer();
          String line = "";
          while ((line = rd.readLine()) != null) {
              result.append(line);
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
      

      }

    【讨论】:

    • 非常感谢你。所以我把认证放在了错误的地方,误解了参数部分。所以它在 URL 中就像 ?Authorization=Basic.. 等?
    • 您尝试使用Authorization 作为表单数据,grant_type 作为查询参数。 :)
    猜你喜欢
    • 1970-01-01
    • 2022-08-06
    • 2021-12-22
    • 2014-11-20
    • 1970-01-01
    • 2017-08-12
    • 2020-07-07
    • 2012-12-17
    • 2019-04-28
    相关资源
    最近更新 更多