成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

HarmonyOS 第三方登錄之QQ登錄

開發 OpenHarmony
因為鴻蒙系統剛出不久,官方的第三方登錄SDK還沒出來,下面就介紹下在鴻蒙應用中實現QQ登錄的方法(支持喚起QQ安卓客戶端進行授權)

[[438480]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

前言

因為鴻蒙系統剛出不久,官方的第三方登錄SDK還沒出來,下面就介紹下在鴻蒙應用中實現QQ登錄的方法(支持喚起QQ安卓客戶端進行授權)

前期準備

登錄QQ開放平臺 > 應用管理 > 創建應用 ,創建一個網站應用。

注意:要選擇網站應用,移動應用和小程序不適用該方案。

編寫代碼

判斷是否已登錄

獲取登錄狀態

在入口AbilitySliceMainAbilitySlice中進行判斷。

從數據庫獲取token的值判斷是否已經登錄賬號 (已登錄返回token,未登錄返回null)

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

進行相應跳轉

已登錄跳轉至個人界面MyAbility,未登錄跳轉至登錄界面LoginAbility.

  1. if(token != null){ 
  2.             // 已登錄,跳轉至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.             // 未登錄,跳轉至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(); 

登錄界面的操作

申請網絡訪問權限

在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

需要用到的幾個常量

  1. String state = UUID.randomUUID().toString();// 唯一標識,成功授權后回調時會原樣帶回。 
  2. String client_id = "101***151";//QQ開放平臺 應用 APP ID 
  3. String redirect_uri = "https%3A%2F%2Fapi.dsttl3.cn%2FRedis%2FQQLogin"; //應用 網站回調域 需進行url編碼,授權成功后會跳轉至該鏈接 
  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設置為安卓,授權頁才顯示QQ客戶端一鍵登錄按鈕 

自定義WebAgent

當WebView即將打開一個鏈接時調用isNeedLoadUrl方法,當在網頁上點擊“一鍵登錄”時,打開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); 

因為目前還找不到網頁端喚起鴻蒙應用的方法,所以QQ客戶端回調的code放在自己服務器處理。

授權成功后,會打開之前在QQ開放平臺設置的回調域redirect_uri

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

code:QQ授權返回的code,用于申請token

state:在webview請求QQ授權頁面時傳入的唯一標識,用于判斷用戶身份,方便后續從服務器請求token

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

客戶端請求到token后,將token存儲到數據庫

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

token存儲完成后跳轉至MyAbility

自定義WebAgent完整代碼

  1. myWebView.setWebAgent(new WebAgent(){ 
  2.             // 當WebView即將打開一個鏈接時調用該方法 
  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.                     // 向自己的服務器請求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存入數據庫 
  30.                                                 Preferences preferences = new DatabaseHelper(getApplicationContext()).getPreferences("DATA_NAME"); 
  31.                                                 preferences.putString("token",token); 
  32.                                                 preferences.flush(); 
  33.                                                 // 跳轉至用戶界面 
  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.         }); 

加載網頁

  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開放平臺登錄授權文檔 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();// 唯一標識,成功授權后回調時會原樣帶回。 
  21.     String client_id = "101547151";//QQ開放平臺 應用 APP ID 
  22.     String redirect_uri = "https%3A%2F%2Fapi.dsttl3.cn%2FRedis%2FQQLogin"; //應用 網站回調域 需進行url編碼,授權成功后會跳轉至該鏈接 
  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.             // 當WebView即將打開一個鏈接時調用該方法 
  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.                     // 向自己的服務器請求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存入數據庫 
  63.                                                 Preferences preferences = new DatabaseHelper(getApplicationContext()).getPreferences("DATA_NAME"); 
  64.                                                 preferences.putString("token",token); 
  65.                                                 preferences.flush(); 
  66.                                                 // 跳轉至用戶界面 
  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.     } 

個人界面

獲取token信息

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

 更新Text數據

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

后續操作

獲取用戶信息請參考QQ開放平臺文檔 https://wiki.connect.qq.com/get_user_info

文章相關附件可以點擊下面的原文鏈接前往下載

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

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

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

2019-07-30 11:35:54

AndroidRetrofit

2021-03-03 09:42:26

鴻蒙HarmonyOS圖片裁剪

2018-09-30 15:18:29

2021-08-26 16:07:46

鴻蒙HarmonyOS應用

2021-03-01 14:00:11

鴻蒙HarmonyOS應用

2016-10-21 14:09:10

2009-12-31 14:38:34

Silverlight

2017-12-11 15:53:56

2019-09-03 18:31:19

第三方支付電商支付行業

2021-08-03 10:07:41

鴻蒙HarmonyOS應用

2021-03-12 16:35:33

鴻蒙HarmonyOS應用

2021-03-24 09:30:49

鴻蒙HarmonyOS應用

2017-03-14 13:03:18

Android架構第三方庫

2014-07-25 09:33:22

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: av大片在线观看 | 在线免费观看黄色av | 91在线一区 | www.日韩| 国产一区二区精品在线观看 | 精品久久久久久久久久久 | 中文字幕 亚洲一区 | 欧美1区2区 | 二区中文字幕 | 国产精品一码二码三码在线 | 中文字幕视频在线观看 | 中文在线www | 国产精品99久久久久久宅男 | 精品国产精品国产偷麻豆 | 国产精品精品 | 在线成人av | 久久精品亚洲 | 91精品国产乱码久久久 | 日韩在线精品视频 | 欧美日韩精品 | www.黄色网| 精品一区二区三区四区外站 | 亚洲精品自拍视频 | 欧美精| 99在线免费观看视频 | 欧美三级在线 | 免费在线观看一区二区三区 | 拍真实国产伦偷精品 | 成人高清网站 | 成人二区 | 久久99精品久久 | 一区二区三区中文字幕 | 国产精品视频一二三区 | 国产精品成人一区二区三区吃奶 | 成人免费淫片aa视频免费 | 久久综合一区二区 | 91精品在线看 | 欧美一级电影免费观看 | 日韩欧美不卡 | 99精品在线 | 精品一区在线 |