socialite icon indicating copy to clipboard operation
socialite copied to clipboard

企业微信获取用户详情

Open SuperPx-CN opened this issue 1 year ago • 5 comments

public function userFromCode(string $code): Contracts\UserInterface
{
        $token = $this->getApiAccessToken();
        $user = $this->getUser($token, $code);

        if ($this->detailed) {
            $user = $this->getUserById($user['UserId']);
        }

        return $this->mapUserToObject($user)->setProvider($this)->setRaw($user);
}

开启 detailed 时,会通过 getUserById 方法获取详细信息:

    protected function getUserById(string $userId): array
    {
        $responseInstance = $this->getHttpClient()->post($this->baseUrl.'/cgi-bin/user/get', [
            'query' => [
                Contracts\RFC6749_ABNF_ACCESS_TOKEN => $this->getApiAccessToken(),
                'userid' => $userId,
            ],
        ]);

        $response = $this->fromJsonBody($responseInstance);

        if (($response['errcode'] ?? 1) > 0 || empty($response['userid'])) {
            throw new Exceptions\AuthorizeFailedException((string) $responseInstance->getBody(), $response);
        }

        return $response;
    }

getUserById 方法是通过 企业微信·企业内部开发·服务端API·通讯录管理·读取成员 接口实现的。但是在最新版本的企业微信API中,该接口已经不再返回 头像、性别、手机、邮箱、企业邮箱、员工个人二维码、地址 字段(https://developer.work.weixin.qq.com/document/path/90196)

SuperPx-CN avatar Dec 25 '24 05:12 SuperPx-CN

测试了一下,按官方文档(https://developer.work.weixin.qq.com/document/path/95833),通过 user_ticket 获取访问用户敏感信息 时,确实返回了头像和手机号,但是没有返回姓名

{
   "errcode": 0,
   "errmsg": "ok",
   "userid":"lisi",
   "gender":"1",
   "avatar":"http://shp.qpic.cn/bizmp/xxxxxxxxxxx/0",
   "qr_code":"https://open.work.weixin.qq.com/wwopen/userQRCode?vcode=vcfc13b01dfs78e981c",
   "mobile": "13800000000",
   "email": "[email protected]",
   "biz_mail":"[email protected]",
   "address": "广州市海珠区新港中路"
}
···

SuperPx-CN avatar Dec 25 '24 06:12 SuperPx-CN

问题是什么?

overtrue avatar Dec 25 '24 07:12 overtrue

问题是什么?

WeWork 企业微信获取用户详细信息的方式和企业微信文档中所述不一致。 导致 userFromCode 方法没有办法获取访问用户的头像、性别、手机、邮箱、企业邮箱、员工个人二维码、地址 字段。

SuperPx-CN avatar Dec 25 '24 07:12 SuperPx-CN

所以是让我去企业微信那边改他们接口么?

overtrue avatar Dec 25 '24 08:12 overtrue

所以是让我去企业微信那边改他们接口么?

我自己已经按文档修改好了,如果有需要我可以发起 pull request,但是存在一个问题:如果按企业微信文档修改的话,那么 getRaw() 的数据和目前的差异很大。可能要考虑到有没有人在业务当中使用了这个方法。

目前返回的数据

{
    "UserId": "130****2287",
    "DeviceId": "07****1e",
    "errcode": 0,
    "errmsg": "ok",
    "user_ticket": "uo****JQ",
    "expires_in": 1800,
    "userid": "130****2287",
    "name": "张三",
    "department": [
        90
    ],
    "position": "",
    "status": 1,
    "isleader": 0,
    "extattr": {
        "attrs": []
    },
    "telephone": "",
    "enable": 1,
    "hide_mobile": 0,
    "order": [
        268435456
    ],
    "external_profile": {
        "external_attr": [],
        "external_corp_name": ""
    },
    "main_department": 90,
    "alias": "",
    "is_leader_in_dept": [
        0
    ],
    "direct_leader": []
}

变为了

{
    "UserId": "130****2287",
    "DeviceId": "07**1e",
    "errcode": 0,
    "errmsg": "ok",
    "user_ticket": "H8**RA",
    "expires_in": 1800,
    "userid": "130****2287",
    "mobile": "130****2287",
    "gender": "1",
    "email": "",
    "avatar": "https:\/\/wework.qpic.cn\/wwpic3az\/415472_iaFqQ8rtS2SaM00_1735231845\/0",
    "qr_code": "https:\/\/open.work.weixin.qq.com\/wwopen\/userQRCode?vcode=vc**49",
    "biz_mail": "p**n@g**1.wecom.work",
    "address": ""
}

SuperPx-CN avatar Dec 28 '24 07:12 SuperPx-CN