当前位置:网站首页>Connect with third-party QQ login
Connect with third-party QQ login
2022-07-29 03:33:00 【Young people in Baima town】
stay qq Login and create applications on the Internet ,【 Have registered developer identity 】QQ interconnection , Then download the corresponding sdk, To download
Then decompress .
Create a project 【 Or in the original project 】, And put open-sdk.jar( The file name in the compressed package is :open_sdk_xxxx_lite.jar) File copy to libs( or lib) Under the table of contents , As shown in the figure below :

Then configure AndroidManifest;
In application AndroidManifest.xml Add configured <application> Add the following configurations under the node ( notes : Failure to configure will result in failure to call API);
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application>
<activity
android:name="com.tencent.tauth.AuthActivity"
android:noHistory="true"
android:launchMode="singleTask" >
<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="tencent Yours AppId" />
If your appid yes 12345
<data android:scheme="tencent12345 />
</intent-filter>
</activity>
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="behind"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<application>Then initialize SDK
3.5.7 The new version interface provides the user with the ability to set whether the user is authorized to obtain the device information , Calling interconnection SDK Before relevant functional interfaces , The application needs to confirm that the user has authorized the application to obtain the device information , Call the following code to notify SDK:
Tencent.setIsPermissionGranted(true);
If the interface is not called or the parameter is false when , Calling other function interfaces will directly return failure .
Sample code
public class OneClickLoginActivity extends AppCompatActivity {
private static final String TAG = "OneClickLoginActivity";
private static final String APP_ID = "123456";// Yours qq On the Internet appid
@BindView(R.id.ll_qq_login)
LinearLayout llQqLogin;
@BindView(R.id.iv_wb_icon)
ImageView ivWbIcon;
@BindView(R.id.tv_wb)
TextView tvWb;
public static Tencent mTencent;
//QQ Package name
private static final String PACKAGE_QQ = "com.tencent.mobileqq";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one_click_login);
Tencent.setIsPermissionGranted(true);
ButterKnife.bind(this);
mTencent = Tencent.createInstance(APP_ID, this.getApplicationContext());
}
@OnClick({R.id.ll_qq_login})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.ll_qq_login:
if (!hasApp(PACKAGE_QQ)) {
Toast.makeText(OneClickLoginActivity.this, " Not installed QQ application ",
Toast.LENGTH_SHORT).show();
return;
}
loginQQ();
break;
}
}
/**
* QQ Sign in
*/
private IUiListener listener;
private void getUnionId() {
if (mTencent != null && mTencent.isSessionValid()) {
IUiListener listener = new IUiListener() {
@Override
public void onError(UiError e) {
Toast.makeText(OneClickLoginActivity.this, "onError", Toast.LENGTH_LONG).show();
}
@Override
public void onComplete(final Object response) {
if (response != null) {
JSONObject jsonObject = (JSONObject) response;
try {
String unionid = jsonObject.getString("unionid");
} catch (Exception e) {
Toast.makeText(OneClickLoginActivity.this, "no unionid", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(OneClickLoginActivity.this, "no unionid", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancel() {
Toast.makeText(OneClickLoginActivity.this, "onCancel", Toast.LENGTH_LONG).show();
}
@Override
public void onWarning(int i) {
}
};
UnionInfo unionInfo = new UnionInfo(this, mTencent.getQQToken());
unionInfo.getUnionId(listener);
} else {
Toast.makeText(this, "please login frist!", Toast.LENGTH_LONG).show();
}
}
private void loginQQ() {
listener = new IUiListener() {
@Override
public void onComplete(Object object) {
Log.e(TAG, " Login successful : " + object.toString());
JSONObject jsonObject = (JSONObject) object;
try {
// obtain token、expires、openId Equal parameter
String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN);
String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN);
String openId = jsonObject.getString(Constants.PARAM_OPEN_ID);
mTencent.setAccessToken(token, expires);
mTencent.setOpenId(openId);
Log.e(TAG, "token: " + token);
Log.e(TAG, "expires: " + expires);
Log.e(TAG, "openId: " + openId);
// obtain UnionId
getUnionId();
// Get personal information
getQQInfo();
} catch (Exception e) {
}
}
@Override
public void onError(UiError uiError) {
// Login failed
Log.e(TAG, " Login failed " + uiError.errorDetail);
Log.e(TAG, " Login failed " + uiError.errorMessage);
Log.e(TAG, " Login failed " + uiError.errorCode + "");
}
@Override
public void onCancel() {
// Sign in cancellation
Log.e(TAG, " Sign in cancellation ");
}
@Override
public void onWarning(int i) {
}
};
//context Context 、 The second parameter SCOPO It's a String String of type , Indicates some permissions
// Applications need permission , from “,” Separate . for example :SCOPE = “get_user_info,add_t”; Use all permissions “all”
// The third parameter is the event listener
mTencent.login(this, "all", listener);
// Sign out
//mTencent.logout(this);
}
/**
* obtain QQ Personal information
*/
private void getQQInfo() {
// Get basic information
QQToken qqToken = mTencent.getQQToken();
UserInfo info = new UserInfo(this, qqToken);
info.getUserInfo(new IUiListener() {
@Override
public void onComplete(Object object) {
try {
Log.e(TAG, " Personal information :" + object.toString());
// Head portrait
String avatar = ((JSONObject) object).getString("figureurl_2");
String nickName = ((JSONObject) object).getString("nickname");
if (!avatar.equals("")) {
// Need to add Glide Library for picture display Glide.with(OneClickLoginActivity.this).load(avatar).into(ivWbIcon);
}
tvWb.setText(nickName);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(UiError uiError) {
}
@Override
public void onCancel() {
}
@Override
public void onWarning(int i) {
}
});
}
/**
* Callback is essential , Official documents do not state
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// tencent QQ Callback
Tencent.onActivityResultData(requestCode, resultCode, data, listener);
if (requestCode == Constants.REQUEST_API) {
if (resultCode == Constants.REQUEST_LOGIN) {
Tencent.handleResultData(data, listener);
}
}
}
/**
* true Installed with the corresponding package name app
*/
private boolean hasApp(String packName) {
boolean is = false;
List<PackageInfo> packages = getPackageManager()
.getInstalledPackages(0);
for (PackageInfo packageInfo : packages) {
// The judgment system / Non system applications
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) // Non system applications
{
String packageName = packageInfo.packageName;
if (packageName.equals(packName)) {
is = true;
}
} else {
// System application
}
}
return is;
}
}
notes . obtain unionid stay QQ The Internet is not enabled by default , You need to start it manually , It's down there 【 Need to bind 】

边栏推荐
- Ten thousand words detailed Google play online application standard package format AAB
- How does DataGrid export and recover the entire database data, using a single SQL file
- 最新二开版漫画小说听书三合一完整源码/整合免签接口/搭建教程/带采集接口
- makefile详解
- Simple code implementation of decision tree
- Precautions for using latex
- Matlab learning - accumulation of small knowledge points
- Suffix automata (SAM) board from Jly
- RTP 发送 和接收 h265
- Kubernetes-1.24.x feature
猜你喜欢

Violence recursion to dynamic programming 01 (robot movement)

Rdkit I: using rdkit to screen the structural characteristics of chemical small molecules

Realize multi-level linkage through recursion

ShardingSphere之水平分表实战(三)

AI platform, AI midrange architecture

(nowcoder22529c) diner (inclusion exclusion principle + permutation and combination)

通过递归实现多级联动

How to solve the time zone problem in MySQL timestamp

暴力递归到动态规划 01 (机器人移动)

容斥原理
随机推荐
Various minor problems of jupyter notebook, configuration environment, code completion, remote connection, etc
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
Simple understanding of Poe and UPS Technology
(2022杭电多校三)1011-Link is as bear(思维+线性基)
RTP send and receive h265
Machine learning based on deepchem
Bingbing learning notes: operator overloading -- implementation of date class
Install the packet capturing certificate
Kubernetes-1.24.x feature
深入C语言(1)——操作符与表达式
Introduction and advanced MySQL (13)
Understanding of p-type problems, NP problems, NPC problems, and NP hard problems in natural computing
C language programming | exchange binary odd and even bits (macro Implementation)
实例搭建Flask服务(简易版)
(nowcoder22529C)dinner(容斥原理+排列组合)
Learn exkmp again (exkmp template)
暴力递归到动态规划 01 (机器人移动)
Military product development process - transition phase
Suffix automata (SAM) board from Jly
CUDA GDB prompt: /tmp/tmpxft**** cudafe1.stub. c: No such file or directory.