前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >腾讯云云函数实现小程序全局 access_token 刷新

腾讯云云函数实现小程序全局 access_token 刷新

作者头像
薛定喵君
发布2021-01-29 10:16:13
1.3K0
发布2021-01-29 10:16:13
举报
文章被收录于专栏:薛定喵君薛定喵君

巧用云函数实现小程序全局 access_token 刷新

# 实现思路

利用云函数请求微信服务器获取小程序全局唯一后台接口调用凭据并存入云数据库,配合云函数的触发器来实现定期刷新。

# 云数据库配置

新建集合用于存放调用凭据

# 云函数代码

新建文件 package.json 输入以下内容

代码语言:javascript
复制
{
    "name": "sync_mp_access_token",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {},
    "author": "",
    "license": "ISC",
    "dependencies": {
        "request-promise": "^4.2.6",
        "@cloudbase/node-sdk": "latest"
    }
}

然后点击『保存并安装依赖』

修改 index.js 输入以下内容

代码语言:javascript
复制
'use strict';
const cloudbase = require("@cloudbase/node-sdk");
const rp = require('request-promise')
const app = cloudbase.init({
    env: '环境ID'
});
const db = app.database();
const appId = '自己的id'
const appSecrect = '自己的密钥'
const tokenForUrl =
    'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + appId + '&secret=' + appSecrect
exports.main = async (event, context) => {
    let tokens = await db
        .collection('access_tokens')
        .get()

    if (tokens.data.length > 0) {
        let expires_time = tokens.data[0].expires_time
        console.log(parseInt(Date.now() / 1000) > expires_time + 3600)
        if (parseInt(Date.now() / 1000) > expires_time + 3600) {
            let tokenInfoNew = await rp({ url: tokenForUrl })
            tokenInfoNew = JSON.parse(tokenInfoNew)
            let expires_time = parseInt(Date.now() / 1000)
            await db
                .collection('access_tokens')
                .doc(tokens.data[0]._id)
                .update({
                    access_token: tokenInfoNew.access_token,
                    expires_time: expires_time
                })
            return tokenInfoNew
        } else {
            return tokens.data[0]
        }
    } else {
        let tokenInfoNew = await rp({ url: tokenForUrl })
        tokenInfoNew = JSON.parse(tokenInfoNew)
        let expires_time = parseInt(Date.now() / 1000)
        return await db.collection('access_tokens').add({
            access_token: tokenInfoNew.access_token,
            expires_time: expires_time
        })
    }

};

# 触发器设置

可以设置为每一小时触发一次。

代码语言:javascript
复制
{
    "triggers": [
        {
            "name": "sync_mp_access_token",
            "type": "timer",
            "config": "0 0 */1 * * * *"
        }
    ]
}

# HTTP访问

配合HTTP访问服务地址来随时取用

# 测试结果

# 参考资料

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-01-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • # 实现思路
  • # 云数据库配置
  • # 云函数代码
  • # 触发器设置
  • # HTTP访问
  • # 测试结果
  • # 参考资料
相关产品与服务
云函数
云函数(Serverless Cloud Function,SCF)是腾讯云为企业和开发者们提供的无服务器执行环境,帮助您在无需购买和管理服务器的情况下运行代码。您只需使用平台支持的语言编写核心代码并设置代码运行的条件,即可在腾讯云基础设施上弹性、安全地运行代码。云函数是实时文件处理和数据处理等场景下理想的计算平台。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档