OAuth

{warning} 此文档为企业微信内部应用开发的网页授权

企业微信官方文档

创建实例:

  1. $config = [
  2. 'corp_id' => 'xxxxxxxxxxxxxxxxx',
  3. 'secret' => 'xxxxxxxxxx', // 应用的 secret
  4. 'agent_id' => 100001,
  5. ];
  6. $app = Factory::work($config);

跳转授权

  1. // $callbackUrl 为授权回调地址
  2. $callbackUrl = 'https://xxx.xxx'; // 需设置可信域名
  3. // 返回一个 redirect 实例
  4. $redirect = $app->oauth->redirect($callbackUrl);
  5. // 获取企业微信跳转目标地址
  6. $targetUrl = $redirect->getTargetUrl();
  7. // 直接跳转到企业微信授权
  8. $redirect->send();

获取授权用户信息

在回调页面中,你可以使用以下方式获取授权者信息:

  1. $code = "回调URL中的code";
  2. $user = $app->oauth->detailed()->userFromCode($code);
  3. // 获取用户信息
  4. $user->getId(); // 对应企业微信英文名(userid)
  5. $user->getRaw(); // 获取企业微信接口返回的原始信息

获取用户其他信息需调用通讯录接口,参考:企业微信通讯录API

参考阅读