目前越來越多的app,都需要集成三方登錄(騰訊微博、新浪微博、人人等等)。這裏分享一個簡單的騰訊微博demo。廢話不多說,三步走。
第一步註冊一個賬戶,並創建一個應用。獲取app ID與 app Key。
具體地址:http://open.qq.com
第二步下載sdk
具體地址:http://wiki.opensns.qq.com/wiki/%E3%80%90QQ%E7%99%BB%E5%BD%95%E3%80%91SDK%E4%B8%8B%E8%BD%BD
第三步新建工程,修改清單文件,導入相關的sdk文件及調用相應的api搞定。
3.1 修改清單文件,主要是加入一個webview的activity
<activity
android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
android:label="@string/app_name" >
</activity>
3.2 將Android_SDK_v1.2.jar與httpmime-4.1.3.jar導入libs中就好。
3.3 在需要三方登錄的地方,調用相應的api即可。
下面是小demo工程的清單文件及activity中api代碼簡單示例。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.chesterweibodemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<!-- 允許網絡訪問 -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- demo activity,調用api -->
<activity
android:name=".ChesterWeiboDemoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- OAuth Version 2. 使用 WebView 輔助進行ImplicitGrant方式授權必須 -->
<activity
android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
ChesterWeiboDemoActivity 代碼如下:
package com.test.chesterweibodemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import com.tencent.weibo.api.UserAPI;
import com.tencent.weibo.constants.OAuthConstants;
import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.webview.OAuthV2AuthorizeWebView;
/**
* @author chensf5 2013-1-22
*/
public class ChesterWeiboDemoActivity extends Activity {
private static final String TAG = "ChesterWeiboDemoActivity";
private OAuthV2 oAuth;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//驗證回調url地址,隨便填寫個 http://www.tencent.com/zh-cn/index.shtml
oAuth = new OAuthV2("http://apk.91.com/Soft/Android/com.palmit.player-1-1.0.html");
oAuth.setClientId("801297210"); // 801115505
oAuth.setClientSecret("d163aeecdc7a9e5a601b03d66d4265be"); // be1dd1410434a9f7d5a2586bab7a6829
Intent intent = new Intent(ChesterWeiboDemoActivity.this,
OAuthV2AuthorizeWebView.class);
intent.putExtra("oauth", oAuth);
startActivityForResult(intent, 1); //開啓webview加載個html頁面
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (null != msg.obj && msg.obj instanceof String) {
String response = (String) msg.obj;
((TextView) findViewById(R.id.tv_content)).setText(response
+ "\n");
Log.i(TAG, response);
Log.i(TAG + "----------",
"redirectUri:" + oAuth.getRedirectUri() + ",clientId:"
+ oAuth.getClientId() + ",clientSecret:"
+ oAuth.getClientSecret() + ",responseType:"
+ oAuth.getResponeType() + ",type:"
+ oAuth.getType() + ",authorizeCode:"
+ oAuth.getAuthorizeCode() + ",accessToken:"
+ oAuth.getAccessToken() + ",expiresIn:"
+ oAuth.getExpiresIn() + ",grantType:"
+ oAuth.getGrantType() + ",refreshToken:"
+ oAuth.getRefreshToken() + ",openid:"
+ oAuth.getOpenid() + "," + oAuth.getOpenkey());
}
};
};
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
if (requestCode == 1) {
if (resultCode == OAuthV2AuthorizeWebView.RESULT_CODE) {
new Thread() {
public void run() {
oAuth = (OAuthV2) data.getExtras().getSerializable("oauth");
// 調用API獲取用戶信息
UserAPI userAPI = new UserAPI(
OAuthConstants.OAUTH_VERSION_2_A);
try {
String response = userAPI.info(oAuth, "json");// 獲取用戶信息
Message msg = handler.obtainMessage();
msg.obj = response;
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
userAPI.shutdownConnection();
};
}.start();
} else {
Log.i(TAG, "返回過來的不對");
}
} else {
Log.i(TAG, "沒有授權可拿");
}
}
}
Demo工程已上傳至csdn免積分下載中,尚在審覈中。過兩天有具體地址再補上。
審覈通過,csdn效率還是蠻高的,下載地址:http://download.csdn.net/detail/chenshufei2/5289129