in Authorization.java
Sina SDK 版
private static final String TAG = "WeiBoTong";
private final String CALLBACKURL = "myapp://mainactivity";
AccessToken accessToken = null;
RequestToken requestToken = null;
Weibo weibo = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
doOauth();
}
private void doOauth() {
// TODO Auto-generated method stub
System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
weibo = new Weibo();
// set callback url, desktop app please set to null
// http://callback_url?oauth_token=xxx&oauth_verifier=xxx
try {
requestToken = weibo.getOAuthRequestToken(CALLBACKURL);
} catch (WeiboException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v(TAG, "Got request token.");
Log.v(TAG, "Request token: "+ requestToken.getToken());
Log.v(TAG, "Request token secret: "+ requestToken.getTokenSecret());
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthorizationURL())));
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
getAccess(intent);
}
private void getAccess(Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, "onNewIntent");
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACKURL)) {
String verifier = uri.getQueryParameter("oauth_verifier");
Log.d(TAG, "onNewIntent " + " verifier " + verifier);
try {
accessToken = requestToken.getAccessToken(verifier);
} catch (WeiboException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v(TAG, "Got access token.");
Log.v(TAG, "Access token: "+ accessToken.getToken());
Log.v(TAG, "Access token secret: "+ accessToken.getTokenSecret());
List<User> list = null;
try {
list = weibo.getFriendsStatuses();
} catch (WeiboException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Successfully get Friends to [" + list + "].");
}
}
Signpost lib 版
private static String TAG = "OAuthForSina";
private CommonsHttpOAuthConsumer httpOauthConsumer;
private OAuthProvider httpOauthprovider;
public final static String consumerKey = "********";
public final static String consumerSecret = "********";
private final String CALLBACKURL = "myapp://mainactivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
doOauth();
}
private void doOauth() {
try {
httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret);
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);
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
Log.d(TAG, "sent doOauth");
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
Log.d(TAG, "doOauth Complete");
}
@SuppressWarnings({ "unused", "unchecked" })
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d(TAG, "onNewIntent");
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACKURL)) {
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Log.d(TAG, "onNewIntent " + " verifier " + verifier);
try {
httpOauthprovider.setOAuth10a(true); //之前鼓捣半天,总是返回错误,查看源码发现要设置这个标志
Log.d(TAG, "isOAuth10a = " + httpOauthprovider.isOAuth10a());
// this will populate token and token_secret in consumer
httpOauthprovider.retrieveAccessToken(httpOauthConsumer,
verifier);
} catch (OAuthMessageSignerException ex) {
ex.printStackTrace();
} catch (OAuthNotAuthorizedException ex) {
ex.printStackTrace();
} catch (OAuthExpectationFailedException ex) {
ex.printStackTrace();
} catch (OAuthCommunicationException ex) {
ex.printStackTrace();
}
String userKey = httpOauthConsumer.getToken();
String userSecret = httpOauthConsumer.getConsumerSecret();
Log.d(TAG, "Got access token.");
Log.d(TAG, "Access token: " + httpOauthConsumer.getToken());
Log.d(TAG, "Access token secret: "
+ httpOauthConsumer.getTokenSecret());
String url = "http://api.t.sina.com.cn/statuses/friends.xml";
HttpPost post = new HttpPost(url);
HttpClient httpClient = null;
HttpResponse response = null;
List params=new ArrayList();
params.add(new BasicNameValuePair("source", consumerKey));
try{
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
try {
httpOauthConsumer.sign(post);
} catch (OAuthMessageSignerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthCommunicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//取得HTTP response
try {
response = new DefaultHttpClient().execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//若状态码为200 ok
if (200 == response.getStatusLine().getStatusCode()) {
try {
InputStream is = response.getEntity().getContent();
Reader reader = new BufferedReader(new InputStreamReader(is), 4000);
StringBuilder buffer = new StringBuilder((int) response.getEntity().getContentLength());
try {
char[] tmp = new char[1024];
int l;
while ((l = reader.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
} finally {
reader.close();
}
String string = buffer.toString();
Log.d(TAG, "rs:" + string);
response.getEntity().consumeContent();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
in manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Authorization" android:label="oauth/OauthActivity"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="action_view" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" android:host="mainactivity" />
</intent-filter>
</activity>
结合了网上各种实现方式,现在可以通过oauth了,再研究研究。
设计个UI流程,改天完成自己的sina客户端