package com.mzba.peng;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.security.KeyStore;
import java.util.List;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import commonshttp.CommonsHttpOAuthConsumer;
import commonshttp.CommonsHttpOAuthProvider;
public class OAuth {
public static final String TAG = OAuth.class.getCanonicalName();
public static final String SIGNATURE_METHOD = "HMAC-SHA1";
public static OAuthConsumer consumer;
public static OAuthProvider provider;
public String consumerKey;
public String consumerSecret;
public String verifier;
private String weiboId;
public static VerfierRecivier reciver;
public OAuth(String weiboId, String consumerKey, String consumerSecret){
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
this.weiboId = weiboId;
}
public String requestAccessToken(Activity activity, String callbackurl,
String requestToken, String accessToken, String authorization) {
consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
provider = new CommonsHttpOAuthProvider(requestToken, accessToken, authorization);
String authUrl = "";
try {
if (weiboId.equals(SystemConfig.TENCENT_WEIBO)) {
authUrl = provider.retrieveRequestTokenForTencent(consumer, callbackurl);
} else {
authUrl = provider.retrieveRequestToken(consumer, callbackurl);
}
} catch (Exception e) {
e.printStackTrace();
}
Log.i(OAuth.class.getCanonicalName(), "token:" + consumer.getToken() + ",tokenSecret:" + consumer.getTokenSecret());
Intent intent = new Intent();
Bundle extras = new Bundle();
extras.putString("url", authUrl);
intent.setClass(activity.getApplicationContext(),WebViewActivity.class);
intent.putExtras(extras);
activity.startActivity(intent);
IntentFilter filter = new IntentFilter();
filter.addAction(MyAction.VERIFIER);
reciver = new VerfierRecivier();
activity.registerReceiver(reciver, filter);
return authUrl;
}
public HttpURLConnection signRequest(String token, String tokenSecret, HttpURLConnection conn) {
consumer = new DefaultOAuthConsumer(consumerKey, consumerSecret);
consumer.setTokenWithSecret(token, tokenSecret);
try {
consumer.sign(conn);
} catch (OAuthMessageSignerException e1) {
e1.printStackTrace();
} catch (OAuthExpectationFailedException e1) {
e1.printStackTrace();
} catch (OAuthCommunicationException e1) {
e1.printStackTrace();
}
try {
conn.connect();
} catch (IOException e) {
e.printStackTrace();
}
return conn;
}
public HttpResponse signRequest(String token, String tokenSecret,
String url, List<BasicNameValuePair> params) {
HttpPost post = new HttpPost(url);
try {
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 关闭Expect:100-Continue握手
// 100-Continue握手需谨慎使用,因为遇到不支持HTTP/1.1协议的服务器或者代理时会引起问题
post.getParams().setBooleanParameter(
CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
return signRequest(token, tokenSecret, post);
}
public HttpResponse signRequest(String token, String tokenSecret,
HttpPost post) {
consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
consumer.setTokenWithSecret(token, tokenSecret);
HttpResponse response = null;
try {
consumer.sign(post);
} catch (OAuthMessageSignerException e1) {
e1.printStackTrace();
} catch (OAuthExpectationFailedException e1) {
e1.printStackTrace();
} catch (OAuthCommunicationException e1) {
e1.printStackTrace();
}
// 取得HTTP response
try {
response = (HttpResponse) getNewHttpClient().execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
public HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
public class VerfierRecivier extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
UserInfo userinfo = new UserInfo();
Bundle bundle = intent.getExtras();
if(bundle != null){
verifier = bundle.getString("verifier");
Log.i(VerfierRecivier.class.getCanonicalName(), "oauth:" + verifier);
try{
provider.setOAuth10a(true);
if (weiboId.equals(SystemConfig.TENCENT_WEIBO)) {
provider.retrieveAccessTokenForTencent(consumer, verifier);
} else {
provider.retrieveAccessToken(consumer, verifier);
}
}catch(Exception e){
e.printStackTrace();
}
String userKey = consumer.getToken();
String userSecret = consumer.getTokenSecret();
Log.i(VerfierRecivier.class.getCanonicalName(), "userKey:" + userKey);
Log.i(VerfierRecivier.class.getCanonicalName(), "userSecret:" + userSecret);
userinfo.setToken(userKey);
userinfo.setTokenSecret(userSecret);
userinfo.setWeiboId(weiboId);
Intent intentAuthorize = new Intent();
intentAuthorize.setClass(context, AuthorizeActivity.class);
intentAuthorize.putExtra("userinfo", userinfo);
context.startActivity(intentAuthorize);
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
OAuthForAllWeibo.zip (96个子文件)
OAuthForAllWeibo
AndroidManifest.xml 981B
res
layout
inputverifier.xml 2KB
webview.xml 406B
accountbind.xml 6KB
main.xml 431B
drawable-ldpi
icon.png 2KB
drawable-mdpi
icon.png 3KB
drawable-hdpi
success.png 6KB
blog_bg_list_mid.xml 455B
detail_index_select.xml 393B
blog_list_above.9.png 783B
sohu_icon.png 7KB
blog_list_mid.9.png 368B
titlebar_lightgray_bg.png 294B
blog_list_above_sel.9.png 829B
blog_list_mid_sel.9.png 234B
blog_list_below.9.png 755B
tencent_icon.png 47KB
title_button_normal.png 928B
netease_icon.png 3KB
title_home_selected.png 1KB
blog_list_above_nor.9.png 796B
title_button_selected.png 928B
blog_bg_list_below.xml 467B
blog_bg_list_above.xml 467B
icon.png 4KB
sina_icon.png 6KB
detail_back_select.xml 399B
title_home_normal.png 1KB
blog_list_below_sel.9.png 763B
blog_list_below_nor.9.png 628B
values
strings.xml 180B
proguard.cfg 1KB
src
com
mzba
peng
SystemConfig.java 4KB
AccountBindActivity.java 4KB
UserInfo.java 2KB
MyAction.java 179B
ConfigHelper.java 265B
OAuth.java 8KB
AuthorizeActivity.java 3KB
SSLSocketFactoryEx.java 2KB
InputVerifierActivity.java 2KB
WebViewActivity.java 3KB
bin
classes
com
mzba
peng
OAuth$VerfierRecivier.class 3KB
WebViewActivity$2.class 3KB
InputVerifierActivity$1.class 3KB
SystemConfig.class 4KB
AccountBindActivity.class 3KB
MyAction.class 361B
WebViewActivity.class 3KB
R.class 496B
OAuth.class 9KB
WebViewActivity$1.class 784B
MainActivity.class 870B
AuthorizeActivity.class 3KB
R$layout.class 482B
R$attr.class 325B
WebViewActivity$JavaScriptInterface.class 823B
ConfigHelper.class 437B
R$id.class 1KB
InputVerifierActivity.class 1KB
R$string.class 412B
SSLSocketFactoryEx$1.class 1KB
SSLSocketFactoryEx.class 2KB
UserInfo.class 2KB
R$drawable.class 1KB
res
drawable-ldpi
icon.png 2KB
drawable-mdpi
icon.png 2KB
drawable-hdpi
success.png 6KB
blog_list_above.9.png 929B
sohu_icon.png 4KB
blog_list_mid.9.png 442B
titlebar_lightgray_bg.png 294B
blog_list_above_sel.9.png 939B
blog_list_mid_sel.9.png 302B
blog_list_below.9.png 866B
tencent_icon.png 2KB
title_button_normal.png 928B
netease_icon.png 3KB
title_home_selected.png 1KB
blog_list_above_nor.9.png 929B
title_button_selected.png 928B
icon.png 4KB
sina_icon.png 3KB
title_home_normal.png 1KB
blog_list_below_sel.9.png 877B
blog_list_below_nor.9.png 755B
classes.dex 112KB
OAuthForAllWeibo.apk 145KB
resources.ap_ 50KB
.classpath 414B
assets
project.properties 360B
.project 852B
gen
com
mzba
peng
R.java 4KB
lib
oauth-signpost-1126.jar 93KB
commons-codec.jar 46KB
共 96 条
- 1
baken99
- 粉丝: 5
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页