# 企业微信（企业内部）扫码登录

## 准备工作 <a href="#prepare" id="prepare"></a>

你一共需要准备以下内容：

1. [注册 Authing 开发者账号](/authing/quickstart/create-authing-account.md)
   * 在创建账号的引导过程中，你会得到一个二级域名，在后面会用到。
2. [注册一个企业微信账号](https://work.weixin.qq.com/)
3. 在 Authing 控制台填入企业微信相关信息

### 在 Authing  控制台找到「企业微信（内部应用）扫码登录」

![](/files/-M3jtMGRHyzMhyvzLRUh)

之后你需要将相关配置填入下面的表单：

![](/files/-M3jtqfg2YQJAvznJfeL)

### 获取企业ID（CorpID）

在 [我的企业 - 企业信息 ](https://work.weixin.qq.com/wework_admin/frame#profile)页面可以获取到。

![](/files/-M3juI-YnFCC_N6osuiG)

### 获取 AgentID 和 Secret

在 [应用管理 - 应用管理 ](https://work.weixin.qq.com/wework_admin/frame#apps)页面的应用详情可以获取到。

![](/files/-M3juXpocqwmrNIfv4Zh)

### 启用企业微信授权登录

在应用详情页，点击设置企业微信授权登录：

![](/files/-M3k-lDbh1fiyZdcrP2p)

在新打开的页面，设置授权回调域为 core.authing.cn 。

![](/files/-M3k08EQvfnxJ9NSW81v)

### 添加网页授权信任域名

在应用详情页，设置网页授权及JS-SDK域名，填写你的 OIDC 应用二级域名，如 yourapp.authing.cn 。

![](/files/-M3k-SRC4Raye3k4gIEV)

### 配置回调链接 Redirect URL

如果你使用 [Guard](/authing/sdk/guard.md)，可以留空填 「#」。

如果你需要手动接入企业微信扫码登录，需要填写你的**业务回调链接**，用户授权之后，Authing 将会把用户信息回调给你。

## 开始接入

### 拼接网页授权链接

<mark style="color:blue;">`GET`</mark> `https://core.authing.cn/oauth/wechatwork/:userPoolId/sso-qrconnect-url`

获取企业微信扫码授权登录链接，**此链接需要在浏览器内访问！**

#### Path Parameters

| Name       | Type   | Description |
| ---------- | ------ | ----------- |
| userPoolId | string | 用户池 ID      |

{% tabs %}
{% tab title="200 " %}

```
```

{% endtab %}
{% endtabs %}

比如你的用户池 ID 为 5e4cdd055df3df65dc58b97d，则你需要引导你的用户跳转到[https://core.authing.cn/oauth/wechatwork/5e4cdd055df3df65dc58b97d/sso-qrconnect-url](http://foreign.holegots.com:8005/oauth/wechatwork/5e4cdd055df3df65dc58b97d/authorization-url)

{% hint style="info" %}
你可以在 Web 页面上放置一个可点击的按钮或 Logo 链接到上面的链接以便用户可以点击登录。
{% endhint %}

在浏览器中访问上述链接，你应该成功可以跳转企业微信扫码登录页面：

![](/files/-M3jvfi-pIyN_adbyAO6)

### 处理 Authing 回调数据

{% hint style="success" %}
Authing 为你隐藏了和企业微信服务器的交互过程，你可以直接获取到用户信息。
{% endhint %}

上一步用户同意授权之后，会先跳转到 Authing 服务器，之后 Authing 会携带用户信息跳转到开发者在 Authing 控制台中配置的业务回调链接，并附带以下 Get 请求参数：

| 参数        | 说明                                                                                                                                                     |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| code      | 错误或成功代码，200 为成功，非 200 为失败                                                                                                                              |
| message   | 成功或错误信息                                                                                                                                                |
| data      | userInfo 用户信息，若 code 为非 200 不返回此参数                                                                                                                     |
| bindOAuth | 是否是登录操作， 无此参数或此参数为`0`则为登录, 此参数为`1`则为绑定账号操作, 此时可使用`data`参数中的数据[绑定OAuth](/authing/sdk/sdk-for-node/bind-social-login.md#bang-ding-she-hui-hua-zhang-hao) |

{% hint style="info" %}
Authing 回调给你的用户信息是经过加工过的，会自动注册到你的用户池，会和企业微信返回的原始数据不一样。
{% endhint %}

{% hint style="warning" %}
部分浏览器和 Web Server 在 URL 过长的情况下有可能出现 404，如 ASP.NET，这个时候需要修改一下配置，具体方式请见[这个 StackOverflow 回答](https://stackoverflow.com/questions/28681366/in-asp-net-mvc-would-a-querystring-too-long-result-in-404-file-not-found-error/28681600)。
{% endhint %}

data （用户信息）示例：

```javascript
{
  email: '',
  phone: null,
  emailVerified: false,
  username: 'liaozhangjiang',
  nickname: 'LiaoZhangJiang',
  company: '',
  photo: 'http://wework.qpic.cn/bizmail/mKZmqeWl7K57rTUjpictGYYpRgOgv9hMibGFjXqC05RKOkEFoibOrbzCw/0',
  loginsCount: 9,
  registerMethod: 'oauth:wechatwork-qrconnect',
  blocked: false,
  isDeleted: false,
  oauth: '{"corpid":"ww736adab7f131153d","userid":"LiaoZhangJiang","deviceId":"883CBA07-87A3-46B2-94FF-6907D447D398","name":"LiaoZhangJiang","gender":"1","avatar":"http://wework.qpic.cn/bizmail/mKZmqeWl7K57rTUjpictGYYpRgOgv9hMibGFjXqC05RKOkEFoibOrbzCw/0","qr_code":"https://open.work.weixin.qq.com/wwopen/userQRCode?vcode=vcc027dee84b322352"}',
  metadata: '"{}"',
  _id: "5e7efb7ff0dfe6f2ffb64763",
  unionid: 'ww:ww736adab7f131153d:LiaoZhangJiang',
  openid: 'ww:ww736adab7f131153d:LiaoZhangJiang',
  lastIP: '::ffff:127.0.0.1',
  lastLogin: "2020-03-28T10:17:14.764Z",
  signedUp: "2020-03-28T07:23:43.764Z",
  token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImVtYWlsIjoiIiwidW5pb25pZCI6Ind3Ond3NzM2YWRhYjdmMTMxMTUzZDpMaWFvWmhhbmdKaWFuZyIsImlkIjoiNWU3ZWZiN2ZmMGRmZTZmMmZmYjY0NzYzIiwiY2xpZW50SWQiOiI1ZTRjZGQwNTVkZjNkZjY1ZGM1OGI5N2QifSwiaWF0IjoxNTg1MzkwNjM0LCJleHAiOjE1ODY2ODY2MzR9.Jt7ovlBgl_Lfb63lK5OWaClFDZypxUFP8J32TerBUDY',
  tokenExpiredAt: 2020-04-12T10:17:14.000Z
}

```

以下是使用 JavaScript 从 URL 参数中获取用户数据的代码：

```javascript
// 获取 URL 参数
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

// 将 Code 转为 Int 类型，方便判断
const code = parseInt(getQueryString('code'));

if(code !== 200) {
  // 出错了
  const errorMsg = getQueryString('message');
  // 展示 errorMsg 给用户或执行其他业务 ...
  
}else {
  const userInfo = getQueryString('data');
  
  // 将 token 存储到 localStorage 
  // 建议在之后的请求中附带 Token，并由后端验证 Token 合法性
  localStorage.setItem('token', userInfo.token);
}
```

## 完成接入

恭喜你，此时已经接入了企业微信扫码登录。获取到用户信息之后，你可以得到登录凭证 token，你可以在后续的 API 请求中携带上此 token, 然后在后端接口中根据此 token 区分不同用户，详情请见[验证 token](/authing/advanced/verify-jwt-token.md)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://authing.gitbook.io/authing/social-login/web/wechatwork-corp-qrconnect.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
