有奖捉虫:办公协同&微信生态&物联网文档专题 HOT

接口描述

用于客户端 SDK(Web SDK 微信 H5)获取临时授权凭证
注意
建议用户使用子账号密钥 + 环境变量的方式调用 SDK,提高 SDK 使用的安全性。为子账号授权时,请遵循 最小权限指引原则,防止泄露其他资源。
本文将介绍 临时密钥(临时访问凭证)的使用方式,如果您一定要使用永久密钥,建议遵循 最小权限指引原则 对永久密钥的权限范围进行限制。
本文示例遵循 最小权限指引原则,仅提供 soe:TransmitOralProcessWithInit 接口权限。

请求方式

GET

请求地址

https://127.0.0.1:5050/getAuthorization (以实际提供接口地址为准)

请求参数

在 Web SDK 填写 getAuthorization 中的 url 和其他评测参数,参考参数说明
let recorder = new TencentSOE({
getAuthorization(callback) {
let url = 'http://127.0.0.1:5050/getAuthorization'; // 服务端获取临时密钥地址
axios.get(url).then(response => {
console.log(response.data)
callback({
Token: response.data.Credentials.Token,
TmpSecretId: response.data.Credentials.TmpSecretId,
TmpSecretKey: response.data.Credentials.TmpSecretKey,
ExpiredTime: response.data.ExpiredTime
});
}).catch(err =>{
console.log(err)
})
},
WorkMode: 0,
EvalMode: 1,
success() {
recorder.start({
RefText: "I would like to eat an orange",
});
},
error(err) {
console.log(err);
}
});


响应参数

主要响应参数说明
属性名
类型
必填
参数描述
Token
String
获取回来的临时密钥的 Token
TmpSecretId
String
获取回来的临时密钥的 TmpSecretId,用于前端计算签名
TmpSecretKey
String
获取回来的临时密钥的 TmpSecretKey,用于前端计算签名
ExpiredTime
String
获取回来的临时密钥的 ExpiredTime,过期时间
响应示例
{
"Credentials":{
"Token":"kTRt***Jb7m",
"TmpSecretId":"AKID****CjE6",
"TmpSecretKey":"Eo28***7ps="
},
"Expiration":"2023-06-14T05:06:57Z",
"ExpiredTime":1686719217,
"RequestId":"59a5e07e-4147-4d2e-a808-dca76ac5b3fd"
}

请求示例

请求流程

参考 STS 控制台 或者文档示例获取临时授权凭证,提供 Web 服务,将项目部署上线即可。
服务端 SDK
临时访问凭证示例
Python SDk
Java SDK
PHP SDK
Go SDK
Node.js SDK
.NET SDK

Python 示例

使用 flask 框架,需要使用 pip 安装以下依赖。
pip install tencentcloud-sdk-python flask flask_cors gevent
示例代码:
import json
from flask import Flask, request, render_template
from flask_cors import CORS
from gevent import pywsgi

from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException

app = Flask(__name__)

CORS(app) # 允许跨域

@app.route("/getAuthorization", methods=["GET"]) #通过该地址请求接口,只能使用get方法
def getAuthorization():
try:
# 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
cred = credential.Credential("", "")

# 实例化一个http选项,可选的,没有特殊需求可以跳过。
httpProfile = HttpProfile()
httpProfile.reqMethod = "POST" # post请求(默认为post请求)
httpProfile.reqTimeout = 30 # 请求超时时间,单位为秒(默认60秒)
httpProfile.endpoint = "sts.tencentcloudapi.com" # 指定接入地域域名(默认就近接入)

# 实例化一个client选项,可选的,没有特殊r需求可以跳过。
clientProfile = ClientProfile()
clientProfile.signMethod = "TC3-HMAC-SHA256" # 指定签名算法(默认为HmacSHA256)
clientProfile.unsignedPayload = True
clientProfile.httpProfile = httpProfile

from tencentcloud.sts.v20180813 import sts_client, models
client = sts_client.StsClient(cred, "ap-guangzhou", clientProfile)

req = models.GetFederationTokenRequest()
req.Name = "soe"
req.Policy = "{\\"version\\": \\"2.0\\",\\"statement\\": {\\"effect\\": \\"allow\\",\\"action\\": [\\"soe:TransmitOralProcessWithInit\\"],\\"resource\\": \\"*\\"}}"
# req.DurationSeconds = 10
# 请求服务,获取结果
resp = client.GetFederationToken(req)
json_resp = resp.to_json_string()
json_loads = json.loads(json_resp)
# json_dumps = json.dumps(json_loads)
return json_loads # 回调

except TencentCloudSDKException as err:
return err


if __name__ == '__main__':
server = pywsgi.WSGIServer(('0.0.0.0', 5050), app)
server.serve_forever()

Node.js 示例

使用 express 框架,需要 npm 安装如下依赖。
npm install express cors tencentcloud-sdk-nodejs
示例代码:
let express = require('express')
let server = express();
const cors = require('cors');
server.use(cors());

server.get('/getAuthorization', async (req, resp) => {
const StsClient = tencentcloud.sts.v20180813.Client;
const clientConfig = {
credential: {
secretId: "",
secretKey: "",
},
region: "ap-beijing",
};
const client = new StsClient(clientConfig);
const params = {
"Name": "soe",
"Policy": "{\\"version\\": \\"2.0\\",\\"statement\\": {\\"effect\\": \\"allow\\", \\"action\\":[\\"soe:TransmitOralProcessWithInit\\"],\\"resource\\": \\"*\\"}}"
};
client.GetFederationToken(params).then(
(data) => {
resp.json(data)
},
(err) => {
resp.json(err)

}
);
})

server.listen(5050, () => {
console.log('服务器已就绪')
})

module.exports = server;