Xbox music API RESTful with Android

237 Views Asked by At

I am trying to make a music player out of Xbox music API in Android. So far i cant figure out how to exactly retrieve the access token as the documentation says here:

http://msdn.microsoft.com/en-us/library/dn546686.aspx

So far i have an asynctask from where i try to make an HttpPost work (im new to RESTful services)

This is my code:

@Override
protected String doInBackground(String... strings) {
    String postData = "client_id=" + Constants.CLIENT_ID
            + "&client_secret=" + Constants.CLIENT_SECRET
            + "&scope=" + Constants.SCOPE
            + "&grant_type=" + Constants.GRANT_TYPE;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost(Constants.SERVICE + "/" + postData);
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),8);
        String line = null;
        while((line=bufferedReader.readLine())!=null){
            Log.w(Constants.TAG, line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

On the Log.w i am seeing a page indicating "Server application error" i dont know how to properly do this request.

All the "Constants.LIKE" are the strings of the information the documentation says i need.

EDIT:

This are my constants (secret hidden for security of my app)

public static final String TAG="MUSIC_PLAYER_APP";
public static final String CLIENT_ID="musicplayer_internship_ldurazo";
public static final String CLIENT_SECRET="";
public static final String CALLBACK_URL="http://luisdurazoa.tumblr.com/";
public static final String SERVICE="https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
public static final String SCOPE="http://music.xboxlive.com";
public static final String TOKEN="TOKEN";
public static final String GRANT_TYPE="client_credentials";
1

There are 1 best solutions below

0
On BEST ANSWER

In case someone gets the same problem o, i modified my code and solved the problem:

@Override
protected String doInBackground(String... strings) {
    try {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost(Constants.SERVICE);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
        nameValuePairs.add(new BasicNameValuePair("client_id",Constants.CLIENT_ID));
        nameValuePairs.add(new BasicNameValuePair("client_secret",Constants.CLIENT_SECRET));
        nameValuePairs.add(new BasicNameValuePair("scope",Constants.SCOPE));
        nameValuePairs.add(new BasicNameValuePair("grant_type",Constants.GRANT_TYPE));
        request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),8);
        String line = null;
        while((line=bufferedReader.readLine())!=null){
            stringBuilder.append(line);
            Log.w(Constants.TAG, line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

All that is missing is to make a JSON Object from the "line" String and you are good to go. You can see your access token in the log with my code.