自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS 第三方登錄之QQ登錄

開發(fā) OpenHarmony
因?yàn)轼櫭上到y(tǒng)剛出不久,官方的第三方登錄SDK還沒出來,下面就介紹下在鴻蒙應(yīng)用中實(shí)現(xiàn)QQ登錄的方法(支持喚起QQ安卓客戶端進(jìn)行授權(quán))

[[438480]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

前言

因?yàn)轼櫭上到y(tǒng)剛出不久,官方的第三方登錄SDK還沒出來,下面就介紹下在鴻蒙應(yīng)用中實(shí)現(xiàn)QQ登錄的方法(支持喚起QQ安卓客戶端進(jìn)行授權(quán))

前期準(zhǔn)備

登錄QQ開放平臺(tái) > 應(yīng)用管理 > 創(chuàng)建應(yīng)用 ,創(chuàng)建一個(gè)網(wǎng)站應(yīng)用。

注意:要選擇網(wǎng)站應(yīng)用,移動(dòng)應(yīng)用和小程序不適用該方案。

編寫代碼

判斷是否已登錄

獲取登錄狀態(tài)

在入口AbilitySliceMainAbilitySlice中進(jìn)行判斷。

從數(shù)據(jù)庫(kù)獲取token的值判斷是否已經(jīng)登錄賬號(hào) (已登錄返回token,未登錄返回null)

  1. // 創(chuàng)建數(shù)據(jù)庫(kù)(這里使用官方提供的“輕量級(jí)數(shù)據(jù)存儲(chǔ)”,相關(guān)文檔:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/database-preference-guidelines-0000000000030083) 
  2. Preferences preferences = new DatabaseHelper(getApplicationContext()).getPreferences("DATA_NAME"); 
  3. //  從數(shù)據(jù)庫(kù)獲取token的值判斷是否已經(jīng)登錄賬號(hào) (已登錄返回token,未登錄返回null
  4. String token = preferences.getString("token",null); 

進(jìn)行相應(yīng)跳轉(zhuǎn)

已登錄跳轉(zhuǎn)至個(gè)人界面MyAbility,未登錄跳轉(zhuǎn)至登錄界面LoginAbility.

  1. if(token != null){ 
  2.             // 已登錄,跳轉(zhuǎn)至MyAbility 
  3.             Intent myIntent = new Intent(); 
  4.             myIntent.setParam("token", token); 
  5.             Operation myOperation = new Intent.OperationBuilder() 
  6.                     .withBundleName("cn.dsttl3.test"
  7.                     .withAbilityName("cn.dsttl3.qqlogin.MyAbility"
  8.                     .build(); 
  9.             myIntent.setOperation(myOperation); 
  10.             startAbility(myIntent); 
  11.             terminateAbility(); 
  12. }else { 
  13.             // 未登錄,跳轉(zhuǎn)至LoginAbility 
  14.             Intent loginIntent = new Intent(); 
  15.             Operation loginOperation = new Intent.OperationBuilder() 
  16.                     .withBundleName("cn.dsttl3.test"
  17.                     .withAbilityName("cn.dsttl3.qqlogin.LoginAbility"
  18.                     .build(); 
  19.             loginIntent.setOperation(loginOperation); 
  20.             startAbility(loginIntent); 
  21.             terminateAbility(); 

登錄界面的操作

申請(qǐng)網(wǎng)絡(luò)訪問權(quán)限

在config.json添加

  1. "reqPermissions": [ 
  2.       { 
  3.         "name""ohos.permission.INTERNET" 
  4.       } 
  5.     ] 

登錄界面布局文件ability_login.xml

在布局文件中添加以后webview組件

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:alignment="center" 
  7.     ohos:orientation="vertical"
  8.  
  9.     <ohos.agp.components.webengine.WebView 
  10.         ohos:id="$+id:WebView_qqlogin" 
  11.         ohos:height="match_parent" 
  12.         ohos:width="match_parent"/> 
  13.  
  14. </DirectionalLayout> 

登錄界面的AbilitySlice LoginAbilitySlice.java

需要用到的幾個(gè)常量

  1. String state = UUID.randomUUID().toString();// 唯一標(biāo)識(shí),成功授權(quán)后回調(diào)時(shí)會(huì)原樣帶回。 
  2. String client_id = "101***151";//QQ開放平臺(tái) 應(yīng)用 APP ID 
  3. String redirect_uri = "https%3A%2F%2Fapi.dsttl3.cn%2FRedis%2FQQLogin"; //應(yīng)用 網(wǎng)站回調(diào)域 需進(jìn)行url編碼,授權(quán)成功后會(huì)跳轉(zhuǎn)至該鏈接 
  4. String authorize_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code" + 
  5.             "&client_id=" + client_id + 
  6.             "&redirect_uri=" + redirect_uri + 
  7.             "&state="+ state; 

 WebView的配置

  1. WebView myWebView = (WebView) findComponentById(ResourceTable.Id_WebView_qqlogin); 
  2.         myWebView.getWebConfig().setJavaScriptPermit(true);//支持JavaScript 
  3.         myWebView.getWebConfig().setUserAgent("android");//將UserAgent設(shè)置為安卓,授權(quán)頁(yè)才顯示QQ客戶端一鍵登錄按鈕 

自定義WebAgent

當(dāng)WebView即將打開一個(gè)鏈接時(shí)調(diào)用isNeedLoadUrl方法,當(dāng)在網(wǎng)頁(yè)上點(diǎn)擊“一鍵登錄”時(shí),打開QQ客戶端

wtloginmqq是QQ安卓客戶端URL Scheme

  1. if (request.getRequestUrl().toString().startsWith("wtloginmqq")){ 
  2.                    // 打開QQ客戶端 
  3.                    Intent qqIntent = new Intent(); 
  4.                    Operation qqOperation = new Intent.OperationBuilder() 
  5.                            .withAction("android.intent.action.VIEW"
  6.                            .withUri(Uri.parse(request.getRequestUrl().toString())) 
  7.                            .build(); 
  8.                    qqIntent.setOperation(qqOperation); 
  9.                    startAbility(qqIntent); 

因?yàn)槟壳斑€找不到網(wǎng)頁(yè)端喚起鴻蒙應(yīng)用的方法,所以QQ客戶端回調(diào)的code放在自己服務(wù)器處理。

授權(quán)成功后,會(huì)打開之前在QQ開放平臺(tái)設(shè)置的回調(diào)域redirect_uri

示例:https://api.dsttl3.cn/Redis/QQLogin?code=********&state=*****

code:QQ授權(quán)返回的code,用于申請(qǐng)token

state:在webview請(qǐng)求QQ授權(quán)頁(yè)面時(shí)傳入的唯一標(biāo)識(shí),用于判斷用戶身份,方便后續(xù)從服務(wù)器請(qǐng)求token

出于安全考慮 ,請(qǐng)求token操作放在服務(wù)器上執(zhí)行。獲取到token后將token存入數(shù)據(jù)庫(kù),客戶端通過請(qǐng)求https://api.dsttl3.cn/Redis/Get?key= + state來獲取到token

客戶端請(qǐng)求到token后,將token存儲(chǔ)到數(shù)據(jù)庫(kù)

  1.  // 將token存入數(shù)據(jù)庫(kù) 
  2. Preferences preferences = new DatabaseHelper(getApplicationContext()).getPreferences("DATA_NAME"); 
  3. preferences.putString("token",token); 
  4. preferences.flush(); 

token存儲(chǔ)完成后跳轉(zhuǎn)至MyAbility

自定義WebAgent完整代碼

  1. myWebView.setWebAgent(new WebAgent(){ 
  2.             // 當(dāng)WebView即將打開一個(gè)鏈接時(shí)調(diào)用該方法 
  3.             @Override 
  4.             public boolean isNeedLoadUrl(WebView webView, ResourceRequest request) { 
  5.                 // request.getRequestUrl().toString() WebView即將打開的鏈接地址 
  6.                 if (request.getRequestUrl().toString().startsWith("wtloginmqq")){ 
  7.                     // 打開QQ客戶端 
  8.                     Intent qqIntent = new Intent(); 
  9.                     Operation qqOperation = new Intent.OperationBuilder() 
  10.                             .withAction("android.intent.action.VIEW"
  11.                             .withUri(Uri.parse(request.getRequestUrl().toString())) 
  12.                             .build(); 
  13.                     qqIntent.setOperation(qqOperation); 
  14.                     startAbility(qqIntent); 
  15.                     // 向自己的服務(wù)器請(qǐng)求token 
  16.                     new Thread(new Runnable() { 
  17.                         @Override 
  18.                         public void run() { 
  19.                             while (true){ 
  20.                                 String getTokenURL = "https://api.dsttl3.cn/Redis/Get?key=" + state; 
  21.                                 try { 
  22.                                     OkHttpClient client = new OkHttpClient(); 
  23.                                     Request request = new Request.Builder().url(getTokenURL).build(); 
  24.                                     String token = client.newCall(request).execute().body().string(); 
  25.                                     if (token.length() == 32){ 
  26.                                         getUITaskDispatcher().asyncDispatch(new Runnable() { 
  27.                                             @Override 
  28.                                             public void run() { 
  29.                                                 // 將token存入數(shù)據(jù)庫(kù) 
  30.                                                 Preferences preferences = new DatabaseHelper(getApplicationContext()).getPreferences("DATA_NAME"); 
  31.                                                 preferences.putString("token",token); 
  32.                                                 preferences.flush(); 
  33.                                                 // 跳轉(zhuǎn)至用戶界面 
  34.                                                 Intent myIntent = new Intent(); 
  35.                                                 Operation myOperation = new Intent.OperationBuilder() 
  36.                                                         .withBundleName("cn.dsttl3.test"
  37.                                                         .withAbilityName("cn.dsttl3.qqlogin.MyAbility"
  38.                                                         .build(); 
  39.                                                 myIntent.setOperation(myOperation); 
  40.                                                 startAbility(myIntent); 
  41.                                                 terminateAbility(); 
  42.                                             } 
  43.                                         }); 
  44.                                         break; 
  45.                                     } 
  46.                                     Time.sleep(1500); 
  47.                                 } catch (IOException e) { 
  48.                                     e.printStackTrace(); 
  49.                                 } 
  50.                             } 
  51.                         } 
  52.                     }).start(); 
  53.                     return false
  54.                 } 
  55.                 return true
  56.             } 
  57.         }); 

加載網(wǎng)頁(yè)

  1. myWebView.load(authorize_url); 

LoginAbilitySlice.java完整代碼

  1. import cn.dsttl3.qqlogin.ResourceTable; 
  2. import ohos.aafwk.ability.AbilitySlice; 
  3. import ohos.aafwk.content.Intent; 
  4. import ohos.aafwk.content.Operation; 
  5. import ohos.agp.components.webengine.ResourceRequest; 
  6. import ohos.agp.components.webengine.WebAgent; 
  7. import ohos.agp.components.webengine.WebView; 
  8. import ohos.data.DatabaseHelper; 
  9. import ohos.data.preferences.Preferences; 
  10. import ohos.miscservices.timeutility.Time
  11. import ohos.utils.net.Uri; 
  12. import okhttp3.OkHttpClient; 
  13. import okhttp3.Request; 
  14. import java.io.IOException; 
  15. import java.util.UUID; 
  16.  
  17. public class LoginAbilitySlice extends AbilitySlice { 
  18.  
  19.     //QQ開放平臺(tái)登錄授權(quán)文檔 https://wiki.connect.qq.com/%e5%87%86%e5%a4%87%e5%b7%a5%e4%bd%9c_oauth2-0 
  20.     String state = UUID.randomUUID().toString();// 唯一標(biāo)識(shí),成功授權(quán)后回調(diào)時(shí)會(huì)原樣帶回。 
  21.     String client_id = "101547151";//QQ開放平臺(tái) 應(yīng)用 APP ID 
  22.     String redirect_uri = "https%3A%2F%2Fapi.dsttl3.cn%2FRedis%2FQQLogin"; //應(yīng)用 網(wǎng)站回調(diào)域 需進(jìn)行url編碼,授權(quán)成功后會(huì)跳轉(zhuǎn)至該鏈接 
  23.     String authorize_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code" + 
  24.             "&client_id=" + client_id + 
  25.             "&redirect_uri=" + redirect_uri + 
  26.             "&state="+ state; 
  27.     @Override 
  28.     public void onStart(Intent intent) { 
  29.         super.onStart(intent); 
  30.         super.setUIContent(ResourceTable.Layout_ability_login); 
  31.         WebView myWebView = (WebView) findComponentById(ResourceTable.Id_WebView_qqlogin); 
  32.         myWebView.getWebConfig().setJavaScriptPermit(true); 
  33.         myWebView.getWebConfig().setUserAgent("android"); 
  34.         myWebView.setWebAgent(new WebAgent(){ 
  35.             // 當(dāng)WebView即將打開一個(gè)鏈接時(shí)調(diào)用該方法 
  36.             @Override 
  37.             public boolean isNeedLoadUrl(WebView webView, ResourceRequest request) { 
  38.                 // request.getRequestUrl().toString() WebView即將打開的鏈接地址 
  39.                 if (request.getRequestUrl().toString().startsWith("wtloginmqq")){ 
  40.                     // 打開QQ客戶端 
  41.                     Intent qqIntent = new Intent(); 
  42.                     Operation qqOperation = new Intent.OperationBuilder() 
  43.                             .withAction("android.intent.action.VIEW"
  44.                             .withUri(Uri.parse(request.getRequestUrl().toString())) 
  45.                             .build(); 
  46.                     qqIntent.setOperation(qqOperation); 
  47.                     startAbility(qqIntent); 
  48.                     // 向自己的服務(wù)器請(qǐng)求token 
  49.                     new Thread(new Runnable() { 
  50.                         @Override 
  51.                         public void run() { 
  52.                             while (true){ 
  53.                                 String getTokenURL = "https://api.dsttl3.cn/Redis/Get?key=" + state; 
  54.                                 try { 
  55.                                     OkHttpClient client = new OkHttpClient(); 
  56.                                     Request request = new Request.Builder().url(getTokenURL).build(); 
  57.                                     String token = client.newCall(request).execute().body().string(); 
  58.                                     if (token.length() == 32){ 
  59.                                         getUITaskDispatcher().asyncDispatch(new Runnable() { 
  60.                                             @Override 
  61.                                             public void run() { 
  62.                                                 // 將token存入數(shù)據(jù)庫(kù) 
  63.                                                 Preferences preferences = new DatabaseHelper(getApplicationContext()).getPreferences("DATA_NAME"); 
  64.                                                 preferences.putString("token",token); 
  65.                                                 preferences.flush(); 
  66.                                                 // 跳轉(zhuǎn)至用戶界面 
  67.                                                 Intent myIntent = new Intent(); 
  68.                                                 Operation myOperation = new Intent.OperationBuilder() 
  69.                                                         .withBundleName("cn.dsttl3.test"
  70.                                                         .withAbilityName("cn.dsttl3.qqlogin.MyAbility"
  71.                                                         .build(); 
  72.                                                 myIntent.setOperation(myOperation); 
  73.                                                 startAbility(myIntent); 
  74.                                                 terminateAbility(); 
  75.                                             } 
  76.                                         }); 
  77.                                         break; 
  78.                                     } 
  79.                                     Time.sleep(1500); 
  80.                                 } catch (IOException e) { 
  81.                                     e.printStackTrace(); 
  82.                                 } 
  83.                             } 
  84.                         } 
  85.                     }).start(); 
  86.                     return false
  87.                 } 
  88.                 return true
  89.             } 
  90.         }); 
  91.         myWebView.load(authorize_url); 
  92.     } 

個(gè)人界面

獲取token信息

  1. Preferences preferences = new DatabaseHelper(getApplicationContext()).getPreferences("DATA_NAME"); 
  2. String token = preferences.getString("token",null); 

 更新Text數(shù)據(jù)

  1. Text text = findComponentById(ResourceTable.Id_text_helloworld); 
  2. text.setText(token); 

后續(xù)操作

獲取用戶信息請(qǐng)參考QQ開放平臺(tái)文檔 https://wiki.connect.qq.com/get_user_info

文章相關(guān)附件可以點(diǎn)擊下面的原文鏈接前往下載

https://harmonyos.51cto.com/resource/1554

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2015-11-05 16:44:37

第三方登陸android源碼

2015-01-20 17:01:30

Android源碼QQdemo

2014-07-23 08:55:42

iOSFMDB

2025-02-05 10:19:24

2024-03-04 10:36:39

2011-12-20 14:23:15

MozillaBrower ID

2018-09-30 15:18:29

2019-07-30 11:35:54

AndroidRetrofit庫(kù)

2019-09-03 18:31:19

第三方支付電商支付行業(yè)

2021-08-03 10:07:41

鴻蒙HarmonyOS應(yīng)用

2021-03-03 09:42:26

鴻蒙HarmonyOS圖片裁剪

2009-12-31 14:38:34

Silverlight

2016-10-21 14:09:10

2017-12-11 15:53:56

2021-03-10 15:03:40

鴻蒙HarmonyOS應(yīng)用

2021-04-29 14:32:24

鴻蒙HarmonyOS應(yīng)用

2009-01-14 12:45:05

MSNIM蘋果

2014-07-22 10:56:45

Android Stu第三方類庫(kù)

2017-05-16 13:24:02

LinuxCentOS第三方倉(cāng)庫(kù)

2021-09-26 10:43:08

注冊(cè)Istio集成
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)