使用第三方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>
相關文章
- 關於QQ授權登入
- Java QQ授權第三方登陸Java
- laravel使用EasyWeChat 授權登入Laravel
- Android 第三方登入之新浪微博授權登入Android
- php使用第三方QQ登入PHP
- JAVA接入支付寶授權第三方登入Java
- 第三方微信登入 | 靜默授權與網頁授權的實現網頁
- 微信授權登入
- .netcore第三方登入授權:10分鐘急速接入NetCore
- 基於 QQ 第三方登入
- JWT登入鑑權:避免在使用者操作的過程中JWT到期跳轉登入JWT
- java 微信授權登入配置Java
- 使用釘釘Oauth2授權登入Odoo配置OAuthOdoo
- Java實現QQ第三方登入Java
- qq郵箱授權碼如何獲取 QQ郵箱授權碼怎麼弄
- 微信小程式的授權登入微信小程式
- laravel實現第三方qq一鍵登入Laravel
- QQ第三方登入認證流程(乾貨)
- Blazor OIDC 單點登入授權例項7 - Blazor hybird app 端授權BlazorAPP
- <span>小程式授權登入彈框</span>
- Java微信授權登入小程式介面Java
- 「新手上路」Go 微博授權登入Go
- 客服系統配置抖音開放平臺,實現授權登入回覆私信和評論 實現授權登入,為授權使用者管理回覆私信和評論
- 微信小程式授權登入獲取使用者資訊微信小程式
- 簡單實現第三方qq登入和分享
- Ant design pro使用(五):未登入時自動跳轉到登入頁,登入之後不再返回登入頁
- ajax 實現微信網頁授權登入網頁
- 微信小程式授權登入最佳實踐微信小程式
- 微信網頁授權登入(c# Webform)網頁C#WebORM
- 認證授權:IdentityServer4 - 單點登入IDEServer
- golang 基於 jwt 實現的登入授權GolangJWT
- uni-app 微信小程式授權登入APP微信小程式
- 鴻蒙Navigation攔截器實現頁面跳轉登入鑑權方案鴻蒙Navigation
- 快速搭建Node.js(Express)使用者註冊、登入以及授權Node.jsExpress
- IDEA支付寶小程式開發流程——授權登入Idea
- 小程式登入、微信網頁授權(Java版)網頁Java
- C#微信網頁授權登入(NET MVC)C#網頁MVC
- .NET Core企業微信網頁授權登入網頁
- ADFS3.0/4.0 訪問登入頁跳轉到登出介面後再跳轉回登入頁的方法S3