2017-12-07 8 views
1

認可コード付与を使用してspotify Web APIを認可するのに問題があります。クライアントのIDとクライアントの秘密情報を入力してURIにリダイレクトする必要があることを知っていますが、アクセストークンを取得するためにはコードという文字列を取得する方法がわかりません。webify api認可コードgrant thelinmichael/spotify-web-api-java android

final String clientId = "<your_client_id>"; 
final String clientSecret = "<your_client_secret>"; 
final String redirectURI = "<your_redirect_uri>"; 

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); 

/* 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 = "<insert code>"; //I don't know where I get the value for this string from 

/* 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! */ 

    /* 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. */ 

     } 
}); 

あなたは、このコードを取得し、成功したアクセストークンを取得する方法を知っていますか?

ありがとうございます!

答えて

1

Authorization Code Flowの説明にあるように、ユーザーをSpotify URLに送信する必要があります。このURLは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 
*/ 

ログインしているユーザーと指定したスコープにアクセスするためのアプリケーションのアクセス許可の後、Spotifyは、コールバックURLにユーザーをリダイレクトします。これは複雑になっています。 SpotifyがコールバックURLで提供するコードパラメータを受け取る必要があります。このコードパラメータは、進行中のプロセスに必要な値です。使用しているspotify-web-api-javaは、このリクエストを受け取るためのものを提供しません。たとえば、次のような方法で解決策を見つける必要があります。 a Spring RESTful Web Service

関連する問題