使用第三方APPKey授权 跳转登录 QQ
1. 导包添加依赖:
(1)gson包解析数据;
(2)glide包加载图片;
(3)open_sdk_r5886_lite.jar包 第三方key。
2. 登录页面:
import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Toast; import com.google.gson.Gson; import com.tencent.connect.UserInfo; import com.tencent.connect.auth.QQToken; import com.tencent.connect.common.Constants; import com.tencent.tauth.IUiListener; import com.tencent.tauth.Tencent; import com.tencent.tauth.UiError; import org.json.JSONException; import org.json.JSONObject; //登录页面 public class MainActivity extends AppCompatActivity {private static final String APP_ID = "1105602574";//官方获取的APPID private static final String TAG = "MainActivity";private BaseUiListener mIUiListener;private UserInfo mUserInfo;private Tencent mTencent;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//传入参数APPID和全局Context上下文 mTencent = Tencent.createInstance(APP_ID,MainActivity.this.getApplicationContext());}public void buttonLogin(View v){/**通过这句代码,SDK实现了QQ的登录,这个方法有三个参数,第一个参数是context上下文,第二个参数SCOPO 是一个String类型的字符串,表示一些权限 官方文档中的说明:应用需要获得哪些API的权限,由“,”分隔。例如:SCOPE = “get_user_info,add_t”;所有权限用“all” 第三个参数,是一个事件监听器,IUiListener接口的实例,这里用的是该接口的实现类 */ mIUiListener = new BaseUiListener();//all表示获取所有权限 mTencent.login(MainActivity.this,"all", mIUiListener);}/** * 自定义监听器实现IUiListener接口后,需要实现的3个方法 * onComplete完成 onError错误 onCancel取消 */ private class BaseUiListener implements IUiListener {@Override public void onComplete(Object response) {Toast.makeText(MainActivity.this, "授权成功", Toast.LENGTH_SHORT).show();Log.e(TAG, "response:" + response);JSONObject obj = (JSONObject) response;try {String openID = obj.getString("openid");String accessToken = obj.getString("access_token");String expires = obj.getString("expires_in");mTencent.setOpenId(openID);mTencent.setAccessToken(accessToken,expires);QQToken qqToken = mTencent.getQQToken();mUserInfo = new UserInfo(getApplicationContext(),qqToken);mUserInfo.getUserInfo(new IUiListener() {@Override public void onComplete(final Object response) {//用户登录成功后会返回用户的数据,在此可以封装成bean类,跳转传值,设置用户的基本信息 Log.i(TAG,"登录成功"+response.toString());runOnUiThread(new Runnable() {@Override public void run() {//跳转传值,设置用户信息 UserBean userBean = new Gson().fromJson(response.toString(), UserBean.class);Intent intent = new Intent(MainActivity.this,SecondActivity.class);intent.putExtra("headPhoto",userBean.getFigureurl_qq_1());intent.putExtra("name",userBean.getNickname());intent.putExtra("gender",userBean.getGender());startActivity(intent);}});}@Override public void onError(UiError uiError) {Log.e(TAG,"登录失败"+uiError.toString());}@Override public void onCancel() {Log.e(TAG,"登录取消");}});} catch (JSONException e) {e.printStackTrace();}}@Override public void onError(UiError uiError) {Toast.makeText(MainActivity.this, "授权失败", Toast.LENGTH_SHORT).show();}@Override public void onCancel() {Toast.makeText(MainActivity.this, "授权取消", Toast.LENGTH_SHORT).show();}}/** * 在调用Login的Activity或者Fragment中重写onActivityResult方法 * @param requestCode * @param resultCode * @param data */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {if(requestCode == Constants.REQUEST_LOGIN){Tencent.onActivityResultData(requestCode,resultCode,data,mIUiListener);}super.onActivityResult(requestCode, resultCode, data);} }
3.用户登录成功后会返回用户的数据,可以封装成bean类(此类根据自己需求判断是否进行添加)
/** * 用户登录成功后返回的数据 */ public class UserBean { private String is_yellow_year_vip;private int ret;private String figureurl_qq_1;private String figureurl_qq_2;private String nickname;private String yellow_vip_level;private int is_lost;private String msg;private String city;private String figureurl_1;private String vip;private String level;private String figureurl_2;private String province;private String is_yellow_vip;private String gender;private String figureurl;public String getIs_yellow_year_vip() {return is_yellow_year_vip;}public void setIs_yellow_year_vip(String is_yellow_year_vip) {this.is_yellow_year_vip = is_yellow_year_vip;}public int getRet() {return ret;}public void setRet(int ret) {this.ret = ret;}public String getFigureurl_qq_1() {return figureurl_qq_1;}public void setFigureurl_qq_1(String figureurl_qq_1) {this.figureurl_qq_1 = figureurl_qq_1;}public String getFigureurl_qq_2() {return figureurl_qq_2;}public void setFigureurl_qq_2(String figureurl_qq_2) {this.figureurl_qq_2 = figureurl_qq_2;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}public String getYellow_vip_level() {return yellow_vip_level;}public void setYellow_vip_level(String yellow_vip_level) {this.yellow_vip_level = yellow_vip_level;}public int getIs_lost() {return is_lost;}public void setIs_lost(int is_lost) {this.is_lost = is_lost;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getFigureurl_1() {return figureurl_1;}public void setFigureurl_1(String figureurl_1) {this.figureurl_1 = figureurl_1;}public String getVip() {return vip;}public void setVip(String vip) {this.vip = vip;}public String getLevel() {return level;}public void setLevel(String level) {this.level = level;}public String getFigureurl_2() {return figureurl_2;}public void setFigureurl_2(String figureurl_2) {this.figureurl_2 = figureurl_2;}public String getProvince() {return province;}public void setProvince(String province) {this.province = province;}public String getIs_yellow_vip() {return is_yellow_vip;}public void setIs_yellow_vip(String is_yellow_vip) {this.is_yellow_vip = is_yellow_vip;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getFigureurl() {return figureurl;}public void setFigureurl(String figureurl) {this.figureurl = figureurl;} }
4. 设置用户信息的页面
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide;//显示用户数据的页面 public class SecondActivity extends AppCompatActivity{private ImageView headPhoto;private TextView userName;private TextView userSex;private Button outLogin;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);initView(); //初始化数据 //设置用户信息 Intent intent=getIntent();String photo = intent.getStringExtra("headPhoto");String name= intent.getStringExtra("name");String sex= intent.getStringExtra("gender");Glide.with(this).load(photo).into(headPhoto);userName.setText(" "+name);userSex.setText(" "+sex);}private void initView() {headPhoto = (ImageView) findViewById(R.id.headPhoto);userName = (TextView) findViewById(R.id.name);userSex = (TextView) findViewById(R.id.sex);outLogin = (Button) findViewById(R.id.outLogin);outLogin.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View view) {Intent intent=new Intent(SecondActivity.this,MainActivity.class);startActivity(intent);finish();}});} }
5. 登录页面布局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.rikao09.MainActivity"><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击QQ登录" android:onClick="buttonLogin" android:layout_centerInParent="true" android:textSize="16sp" android:textColor="#f4736e"/></RelativeLayout>
6.用户信息页面布局: activity_second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="36dp" android:text="用户信息" /><ImageView android:src="@drawable/qq" android:id="@+id/headPhoto" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="36dp" android:layout_gravity="center_horizontal"/><LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="36dp" android:orientation="horizontal"><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="性别:" android:textSize="20dp" /><TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" /></LinearLayout><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="108dp" android:orientation="horizontal"><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="年龄:" android:textSize="20dp" /><TextView android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" /></LinearLayout><Button android:text="退出登录" android:id="@+id/outLogin" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
7. 最后设置清单文件:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bwie.thirdlogin_qq"><!-- QQ登录授权所需权限 --> <uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><application android:allowBackup="true" android:icon="@drawable/qq" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!-- 注册SDKActivity --> <activity android:name="com.tencent.tauth.AuthActivity" android:launchMode="singleTask" android:noHistory="true"><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="tencent1105602574" /><!-- 开放平台获取的APPID --> </intent-filter></activity><activity android:name="com.tencent.connect.common.AssistActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" /><activity android:name=".SecondActivity"></activity></application></manifest>
使用第三方APPKey授权 跳转登录 QQ相关推荐
- 【QQ邮箱第三方客户端设置】Outlook登录QQ邮箱报错,解决方案。
@[TOC]Outlook登录QQ邮箱报错,解决方案. 问题描述: Outlook添加或者登录QQ邮箱时,出现如如正确的邮箱密码,仍报错的情况,反复确认密码输入无误. 原因: 因为QQ邮箱本身推出了& ...
- 微信公共号系列---快速整合微信多端页面授权之单点登录
本人从事互联网项目java开发五年,会java,微信公共号开发,python,nodejs,爬虫等技术,对发票项目很熟悉,如果想交一个朋友请加QQ技术交流群@群主吧:131831533 .本文章来源于 ...
- android qq三方登录授权失败,QQ第三方登陆授权失败110401原因及解决办法分享
qq第三方登陆授权失败出现代码110401是什么情况?遇到这种情况该如何解决?相信很多用户们在操作的时候都出现过类似的情况吧?下面是小编带来的攻略解析,一起来关注下! qq第三方登陆授权失败11040 ...
- 第三方平台授权登录— —QQ登录
第三方平台授权登录- -QQ登录 本来前两天开开心心!心情乐乐!居然因为自己没有仔细看代码.居然被埋在这里两天!呜呜!怒火中烧!不行,我要去消灭 五碗大米饭 来平复我的心情! <程序运行图> ...
- android qq三方登录授权失败,教大家qq第三方登陆授权失败110401怎么办的解决方法...
今天小编来给大家针对这个教大家qq第三方登陆授权失败110401怎么办的解决方法的问题来进行一个介绍,毕竟当下也是有诸多的小伙伴对于教大家qq第三方登陆授权失败110401怎么办的解决方法这个问题非常 ...
- php第三方扣扣登陆,thinkphp3.2 实现qq第三方登录
最近实现了QQ登录,qq第三方登录步骤就是首先获取appid,appkey,填写callback: 拼接登录url,带上自己的应用信息, 通过callback地址获取token,通过token获取登陆 ...
- [转载]iOS开发之第三方登录QQ
转载自 iOS_developer_zhong 1. 申请腾讯开发账号. 地址:点击打开链接 2. 下载最新的SDK 地址: SDK下载 SDK内容如下: 1.sample 这个是简单的demo ...
- Springboot网站第三方登录——QQ登录
Springboot网站第三方登录--QQ登录 这段时间为了做这几个第三方登录,走了很多弯路,跳了很多坑,为以后使用特地记录下来. 由于做了多个登录,所以代码做了一定程度的封装,大致如下: //多个登 ...
- Spring Security渐入佳境(四) -- 第三方应用授权登录
(一)前言 (1.1)什么是单点登录? 单点登录(Single Sign On,简称SSO),它的用途在于,不管多么复杂的应用群,只要在用户权限范围内,那么就可以做到,用户只需要登录一次就可以访问权限 ...
最新文章
- controller不跳转页面的几个原因_光知道SpringBoot,不用thymeleaf就太不对了
- Express4.x API (四):Router (译)
- 车辆计数--FCN-rLSTM: Deep Spatio-Temporal Neural Networks for Vehicle Counting in City Cameras
- SSM中通过okhttp3向接口发送xml格式的请求参数
- Quartz.net 开源job调度框架(二)----定点执行
- 如何修改oracle字段类型
- hbase的2.2.4版本支持哪个版本的hadoop_Hadoop 2.7 不停服升级到 3.2 在滴滴的实践
- maven netty 配置_使用Springboot整合开发Netty(一个表白的小案例)
- 如何在没有域的环境中搭建AlwaysOn(二)
- linux grep egrep fgrep bash条件判断 bash测试 if 条件判断
- 程序员代码面试指南读书笔记1
- 程序流程图的基本画法大全
- Xposed 框架检测机制
- 做SEO优化网站跳出率太高怎么办
- 快手的未来,没有宿华
- QT实现简单的上位机软件
- BZOJ4372: 烁烁的游戏
- Android 适配Dark Theme(暗黑模式),看完跪了
- MySQL数据库的管理工具
- Uva509 RAID
热门文章
- 金豺优化(GJO)算法(含MATLAB代码)
- idea打包出错时出现 Cleaning up unclosed ZipFile for archive?
- PowerPoint PIA中Application没有RecentFile属性的解决方案
- 智能信息处理大学生科技实践与创新(3I)工作室
- python花萼长度表_读取iris数据集中的花萼长度数据(已保存为csv格式),并对其进行排序、去重,并求出和、累积和、均值、标准差、方差、最小值、最大值...
- oracle数据库 移动,在ORACLE中移动数据库文件
- 源发行版本 17 需要目标发行版 17
- STM32外扩SRAM芯片IS62wv51216兼容替换
- 高等数学笔记:关于等价无穷小替换的一个猜想
- 【python】python 如何跳过异常继续执行