基于oauth2.0协议,实现了QQ互联目前开放的所有接口,例如创建相册、上传照片、发表说说、同步动态等等 均已实现,并亲测通过。package com.tencent.tauthdemo;
import Android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import com.tencent.tauth.TAuthView;
import com.tencent.tauth.TencentOpenAPI;
import com.tencent.tauth.TencentOpenRes;
import com.tencent.tauth.bean.OpenId;
import com.tencent.tauth.http.Callback;
import com.tencent.tauth.http.TDebug;
import com.tencent.tauthdemo.clicklistener.AddAlbumClickListener;
import com.tencent.tauthdemo.clickListener.AddShareClickListener;
import com.tencent.tauthdemo.clickListener.AddTopicClickListener;
import com.tencent.tauthdemo.clickListener.GetuserInfoClickListener;
import com.tencent.tauthdemo.clickListener.GetUserProfileClickListener;
import com.tencent.tauthdemo.clickListener.ListAlbumClickListener;
import com.tencent.tauthdemo.clickListener.UploadPicClickListener;
public class TAuthDemoActivity extends Activity {
private static final String tag="TAuthDemoActivity";
public static final int REQUEST_PICK_PICTURE = 1001;
/**
* <p>不能包含特殊字符“#”</br>
* 不能是浏览器能识别的协议,比如:http://auth.qq.com</br>
* 不设置时默认使用: auth://tauth.qq.com/</br></br>
* 不区分大小写。在Manifest设置的scheme必须是小写</br>
* 在Manifest中设置intent-filter:data android:scheme="auth"</p>
* 例如:</br>
* <intent-filter> </br>
* <action android:name="android.intent.action.VIEW" /> </br>
* <category android:name="android.intent.category.DEFAULT" /> </br>
* <category android:name="android.intent.category.BROWSABLE" /> </br>
* <data android:scheme="auth"/> </br>
* </intent-filter> </br>
*
*/
private static final String CALLBACK = "tencentauth://auth.qq.com";
public String mAppid = "222222";//申请时分配的appid
private String scope = "get_user_info,get_user_profile,add_share,add_topic,list_album,upload_pic,add_album";//授权范围
private AuthReceiver receiver;
public String mAccessToken, mOpenId;
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView loginBtn = (ImageView) findViewById(R.id.login);
loginBtn.seTimageDrawable(TencentOpenRes.getBigLoginBtn(getAssets()));
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAccessToken = null;
mOpenId = null;
((TextView)findViewById(R.id.access_token)).setText("");
((TextView)findViewById(R.id.openid)).setText("");
auth(mAppid, "_self");
}
});
ImageView loginBBtn = (ImageView) findViewById(R.id.login_browser);
loginBBtn.setImageDrawable(TencentOpenRes.getLoginBtn(getAssets()));
loginBBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAccessToken = null;
mOpenId = null;
((TextView)findViewById(R.id.access_token)).setText("");
((TextView)findViewById(R.id.openid)).setText("");
auth(mAppid, "_blank");
}
});
registerIntentReceivers();
findViewById(R.id.get_user_info).setOnClickListener(new GetUserInfoClickListener(this));
findViewById(R.id.get_user_profile).setOnClickListener(new GetUserProfileClickListener(this));
findViewById(R.id.add_share).setOnClickListener(new AddShareClickListener(this));
findViewById(R.id.add_topic).setOnClickListener(new AddTopicClickListener(this));
findViewById(R.id.list_album).setOnClickListener(new ListAlbumClickListener(this));
findViewById(R.id.upload_pic).setOnClickListener(new UploadPicClickListener(this));
findViewById(R.id.add_album).setOnClickListener(new AddAlbumClickListener(this));
}
/**
* 打开登录认证与授权页面
*
* @param String clientId 申请时分配的appid
* @param String target 打开登录页面的方式:“_slef”以webview方式打开; "_blank"以内置安装的浏览器方式打开
* @author John.Meng<arzen1013@gmail> QQ:3440895
* @date 2011-9-5
*/
private void auth(String clientId, String target) {
Intent intent = new Intent(TAuthDemoActivity.this, com.tencent.tauth.TAuthView.class);
intent.putExtra(TAuthView.CLIENT_ID, clientId);
intent.putExtra(TAuthView.SCOPE, scope);
intent.putExtra(TAuthView.TARGET, target);
intent.putExtra(TAuthView.CALLBACK, CALLBACK);
startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (receiver != null) {
unregisterIntentReceivers();
}
}
private void registerIntentReceivers() {
receiver = new AuthReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(TAuthView.AUTH_BROADCAST);
registerReceiver(receiver, filter);
}
private void unregisterIntentReceivers() {
unregisterReceiver(receiver);
}
public void setOpenIdText(String txt) {
((TextView)findViewById(R.id.openid)).setText(txt);
mOpenId = txt;
}
/**
* 广播的侦听,授权完成后的回调是以广播的形式将结果返回
*
* @author John.Meng<arzen1013@gmail> QQ:3440895
* @date 2011-9-5
*/
public class AuthReceiver extends BroadcastReceiver {
private static final String TAG="AuthReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Bundle exts = intent.getExtras();
String raw = exts.getString("raw");
String access_token = exts.getString(TAuthView.ACCESS_TOKEN);
String expires_in = exts.getString(TAuthView.EXPIRES_IN);
String error_ret = exts.getString(TAuthView.ERROR_RET);
String error_des = exts.getString(TAuthView.ERROR_DES);
Log.i(TAG, String.format("raw: %s, access_token:%s, expires_in:%s", raw, access_token, expires_in));
if (access_token != null) {
mAccessToken = access_token;
((TextView)findViewById(R.id.access_token)).setText(access_token);
new Handler().postDelayed(new Runnable() {//在最新的2.3.6系统中直接showDialog(PROGRESS)无线显示出dialog
@Override
public void run() {
showDialog(PROGRESS);
}
}, 100);
//用access token 来获取open id
TencentOpenAPI.openid(access_token, new Callback() {
@Override
public void onSuccess(final Object obj) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dismissDialog(PROGRESS);
setOpenIdText(((OpenId)obj).getOpenId());
}
});
}
@Override
public void onFail(int ret, final String msg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dismissDialog(PROGRESS);
TDebug.msg(msg, getApplicationContext());
}
});
}
});
}
if (error_ret != null) {
((TextView)findViewById(R.id.access_token)).setText("获取access token失败" "\n错误码: " error_ret "\n错误信息: " error_des);
}
}
}
public boolean satisfyConditions() {
return mAccessToken != null &&
mAppid != null &&
mOpenId != null &&
!mAccessToken.equals("") &&
!mAppid.equals("") &&
!mOpenId.equals("");
}
public static final int PROGRESS = 0;
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case PROGRESS:
dialog = new ProgressDialog(this);
((ProgressDialog)dialog).setMessage("请求中,请稍等...");
break;
}
return dialog;
}
public void showMessage(String title, String msg) {
Dialog dialog = new Dialog(TAuthDemoActivity.this);
ScrollView rootView = new ScrollView(TAuthDemoActivity.this);
TextView view = new TextView(TAuthDemoActivity.this);
view.setText(msg);
rootView.addView(view);
dialog.setContentView(rootView);
dialog.setTitle(title);
dialog.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_PICK_PICTURE: {
if (data != null) {
Uri bitmapUri = data.getData();
if (bitmapUri != null) {
UploadPicClickListener.uploadPic(this, bitmapUri);
}
}
break;
}
}
}
}
来源:搜素材网素材
搜素材网所有素材均为本站用户上传,仅供学习与参考,请勿用于商业用途,如有侵犯您的版权请联系客服服务QQ
本站提供各类html5响应式模板,前端js素材,网站模板,后台模板素材,程序源码素材。
由于技术有限本站不提供安装服务与bug修复,各类源码只提供分享服务,感谢您的理解。
如果对本站有任何意见请点击右侧侧边栏的反馈意见,我们会及时处理。