我正在尝试用Android中的Xbox音乐API制作音乐播放器。到目前为止,我无法弄清楚如何准确检索访问令牌,如文档所述:
http://msdn.microsoft.com/en-us/library/dn546686.aspx
到目前为止,我有一个asynctask从我尝试使HttpPost工作(我是RESTful服务的新手)
这是我的代码:
@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;
}
在Log.w上,我看到一个页面,显示"服务器应用程序错误"我不知道如何正确地做这个请求。
所有" Constants.LIKE"是文档说我需要的信息的字符串。
修改
这是我的常量(为我的应用安全而隐藏的秘密)
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";
答案 0 :(得分:0)
如果有人遇到同样的问题,我修改了我的代码并解决了问题:
@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;
}
缺少的是从&#34;行&#34;制作一个JSON对象。字符串,你很高兴。 您可以使用我的代码在日志中查看您的访问令牌。