2017-03-08 6 views
1

私はこのライブラリを使用しようとしていますので、library 私のSpotifyアカウントにアクセスすることができますが、私はどのように私はaccesトークンを取得することができませんがわかりません認証URLからの応答を取得する私は、URLにアクセスし、応答をプリントアウトする入力ストリームを作成してトレイを作ったが、私は正しい出力を与えていない。私はまた、応答を受信して​​サーバーを作成するトレイをトレイしたが、 ....も多いので、私はエラーをしたかもしれないとネットワーキング/ Javaサーバーを使用したことがないアクセストークン/コード

public class privat { 
 
    public privat() throws IOException { 
 

 

 
     final String clientId = "clientId "; 
 
     final String clientSecret = "clientSecret code "; 
 
     final String redirectUri = "http://localhost:8888/callback"; 
 
     final Api api = Api.builder() 
 
       .clientId(clientId) 
 
       .clientSecret(clientSecret) 
 
       .redirectURI(redirectUri) 
 
       .build(); 
 

 
/* Set the necessary scopes that the application will need from the user */ 
 
     final List<String> scopes = Arrays.asList("user-read-private", "user-read-email"); 
 

 
/* Set a state. This is used to prevent cross site request forgeries. */ 
 
     final String state = "someExpectedStateString"; 
 

 
     String authorizeURL = api.createAuthorizeURL(scopes, state); 
 
     System.out.println(authorizeURL); 
 

 
/* Continue by sending the user to the authorizeURL, which will look something like 
 
    https://accounts.spotify.com:443/authorize?client_id=5fe01282e44241328a84e7c5cc169165&response_type=code&redirect_uri=https://example.com/callback&scope=user-read-private%20user-read-email&state=some-state-of-my-choice 
 
*/ 
 

 

 
/* Application details necessary to get an access token */ 
 
     final String code = "" ;/* where to find this ?? */ 
 

 

 

 
     /* Make a token request. Asynchronous requests are made with the .getAsync method and synchronous requests 
 
* are made with the .get method. This holds for all type of requests. */ 
 
     final SettableFuture<AuthorizationCodeCredentials> authorizationCodeCredentialsFuture = api.authorizationCodeGrant(code).build().getAsync(); 
 

 
/* Add callbacks to handle success and failure */ 
 
     Futures.addCallback(authorizationCodeCredentialsFuture, new FutureCallback<AuthorizationCodeCredentials>() { 
 
      @Override 
 
      public void onSuccess(AuthorizationCodeCredentials authorizationCodeCredentials) { 
 
    /* The tokens were retrieved successfully! */ 
 
       System.out.println("Successfully retrieved an access token! " + authorizationCodeCredentials.getAccessToken()); 
 
       System.out.println("The access token expires in " + authorizationCodeCredentials.getExpiresIn() + " seconds"); 
 
       System.out.println("Luckily, I can refresh it using this refresh token! " +  authorizationCodeCredentials.getRefreshToken()); 
 

 
    /* Set the access token and refresh token so that they are used whenever needed */ 
 
       api.setAccessToken(authorizationCodeCredentials.getAccessToken()); 
 
       api.setRefreshToken(authorizationCodeCredentials.getRefreshToken()); 
 
      } 
 

 
      @Override 
 
      public void onFailure(Throwable throwable) { 
 
    /* Let's say that the client id is invalid, or the code has been used more than once, 
 
    * the request will fail. Why it fails is written in the throwable's message. */ 
 
        System.out.println(throwable.getMessage()); 
 
       System.out.println(throwable.getStackTrace()); 
 
      } 
 
     }); 
 
    } 
 

 
}

答えて

0

codeは、ユーザーがアプリケーションを承認すると、コールバックURLのクエリパラメータとして使用されます。ここからコードを取得するには、localhost:8888のWebサーバーを起動するか、リダイレクトURIのクエリパラメータからコードをコピーするようにユーザーに指示することができますリダイレクトされました。承認手順(詳細については、authorization codeまたはimplicit grantフローがあなたのために働くように見えます)on the Spotify Developer site