天天看点

微信开发:网页授权获取 access_token

关于 access_token 

access_token 是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用 access_token 

获取 access_token 的方式有两种 

         1.基础获取 access_token

         2.网页授权获取 access_token

其中 基础 access_token 和 网页 access_token 也是有区别的!!!除了登陆获取的是网页授权的 access_token 之外,其余的都用普通的 access_token 就可以了,具体详介如下图:

微信开发:网页授权获取 access_token

首先介绍一下,第一种方式获取基础 access_token

官方地址:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html

在线接口调试工具测试:微信开发:接口调试工具

接口调用请求说明

请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

参数说明

参数 是否必须 说明
grant_type 获取access_token填写client_credential
appid 第三方用户唯一凭证
secret 第三方用户唯一凭证密钥,即appsecret

返回说明

正常情况下,微信会返回下述JSON数据包给公众号:

{"access_token":"ACCESS_TOKEN","expires_in":7200}
           

参数说明

参数 说明
access_token 获取到的凭证
expires_in 凭证有效时间,单位:秒

测试 Demo

public static void main(String[] args) {
        // 获取 appid 和 appsecret  ==  公众号 ID 和 SERCRET
        String appid = "123";
        String appsecret = "312";
        // 定义请求地址,替换参数
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appid +"&secret="+appsecret;
        //发送请求 的到返回结果
        String json = HttpUtil.doGet(url,new HashMap<String,Object>(),"");
    }
           

第二种方式网页授权获取 access_token

详细代码可查看:微信开发之获取 openid

网页授权适用于用户在微信客户端中访问第三方网页,然后可以通过授权机制来获取用户基本信息

网页授权具体流程大概分为三步

1、通过授权页面获取 code 

2、通过 code 获取到 access_token

3、用 access_token 换取 openid (支持 unionID 机制)

官方文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html

首先配置回调地址,回调地址是用来获取 code 的返回值,这个是我在微信测试号配置安全域名,其实就相当于是一个白名单

微信开发:网页授权获取 access_token

接口调用请求说明 (获取 code 信息)

请求方式: GET https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect

参数说明

参数 是否必须 说明
appid 公众号的唯一标识
redirect_uri 授权后重定向的回调链接地址, 请使用 urlEncode 对链接进行处理
response_type 返回类型,请填写code
scope 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
state 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
#wechat_redirect 无论直接打开还是做页面302重定向时候,必须带此参数

接口调用请求说明 (根据 code 获取 openid 信息)

请求方式: GEThttps://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

参数说明

参数 是否必须 说明
appid 公众号的唯一标识
secret 公众号的appsecret
code 填写第一步获取的code参数
grant_type 填写为authorization_code

测试 Demo

String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
url = url.replace("APPID",appid).replace("SECRET",appsecret).replace("CODE","xxxx");
//发送请求 的到返回结果
String json = HttpUtil.doGet(url,new HashMap<String,Object>(),"");
System.out.println(json);
           

正确时返回的JSON数据包如下:

{
  "access_token":"ACCESS_TOKEN",
  "expires_in":7200,
  "refresh_token":"REFRESH_TOKEN",
  "openid":"OPENID",
  "scope":"SCOPE" 
}
           
参数 描述
access_token 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
expires_in access_token接口调用凭证超时时间,单位(秒)
refresh_token 用户刷新access_token
openid 用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
scope 用户授权的作用域,使用逗号(,)分隔