申请开放平台账号和App key
1. 到对应的开放平台上注册账号
QQ: open.qq.com
Weibo: open.weibo.com
2. 注册好账号后选择添加移动应用,填好信息,提交审核,审核通过后会获得App key和密钥。
注:
- Android签名,下载签名工具 app_signatures.apk(如下图),安装到手机上,输入包名,即可生成Android签名,每个平台都是一样的
- 微博的授权回调页和取消授权回调页均为 http://sina.com
- 微博的Android下载地址和应用地址为你的APK在应用市场的下载页面地址
每个平台都有自己的授权API,授权流程也比较麻烦
采用ShareSDK的授权API,统一接口,调用简单。
下载地址:http://share.mob.com/Download
选择Android,点击即可下载,下载后解压。
在命令行下通过java -jar打开解压包中的ShareSDK for Android/QuickIntegrater.jar
选择要集成的平台,然后确认,会在ShareSDK for Android生成Sample文件夹,将其中的文件拷贝到你的Project中对应的地方
注意其中的AndroidManifest.xml中的内容要Add到你的AndroidManifest.xml中
1. 初始化ShareSDK
ShareSDK.initSDK(this);
2. 授权
Platform platform =ShareSDK.getPlatform(getActivity(), SinaWeibo.NAME);
platform.setPlatformActionListener(new PlatformActionListener() {
@Override
publicvoid onComplete(Platform platform,int i,HashMap<String,Object> stringObjectHashMap) {
//授权成功,并返回授权信息,在stringObjectHashMap中,打印出stringObjectHashMap,会发现你要的信息
}
@Override
publicvoid onError(Platform platform,int i,Throwable throwable) {
//授权中出现异常
}
@Override
publicvoid onCancel(Platform platform,int i) {
//用户取消了授权
}
});
platform.SSOSetting(false); //优先使用客户端登陆,为true时使用网页登陆
platform.showUser(null); //为null表示使用当前账户登陆
Platform weibo = ShareSDK.getPlatform(this, SinaWeibo.NAME);
if (weibo.isValid()) {
weibo.removeAccount();
}
细节参考Githubdemo: https://github.com/MobClub/ThirdPartyLoginDemo