java 微信定时刷新页面_shell脚本定时刷新微信token实例

本文通过一个shell脚本实例,展示了如何使用Java配合cron命令定时刷新微信页面的Token。脚本解析JSON文件获取微信配置,检查Token是否过期,并在过期时进行刷新。同时,脚本还更新并替换文件中的Token信息。
摘要由CSDN通过智能技术生成

cron命令说明:

crontab -l  :display current user's schdule jobs

crontab -e : edit current user's schdule jobs

用法:

d185717226e8933dd1c331525c8cafe2.png

====================文件目录========================

c369c20f1b08238e425cae5d05cb3c71.png

===================== wechat.txt =====================

86881d3edbe7354e28e2d649e7d1732d.png

=====================  main.sh  =======================

#! /bin/bash

wechat_handler(){

code=$1

#    echo ${code}

#    cat wechat.txt | jq .

wechat_name=$(cat wechat.txt | jq ".wechats[${code}].property.wechat_name")

#echo wechat_name ${wechat_name}

length0=$(expr length "${wechat_name}")

if [[ ${length0} == 0 ]];

then

echo Invalid json, please check json in wechat.txt

exit 0

fi

app_id=$(cat wechat.txt | jq ".wechats[${code}].property.app_id")

#echo app_id ${app_id}

app_secret=$(cat wechat.txt | jq ".wechats[${code}].property.app_secret")

#echo app_secret ${app_secret}

access_token=$(cat wechat.txt | jq ".wechats[${code}].property.access_token")

#echo access_token ${access_token}

expired_at=$(cat wechat.txt | jq ".wechats[${code}].property.expired_at")

#echo expired_at ${expired_at}

now_time=$(date +"%s")

#echo now_time ${now_time}

if [ ${expired_at} -gt ${now_time} ]

then

echo ===============================

echo \{\"access_token\":"${access_token}"\,\"expired_at\":"${expired_at}"\}

echo ===============================

else

echo access_token expired

echo start to refresh access_token

refresh_token ${app_id} ${app_secret}

fi

}

# Refresh token

refresh_token(){

str0=$1

str00=${str0:1}

app_id=${str00%\"*}

#echo ${app_id}

str1=$2

str11=${str1:1}

app_secret=${str11%\"*}

#echo ${app_secret}

#Network request

url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${app_id}&secret=${app_secret}"

#echo ${url}

result=$(curl ${url})

#echo result:${result}

access_token=$(echo ${result}|jq '.access_token')

refresh_time=$(date +"%s")

#echo refresh_time: ${refresh_time}

expired_at=$(expr ${refresh_time} + 7200)

#echo expired_at: ${expired_at}

echo =========== new access token ========================

echo \{\"access_token\":"${access_token}"\,\"expired_at\":"${expired_at}"\}

echo =====================================================

rewrite_file ${app_id} ${expired_at} ${access_token}

replace_file

}

#Refile access_token to file

rewrite_file(){

app_id=$1

expired_at=$2

access_token=${3}

#echo run rewrite file function.....

#echo ${app_id}

#echo ${expired_at}

#echo ${access_token}

touch new_file.txt

#Read line from file

flag=0

while read line

do

line_content=$(echo $line)

#echo ${line_content}

if [[ "${line_content}" =~ "${app_id}" ]];then

#echo match success,line_content= ${line_content}

echo ${line_content} >> new_file.txt

flag=1

#echo flag=${flag}

continue

fi

if [[ ${flag} == 3 ]];

then

#echo flag ${flag} rewrite access_token.....

#echo access_token ${line_content}

echo "\"access_token\"": ${access_token} ,>> new_file.txt

flag=$(expr ${flag} + 1)

continue

fi

if [[ ${flag} == 4 ]];

then

#echo flag ${flag} rewrite expire_time.....

#echo expired_at ${line_content}

echo "\"expired_at\"": ${expired_at} >> new_file.txt

flag=$(expr ${flag} + 1)

continue

fi

if [[ ${flag} == 1 || ${flag} == 2 ]];

then

flag=$(expr ${flag} + 1)

fi

echo ${line_content} >> new_file.txt

done < wechat.txt

}

replace_file(){

now_time=`date --date='0 days ago' "+%Y-%m-%d_%H:%M:%S"`

mv wechat.txt wechat_${now_time}.txt

mv new_file.txt wechat.txt

}

wechat_handler 0

======================  main.sh end ========================

touch file1.txt 更新file1.txt的存取和修改时间

touch -c file1.txt 如果file1.txt不存在,不创建文件

touch -r ref_file file1.txt 更新file1.txt的时间戳和ref+file相同

touch -t 0811142234.50 file1.txt 设定文件的时间错为08年11月14日22点34分40秒

=========================================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值