This content originally appeared on DEV Community and was authored by victordeng
Prerequisite: QQ SDK has been imported
reference material:
https://wiki.connect.qq.com/harmonyos_sdk%e6%8e%a5%e5%8f%a3%e8%af%b4%e6%98%8e
The following is the actual code practice of the HarmonyOS project shared through QQ:
Preconditions:
Modify the oh-package.json5 file to integrate CryptoJS dependencies
"dependencies": {
"@tencent/wechat_open_sdk": "1.0.11",
"@ohos/crypto-js": "^2.0.4"
}
Function: Generate signatures using the HMAC-SHA1 algorithm
Share business development process instructions:
To ensure the credibility of business shared data, it is necessary to sign the shared data. Taking the example of sharing graphic and text ark messages, we recommend the following process for business:
Business client: When users share, the shared UGC content is transmitted to the business backend.
Business backend: Use user UGC data to assemble ark business JSON data (ShareData. shareJson), generate timestamps (ShareData. timestamp) and random natural numbers (ShareData. once) in ShareData, and perform signature calculations on these three parts of data. The above data is then called back to the business client, which initiates sharing by calling the interconnection sharing interface.
Parameter description:
Type: Sharing type, currently supports graphic and text ark type 2
ShareData: Share data, including shared business data and signed partial data
The signature steps are as follows:
Splicing the original signed text. The splicing rule for the original signature string is:
Request method+request domain name+request path+? +Request string+Share content JSON string
- Request method: Fixed as POST, note all uppercase
- Interface domain name: fixed as connect.qq.com
- Request path: fixed as/share
- Request string: concatenate signature parameters and values in lexicographic order into a string, such as: appid=222222&nonce=1234&ts=1618924373
- Request body: a JSON string composed of shared content
Assuming the sharing parameters are as follows:
- appid:222222
- nonce:1234
- ts:1618924373
- Share content: {"msc_style": 0, "title": "title", "summary": "content", "brief": "internet sharing", "URL":“ https://www.qq.com ","picture_url":" https://www.qq.com/picture.png "} The original text of the signature spelled out according to the rules is as follows:
POSTconnect.qq.com/share?appid=222222&nonce=1234&ts=1618924373&{"msg_style": 0, "title":"标题", "summary":"内容", "brief":"互联分享", "url":"https://www.qq.com", "picture_url":"https://www.qq.com/picture.png"}
- Calculate Signature This step generates a signature string. Firstly, use the HMAC-SHA1 algorithm to sign the original signature string obtained in the previous step, and then encode the generated signature string using Base64 to obtain the final signature string. Assuming the appkey is fakeAppKey, the final signature result obtained is:
Ngyk0JS5pQR8ffygeeMHFUNFQQA=
Add QQUtil.ets
import { CryptoJS } from '@ohos/crypto-js'
export class QQUtil {
/**
* 分享
* @param title 标题
* @param summary 内容
* @param brief QQ信息列表显示的内容
* @param imageUrl 图片链接
* @param url 跳转链接
*/
static share(title: string, summary: string, brief: string, imageUrl: string, url: string) {
let content = new Object({
msg_style: 0,
title: title,
summary: summary,
brief: brief,
url: url,
picture_url: imageUrl
})
let shareData: ShareData = new ShareData()
shareData.timestamp = Date.parse(new Date().toString()) / 1000
shareData.nonce = Math.floor(Math.random() * 100000000 + 100)
shareData.shareJson = JSON.stringify(content)
let signContent = 'POSTconnect.qq.com/share?appid=' + AppConfigs.qqsdkAppId.toString()
+ '&nonce=' + shareData.nonce.toString()
+ '&ts=' + shareData.timestamp.toString()
+ '&' + shareData.shareJson
const hmac = CryptoJS.HmacSHA1(signContent, AppConfigs.qqsdkAppKey);
let sign = hmac.toString(CryptoJS.enc.Base64);
shareData.shareJsonSign = sign
const qqOpenApi = QQUtil.getQQOpenApi()
qqOpenApi.share(2, shareData).then((result: ShareResult) => {
Logger.info(`qqOpenApi.share, result=${JSON.stringify(result)}`)
switch (result.resultType) {
case ShareResultType.Success: {
promptAction.showToast({ message: "分享成功" })
}
break
case ShareResultType.Cancel: {
let msg: string = result.message ?? "用户取消分享"
promptAction.showToast({ message: msg })
}
break
case ShareResultType.Error: {
let msg: string = result.message ?? "分享失败"
promptAction.showToast({ message: msg })
}
break
}
})
.catch((err: BusinessError) => {
Logger.error(`error, code=${JSON.stringify(err.code)}, message=${JSON.stringify(err.message)}`)
})
}
}
This content originally appeared on DEV Community and was authored by victordeng

victordeng | Sciencx (2025-06-29T08:48:33+00:00) HarmonyOS NEXT project practice:Share content through QQ. Retrieved from https://www.scien.cx/2025/06/29/harmonyos-next-project-practice%ef%bc%9ashare-content-through-qq/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.