在走到 String authUrl = httpOauthprovider.retrieveRequestToken(
httpOauthConsumer, callBackUrl);时报错oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null**
**以下是代码**
package cn.botianxia.utils;
public class OAuth {
private CommonsHttpOAuthConsumer httpOauthConsumer;
private OAuthProvider httpOauthprovider;
public String APP_KEY;
public String APP_SECRET;
public static final String BOUNDARY = "7cd4a6d158c";
public static final String MP_BOUNDARY = "--" + BOUNDARY;
public static final String END_MP_BOUNDARY = "--" + BOUNDARY + "--";
public static final String MULTIPART_FORM_DATA = "multipart/form-data";
private static OAuth instance = null;
public static OAuth getInstance() {
if (instance == null) {
instance = new OAuth();
}
return instance;
}
private OAuth() {
// 修改成自己的APP_KEY和APP_SECRET
this("1736498531", "a398261f34b00555c1cd8db6730f1b4c");
}
private OAuth(String app_key, String app_secret) {
this.APP_KEY = app_key;
this.APP_SECRET = app_secret;
}
/**
* 请求AccessToken
*
* @param activity
* @param callBackUrl
* @return
*/
public Boolean requestAccessToken(Activity activity, String callBackUrl) {
Boolean flag = false;
try {
httpOauthConsumer = new CommonsHttpOAuthConsumer(APP_KEY,
APP_SECRET);
httpOauthprovider = new DefaultOAuthProvider(
"http://api.t.sina.com.cn/oauth/request_token",
"http://api.t.sina.com.cn/oauth/access_token",
"http://api.t.sina.com.cn/oauth/authorize");
String authUrl = httpOauthprovider.retrieveRequestToken(
httpOauthConsumer, callBackUrl);
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(authUrl)));
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public UserInfo getAccessToken(Intent intent) {
Uri uri = intent.getData();
// 处理获取返回的oauth_verifier参数
String verifier = uri
.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
httpOauthprovider.setOAuth10a(true);
httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier);
} catch (OAuthMessageSignerException ex) {
ex.printStackTrace();
} catch (OAuthNotAuthorizedException ex) {
ex.printStackTrace();
} catch (OAuthExpectationFailedException ex) {
ex.printStackTrace();
} catch (OAuthCommunicationException ex) {
ex.printStackTrace();
}
SortedSet<String> user_id = httpOauthprovider.getResponseParameters()
.get("user_id");
String userToken = httpOauthConsumer.getToken();
String userSecret = httpOauthConsumer.getTokenSecret();
UserInfo user = new UserInfo(user_id.first(), userToken, userSecret);
return user;
}
public HttpResponse signRequest(String token, String tokenSecret,
String url, List 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;
}
private static void paramToUpload(OutputStream baos,
List<BasicNameValuePair> params) {
BasicNameValuePair key = null;
for (int loc = 0; loc < params.size(); loc++) {
key = params.get(loc);
StringBuilder temp = new StringBuilder(10);
temp.setLength(0);
temp.append(MP_BOUNDARY).append("\r\n");
temp.append("content-disposition: form-data; name=\"").append(key.getName()).append("\"\r\n\r\n");
temp.append(key.getValue()).append("\r\n");
byte[] res = temp.toString().getBytes();
try {
baos.write(res);
} catch (IOException e) {
}
}
}
private static void imageContentToUpload(OutputStream out, Bitmap imgpath) {
StringBuilder temp = new StringBuilder();
temp.append(MP_BOUNDARY).append("\r\n");
temp.append("Content-Disposition: form-data; name=\"pic\"; filename=\"")
.append("temp.png").append("\"\r\n");
String filetype = "image/png";
temp.append("Content-Type: ").append(filetype).append("\r\n\r\n");
System.out.println("----2----" + temp.toString());
byte[] res = temp.toString().getBytes();
BufferedInputStream bis = null;
try {
out.write(res);
//压缩图片
imgpath.compress(CompressFormat.PNG, 75, out);
out.write("\r\n".getBytes());
out.write(("\r\n" + END_MP_BOUNDARY).getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bis) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
**以下是调用的activity**
package cn.botianxia;
public class OAuthActivity extends Activity {
/** Called when the activity is first created. */
private OAuth oauth;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(OAuthActivity.this, "开始执行", Toast.LENGTH_LONG).show();
// 是否去授权
new Builder(OAuthActivity.this)
.setTitle("是否去新浪授权")
.setPositiveButton("确定",new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String callBackUrl = "weitianxia://bogujin";
oauth = OAuth.getInstance();
oauth.requestAccessToken(OAuthActivity.this, callBackUrl);
}
})
.show();
}
}
|
|