微信企业号php验证token接口,微信企业号/企业微信:获取AccessToken(示例代码)

一、什么是AccessToken

AccessToken是微信企业号和企业微信的全局唯一票据,所有接口在通信时都需要携带此信息用于验证接口的访问权限。

AccessToken需要用CorpID和Secret来换取,不同的Secret会返回不同的AccessToken。正常情况下AccessToken有效期为7200秒,有效期内重复获取返回相同结果。access_token至少保留512字节的存储空间。

2、调用参数:

(1)corpid:微信企业号和企业微信都有唯一的ID号。

(2)secret:微信企业号指的是管理组的凭证密钥,每个管理组都有一个secret;企业微信指的是应用(通讯录也是一个应用)的凭证密钥,每个应用都有一个secret。这点两者是不同的。

3、返回结果:在两者是有差异的,具体如下:

(1)微信企业号:

成功时返回

{

"access_token": "accesstoken000001",

"expires_in": 7200

}

错误时返回

{

"errcode": 43003,

"errmsg": "require https"

}

(2)企业微信

成功时返回

{

"errcode":0,

"errmsg":"",

"access_token": "accesstoken000001",

"expires_in": 7200

}

失败时返回

{

"errcode":40091,

"errmsg":"provider_secret is invalid"

}

三、C#示例,获取AccessToken

为了统一微信企业号和企业微信的调用方法,我们首先要定义一个类,如下:

1010

这个类已经涵盖了微信企业号/企业微信获取AccessToken成功和失败的全部情况,是否成功我们只需要读取access_token的内容,只要不为空则可以认定成功。当然也可以判断errcode的值。但微信企业号成功时没有errcode 这个字段,这时需要特殊处理一下。

下面这个函数调用这个API,将corpid和corpsecret作为参数,返回结果为AccessTokenJsonResult类

1010

下面的代码调用这个函数,自己根据返回结果读取AccessToken的值,如果调用失败就需要自行处理异常。

1010

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是 Java 微信企业上传文件的示例代码: ```java import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class WechatMediaUpload { /** * 上传文件到微信服务器 * * @param accessToken 微信企业access_token * @param fileType 文件类型,可选值为image、voice、video、file * @param file 要上传的文件 * @throws Exception */ public static String uploadFile(String accessToken, String fileType, File file) throws Exception { String url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=" + fileType + "&access_token=" + accessToken; URL urlObj = new URL(url); HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); String boundary = "-----------------------------" + System.currentTimeMillis(); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); OutputStream out = new DataOutputStream(conn.getOutputStream()); // 文件部分 StringBuilder sb = new StringBuilder(); sb.append("--").append(boundary).append("\r\n"); sb.append("Content-Disposition: form-data; name=\"media\"; filename=\"" + file.getName() + "\"\r\n"); sb.append("Content-Type: application/octet-stream\r\n\r\n"); out.write(sb.toString().getBytes()); DataInputStream in = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } in.close(); out.write(("\r\n--" + boundary + "--\r\n").getBytes()); out.flush(); out.close(); // 读取返回数据 StringBuffer buffer = new StringBuffer(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line = null; while ((line = reader.readLine()) != null) { buffer.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { reader.close(); } } conn.disconnect(); return buffer.toString(); } public static void main(String[] args) { try { String accessToken = "your_access_token"; String fileType = "image"; // 上传文件类型,可选值为image、voice、video、file File file = new File("your_file_path"); String result = uploadFile(accessToken, fileType, file); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } } ``` 注意替换代码中的 access_token 和文件路径,以及设置正确的文件类型。文件类型为企业 API 中定义的四种类型之一:image、voice、video 和 file。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值