开发的android新浪微博客户端只能授权我的第一个微博账号,我再申请的3个账号全部无法授权。 单独授权其他账户也不可以。 希望大家帮帮忙。public User getUserInfo(User user){
StringBuilder buffer = null;
String url = "http://api.t.sina.com.cn/users/show.json";
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("source",oauth.APP_KEY ));
params.add(new BasicNameValuePair("user_id",user.getUser_id()));
//提交请求
HttpResponse response = oauth.signRequest(user.getToken(), user.getToken_secret(), url, params); //这里第一个账户返回200没问题,其他全部返回403。
if(HttpStatus.SC_OK == response.getStatusLine().getStatusCode()){
try {
InputStream is = response.getEntity().getContent();
Reader reader = new BufferedReader(new InputStreamReader(is), 4000);
buffer = new StringBuilder((int)response.getEntity().getContentLength());
char[] buf = new char[1024];
int length = 0;
while((length = reader.read(buf))!=-1){ buffer.append(buf,0,length);
}
reader.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
//解析JSON数据
JSONObject data = new JSONObject(buffer.toString());
user.setUser_id(data.getString("id"));
user.setDescription(data.getString("description"));
user.setUser_name(data.getString("screen_name"));
String head_url = data.getString("profile_image_url"); user.setUser_head(getDrawbleFromUrl(head_url)); } catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
}
//请求数据
public HttpResponse signRequest(String token, String tokenSecret,
String url, List<BasicNameValuePair> params) {
HttpPost post = new HttpPost(url);
ByteArrayOutputStream bos = null;
String file = null;
try {
// 参数的编码转换为utf-8
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); for (int i = 0; i < params.size(); i++) {
BasicNameValuePair nameValuePair = (BasicNameValuePair) params
.get(i);
if (nameValuePair.getName().equals("pic")) {
file = nameValuePair.getValue();
}
} byte[] data = null;
bos = new ByteArrayOutputStream(1024 * 50);
if (!TextUtils.isEmpty(file)) {
paramToUpload(bos, params);
//设置表单类型和分隔符
post.setHeader("Content-Type", MULTIPART_FORM_DATA+ "; boundary=" + BOUNDARY);
Bitmap bf = BitmapFactory.decodeFile(file);
imageContentToUpload(bos, bf);
data = bos.toByteArray();
ByteArrayEntity formEntity = new ByteArrayEntity(data);
post.setEntity(formEntity);
} } catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
post.getParams().setBooleanParameter(
CoreProtocolPNames.USE_EXPECT_CONTINUE, false); return signRequest(token, tokenSecret, post);
} public HttpResponse signRequest(String token, String tokenSecret,
HttpPost post) {
httpOauthConsumer = new CommonsHttpOAuthConsumer(APP_KEY, APP_SECRET);
httpOauthConsumer.setTokenWithSecret(token, tokenSecret);
HttpResponse response = null;
try {
httpOauthConsumer.sign(post);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
try {
response = new DefaultHttpClient().execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
StringBuilder buffer = null;
String url = "http://api.t.sina.com.cn/users/show.json";
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("source",oauth.APP_KEY ));
params.add(new BasicNameValuePair("user_id",user.getUser_id()));
//提交请求
HttpResponse response = oauth.signRequest(user.getToken(), user.getToken_secret(), url, params); //这里第一个账户返回200没问题,其他全部返回403。
if(HttpStatus.SC_OK == response.getStatusLine().getStatusCode()){
try {
InputStream is = response.getEntity().getContent();
Reader reader = new BufferedReader(new InputStreamReader(is), 4000);
buffer = new StringBuilder((int)response.getEntity().getContentLength());
char[] buf = new char[1024];
int length = 0;
while((length = reader.read(buf))!=-1){ buffer.append(buf,0,length);
}
reader.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
//解析JSON数据
JSONObject data = new JSONObject(buffer.toString());
user.setUser_id(data.getString("id"));
user.setDescription(data.getString("description"));
user.setUser_name(data.getString("screen_name"));
String head_url = data.getString("profile_image_url"); user.setUser_head(getDrawbleFromUrl(head_url)); } catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
}
//请求数据
public HttpResponse signRequest(String token, String tokenSecret,
String url, List<BasicNameValuePair> params) {
HttpPost post = new HttpPost(url);
ByteArrayOutputStream bos = null;
String file = null;
try {
// 参数的编码转换为utf-8
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); for (int i = 0; i < params.size(); i++) {
BasicNameValuePair nameValuePair = (BasicNameValuePair) params
.get(i);
if (nameValuePair.getName().equals("pic")) {
file = nameValuePair.getValue();
}
} byte[] data = null;
bos = new ByteArrayOutputStream(1024 * 50);
if (!TextUtils.isEmpty(file)) {
paramToUpload(bos, params);
//设置表单类型和分隔符
post.setHeader("Content-Type", MULTIPART_FORM_DATA+ "; boundary=" + BOUNDARY);
Bitmap bf = BitmapFactory.decodeFile(file);
imageContentToUpload(bos, bf);
data = bos.toByteArray();
ByteArrayEntity formEntity = new ByteArrayEntity(data);
post.setEntity(formEntity);
} } catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
post.getParams().setBooleanParameter(
CoreProtocolPNames.USE_EXPECT_CONTINUE, false); return signRequest(token, tokenSecret, post);
} public HttpResponse signRequest(String token, String tokenSecret,
HttpPost post) {
httpOauthConsumer = new CommonsHttpOAuthConsumer(APP_KEY, APP_SECRET);
httpOauthConsumer.setTokenWithSecret(token, tokenSecret);
HttpResponse response = null;
try {
httpOauthConsumer.sign(post);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
try {
response = new DefaultHttpClient().execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
忘了怎么解决的。
记得问题在于,你申请app id的微博帐户肯定和你的第一个账户一样,对不?可能需要通过新浪的认证审核之类的,才能。