您的位置 首页 golang

企业微信开发简明教程(一):用户授权

企业微信开发简明教程(一):用户授权

一、前言

本教程根据应用场景进行划分,重点阐述 企业微信 在不同应用场景下的开发思路,同时讲解使用的接口及细节。

进行企业微信应用开发之前需要准备:

1. 可供外网访问服务器用于发布应用,调用官方 API 及处理官方服务器的回调事件。开发阶段,可以将本地机器作为服务器,只需要安装类似花生壳的软件实现内网穿透,最后在企业微信将域名加至可信域名列表中。

2. 处理GET、POST请求的能力及调试工具。官方API在调用时和回调时采用GET和POST两种方式,数据采用JSON格式传输。在开发阶段,可以使用官方提供的“接口调试工具”(地址:)进行测试,也可在本地安装Postman软件进行远程调试。

3. 阅读官方API文档。企业微信开发分为三种场景:企业内部开发、第三方应用开发和智慧硬件开发。其中企业内部开发是最为常用且重要的一种方式,非常有必要阅读。另外官方提供了“加解密库”的下载,建议下载和使用,避免出现加解密失败Bug。每个企业微信应用都有一个密钥用于对传输数据加密,以保证数据传输安全性。官方已提供了c++、python、Java、php、c#和golang语言实现的加解密库工具库。

4.理解access_token。这是一个调用官方API的凭证,调用API时要提供此凭证,官方服务器依据此来验证请求是否合法。凭证从官方服务器获取,且默认为2小时有效期。获取access_token需要使用企业标识corpid和密钥serect(secret介绍见下面内容)。

二、企业 微信 中“用户”相关概念

企业微信的前身是 RTX (面向企业内使用的IM工具),RTX解决了企业内部员工之间的即时通讯问题,可以聊天、传文件等,支持划分部门组织架构。企业微信在RTX基础上将定位进行了外延,将企业微信与微信用户打通,同时增强了面向企业生产、经营、日常办公等业务功能,使之不仅能够成为“企业的生产力工具”,而且能够成为连接客户(微信用户)的营销工具。

企业微信现在同时具备了to B的能力(驱动企业经营生产,例如连接供应商等)和to C的能力(可触达11亿微信用户,例如企业员工添加客户微信为好友,将客户加入到企业的私域流量池)。随着腾讯对企业微信开放数据的进一步扩大,未来企业微信将被赋予更多经营能力,企业微信将成为企业信息化系统中重要的连接器。

企业微信中涉及“用户”的概念包括:成员、部门、应用、外部联系人。

2.1 企业-部门-成员

成员指企业员工,员工被分配在不同部门下,一个企业包括多个部门。

企业与部门之间是一对多的关系,部门与员工之间是多对多的关系(一个员工可归属于多个部门,一个部门下拥有多名员工)。

企业的唯一标识为corpid,部门的唯一标识为departmentid,员工的标识(账号)为userid。

为了实现企业向员工付款或发红包(企业支付场景),每个成员的userid可以转换为 openid (两者之间支持互转),在调用付款和发红包接口时使用openid。但需要注意的是,成员的openid仅为企业微信下使用,而不是公众号中的openid。

2.2 应用

应用是企业微信作为平台提供服务的载体,每个企业(企业微信)下都会包含多个应用。在应用之上,可以实现消息收发(以会话形式发送消息、接收消息)、设置应用主页(H5)、授权登录(为现有的web网页、APP实现企业微信扫码登录)。

应用重要的参数包括两个,分别是agentid和secret。agentid为应用的唯一标识,secret用于获取access_token(需结合corpid)。

每个应用可设置可见成员的范围,例如创建某个应用只面向技术部成员可见,创建某个应用只面向市场部可见。

2.3 外部联系人

在企业微信中,除了本企业的员工成员外,其余被员工加为好友的都视作为外部联系人,外部联系人包括微信联系人和企业微信联系人。微信联系人是指添加了对方的微信账号、企业微信联系人是指添加了对方的企业微信账号。

企业为了构建自己的私域流量池,为提高营销和客户关系维护效果,会让员工将客户加为自己好友,那么这部分从外部加过来的好友,即为外部联系人。这些外部联系人在企业微信中即使原添加的员工离职了,也支持将其再分配,便于工作交接和客户维护。

每个外部联系人都具有唯一标识——external_userid,这个标识在一个企业中是唯一的,应用可以获取到外部联系人userid,外部联系人的userid可以转换为openid,但需要注意此openid与 微信公众号 平台的openid不同。

三、用户授权

企业微信的用户授权流程与微信公众平台流程基本一致。采用OAuth2机制,实现网页授权和扫码授权。

OAuth2授权机制

OAuth2的目的是不需用户输入用户名、密码,只需用户扫码确认或手动确认授权,应用方即可以获取用户信息。企业微信与微信公众号一样,采用了OAuth2中经典的授权码authorization code模式,这也是各大平台都在采用的模式。

企业微信开发简明教程(一):用户授权

上图为授权码的工作流程示意。具体细节为:

1. 用户打开应用(通常为浏览器),应用首先重定向到授权服务器。

2. 授权服务器显示授权页面,用户确认授权。

3. 用户授权成功后,授权服务器重定向(返回)到redirect_uri,同时返回参数code

4. 应用收到code后,向授权服务器发出请求,申请令牌。

5. 应用收到访问令牌和更新令牌。

企业微信网页授权流程

根据以上OAuth2的授权码流程,企业微信中网页授权具体流程为:

1.构造网页授权URL

授权URL格式为:

#wechat_redirect

具体参数含义为

企业微信开发简明教程(一):用户授权

假设redirect_uri为 , state为1000,则重定向地址为:

2. 获取access_token

在获取access_token前,要先获取企业corpid和serect(密钥secret分为三类:自建应用serect、通讯录secret、外部联系人secret。每一个密钥不仅是调用API的安全钥匙,还是对不同权限的限定)

获取access_token请求地址为:

请求方式:GET

返回结果格式为:

{ “errcode”: 0, “errmsg”: “ok”, “access_token”: “yiuH7Mvyf…..”, “expires_in”: 7200}

调用效果如下图

企业微信开发简明教程(一):用户授权

3. 获取userid

在获取到授权码code和访问令牌access_token后,最后一步即可调用API获取当前访问成员用户的userid。

请求地址:

参数中code使用时需注意:code仅能使用1次,且有效期为5分钟,5分钟内未使用则自动过期。

请求方式:GET

返回结果存在两种情况:

(1)如果当前访问者为“企业成员”,则返回userid

{ “errcode”: 0, “errmsg”: “ok”, “UserId”:”USERID”, “DeviceId”:”DEVICEID”}

返回结果中UserId即为成员在当前企业微信中的唯一标识。DeviceId为设备编号,安装企业微信时随机生成,可用于标识一台手机。

(2)如果当前访问者不是企业成员,则返回OpenId

{ “errcode”: 0, “errmsg”: “ok”, “OpenId”:”OPENID”, “DeviceId”:”DEVICEID”}

返回结果中OpenId为当前访问者在企业微信中的唯一标识,仅限企业微信使用,与公众号中的OpenId不同。企业微信中的OpenId目前仅限支付场景使用。

至此,用户授权流程完成,应用已经获得了成员用户userid(非成员用户openid)。后续如若获取成员用户的详细信息,可调用【读取成员】接口。

三、常见问题

1. 授权码回调返回两次,code返回两次问题

答:授权码code只能使用1次。若出现2次,则表明腾讯服务器认为第一次没能收到正确的响应,所以才会有第2次回调。出现这个情况,需检查:

(1)域名是否正确可到达,是否存在防火墙拦截。

(2)回调地址是否进行了URLCode

(3) 是否正确返回了200状态码

(4)本地进行code判断,若重复则丢弃。

2.userid与openid的关系

答:userid是成员用户在企业微信中的标识,可理解为账号。对于外部联系人,具有external_userid对其在企业微信中进行标识。openid是由userid转化过来的,也是其在企业微信中的标识。但需要注意此openid与微信公众平台openid不同。企业微信中成员用户的userid转为openid后,目前仅用于企业付款、发红包使用。外部联系人external_userid也可以转换external_user_openid,但也仅用于支付场景。

以上为第一部分,请关注我继续学习第二部分内容:企业微信开发简明教——用户管理。

文章来源:智云一二三科技

文章标题:企业微信开发简明教程(一):用户授权

文章地址:https://www.zhihuclub.com/99580.shtml

关于作者: 智云科技

热门文章

评论已关闭

37条评论

  1. All orders are shipped 2nd Day Air to ensure medication is received as soon as possible

  2. Among other things, Google began requiring online pharmacy advertisers to be certified by the National Association of Boards of Pharmacy s Verified Internet Pharmacy Practices Sites program, which conducts site visits; has a stringent standard against the issuance of prescriptions based on online consultations; and, most significantly, does not certify Canadian online pharmacies Other limited studies have shown that supplementation is associated with improved muscle strength, increased muscle size, decreased body fat percentage, and improved semen quality

  3. Headache Flushing of the face Stuffy nose Upset stomach Pain in the muscle Pain in the back Pain in the arms and legs Stuffy nose This can be due to the body natural enzyme known as PDE5, which destroys the nitric oxide NO , obstructing the production of the chemical substance known as cyclic guanosine monophosphate cGMP

  4. Crofelemer has the potential to inhibit CYP3A4 at concentrations expected in the gut; unlikely to inhibit systemically because minimally absorbed

  5. On average, women with a prepregnancy history of infertility had a higher prepregnancy BMI and greater BMI gain since age 18 years, consumed less alcohol, were less engaged in prepregnancy total or vigorous physical activity, were more likely to be married and to report ever oral contraceptive use, and were more likely to report a family history of diabetes.

  6. The girl in black in the distance, after feeling does pomegranate juice interfere with blood pressure meds the pressure, could not help but turn her pretty face Hypertension Drugs List does spicy food lower cholesterol away, and then subconsciously backed away.

  7. During his 2008 campaign, she started making campaign stops on her own in key states like New Hampshire

  8. The most recent of these was a double blind, randomized, placebo controlled, 6 week study N 40 that evaluated the efficacy and safety of tamoxifen with adjunctive lithium in the treatment of acute bipolar mania

  9. Di Paola et al 3 were determined to be eligible; 225 patients enrolled in group B and 223 99

  10. The 4 hydroxytamoxifen conjugates exhibit nanomolar range activity against the MCF 7 breast cancer cell line, paving the way to expand the scope and connectivity of prodigiosene tamoxifen conjugates

  11. Normand auXXPBNdMVM 5 20 2022 Like many other anabolic steroids oxymetholone can cause side effects, which are more or less pronounced depending on how much you take and what dosages you use

  12. nitroglycerin como calcular dosis de ibuprofeno en nios Oil prices edged lower on concern that a shutdown of theU nelfinavir clobetasol acis salbe beipackzettel Stocks closed Thursday with losses, with the Standard Poor s 500 index dropping the most in a month, as data showed weaker than forecast growth in service industries and concern grew that the political impasse could lead to a recession

  13. The authors found that patients who had a response to induction chemotherapy benefited from the addition of mastectomy to chemotherapy and radiation

  14. She simply modified what she ate in line with evidence- based nutritional principles for women with PCOS The median disease specific survival has not been reached in the immediate arm yet 2

  15. This report presents a rare case of lichen planus pigmentosus caused due to tamoxifen intake

  16. Male Anti- Aging Medicine 10 May 29, 2021 Coming off TRT The recently formed World Anti Doping Agency may have the capabilities to initiate these tests, but it will require the cooperation of the International Sports Federations as well as governments who must agree to be zealous about drug testing

  17. The patient received the lower dose for the study duration, highlighting the importance of monitoring potential acute weakness due to dantrolene treatment Dwain CVfoWcrWuEkFEfHS 6 18 2022

  18. Serious Use Alternative 1 voxelotor will increase the level or effect of crizotinib by affecting hepatic intestinal enzyme CYP3A4 metabolism

  19. amifostine, avanafil It works by damaging the genetic material DNA and RNA of the cancer cells which stops their growth and multiplication

  20. After refining the items, cognitive pretesting was performed with seven breast cancer providers

  21. 71 Here, aspirin reduced the risk of adenocarcinoma with metastasis at initial diagnosis and on subsequent follow up in patients without metastasis initially, particularly in patients with colorectal cancer and those who remained on the trial treatment up to or after diagnosis

  22. On the All About Hot Flashes page, you can read more about other medical and non medical options for managing these symptoms Singletary, 28, who had been living with Abid, admitting shooting Whitworth but told investigators Abid paid him to do it, according to court documents

  23. F Compared with WT, T60A M NCC exhibits more rapid degradation but T60D NCC has greater membrane stability According to the current terminology, the treatment period is defined by the term persistence 20

  24. abbreviated as IUI and commonly known as artificial insemination a process of cleaning, concentrating, and loading sperm into a catheter that can then be used to deposit in the uterus so they have a higher chance of reaching the fallopian tubes, the site of fertilization

网站地图