Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

获取 access_token 时 AppSecret 错误,或者 access_token 无效。请开发者认真比对 AppSecret 的正确性,或查看是否正在为恰当的公众号调用接口 #23

Closed
DeyiXu opened this issue May 5, 2020 · 1 comment

Comments

@DeyiXu
Copy link
Member

DeyiXu commented May 5, 2020

错误日志:获取 access_token 时 AppSecret 错误,或者 access_token 无效。请开发者认真比对 AppSecret 的正确性,或查看是否正在为恰当的公众号调用接口

微信开发文档

同一个账号获取 access_token 值,必须要共享这个值。

access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。

1、建议公众号开发者使用中控服务器统一获取和刷新access_token,其他业务逻辑服务器所使用的access_token均来自于该中控服务器,不应该各自去刷新,否则容易造成冲突,导致access_token覆盖而影响业务;

2、目前access_token的有效期通过返回的expire_in来传达,目前是7200秒之内的值。中控服务器需要根据这个有效时间提前去刷新新access_token。在刷新过程中,中控服务器可对外继续输出的老access_token,此时公众平台后台会保证在5分钟内,新老access_token都可用,这保证了第三方业务的平滑过渡;

3、access_token的有效时间可能会在未来有调整,所以中控服务器不仅需要内部定时主动刷新,还需要提供被动刷新access_token的接口,这样便于业务服务器在API调用获知access_token已超时的情况下,可以触发access_token的刷新流程。

4、对于可能存在风险的调用,在开发者进行获取 access_token调用时进入风险调用确认流程,需要用户管理员确认后才可以成功获取。具体流程为:

开发者通过某IP发起调用->平台返回错误码[89503]并同时下发模板消息给公众号管理员->公众号管理员确认该IP可以调用->开发者使用该IP再次发起调用->调用成功。

如公众号管理员第一次拒绝该IP调用,用户在1个小时内将无法使用该IP再次发起调用,如公众号管理员多次拒绝该IP调用,该IP将可能长期无法发起调用。平台建议开发者在发起调用前主动与管理员沟通确认调用需求,或请求管理员开启IP白名单功能并将该IP加入IP白名单列表。

公众号和小程序均可以使用AppID和AppSecret调用本接口来获取access_token。AppID和AppSecret可在“微信公众平台-开发-基本配置”页中获得(需要已经成为开发者,且帐号没有异常状态)。**调用接口时,请登录“微信公众平台-开发-基本配置”提前将服务器IP地址添加到IP白名单中,点击查看设置方法,否则将无法调用成功。**小程序无需配置IP白名单。

@DeyiXu
Copy link
Member Author

DeyiXu commented May 5, 2020

解决方法:

使用全局唯一AccessToken

客户端

package main

import (
	"log"

	"github.com/go-redis/redis/v7"
	"github.com/nilorg/go-wechat"
	"github.com/nilorg/sdk/signal"
)

var (
	// Redis 缓存
	Redis *redis.Client
	// client 微信客户端
	client wechat.Clienter
)

func init() {
	initRedis()
}

func initRedis() {
	// 初始化Redis
	client := redis.NewClient(&redis.Options{
		Addr:     "127.0.0.1:3679",
		Password: "xxxxx",
		DB:       1,
	})
	_, err := client.Ping().Result()
	if err != nil {
		log.Fatalf("Init redis connection failed: %s \n", err)
		return
	}
	Redis = client
}

func main() {
	client = wechat.NewClientFromRedis()
	// 使用自定义Redis
	// client = wechat.NewClientFromRedis(wechat.ClientFromRedisOptionRedisClient(Redis))
	// 使用自定义RedisKey
	// client = wechat.NewClientFromRedis(
	// 	wechat.ClientFromRedisOptionRedisClient(Redis),
	// 	wechat.ClientFromRedisOptionAccessTokenKey("test_access_token"),
	// 	wechat.ClientFromRedisOptionJsAPITicketKey("test_js_api_ticket"),
	// )
	// 获取内容
	log.Printf("AccessToken:%s\n", client.GetAccessToken())
	log.Printf("JsAPITicket:%s\n", client.GetJsAPITicket())

	signal.AwaitExit()
}

服务器端

  1. 使用wechat-auto-refresh-server作为自动刷新服务器
  2. 自己实现刷新AccessToken服务器

@DeyiXu DeyiXu closed this as completed May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant