Skip to main content

选择认证

支持用户选择第三方认证方式,或通过现有认证,或添加新的。

认证是用户凭证的集合,用于某个应用,由 Zapier 安全存储。当需要时,用户必须选择他们为该应用拥有的认证(他们可能有多个),以在 Zap 执行时使用。

认证模式

我们可以向 /authentications 端点 发送请求,获取某个应用的可用认证列表:

// GET /authentications?app=4b3920d6-1d5a-4071-b837-9383dc511b80
{
"data": [
{
"type": "authentication",
"id": "49509",
"app": "4b3920d6-1d5a-4071-b837-9383dc511b80",
"title": "SuperExampleCRM (wade@zapier.com)",
"is_expired": false
},
{
"type": "authentication",
"id": "96983",
"app": "4b3920d6-1d5a-4071-b837-9383dc511b80",
"title": "SuperExampleCRM (bryan@zapier.com)",
"is_expired": false
}
]
}

用户随后可以选择这些认证中的一个,用于 Zap。

当不存在认证时

用户可能尚未为所选应用设置任何认证,例如在新建 Zapier 账户时。在这种情况下,/authentications 端点会在 data 键下返回空列表。在此场景中,应引导用户访问 /apps 端点提供的 links.connect_new_authentication 键中的 URL,以添加新的认证。

即使用户已有可用认证,如果希望提供使用新认证选项,这也是最佳方法(例如,用户想要使用与 Zapier 已链接不同的 SuperExampleCRM 账户)。

如果 links.connect_new_authenticationnull,则表示该应用不需要认证,应传递 null 而非有效的 ID。请参阅下方了解更多。

引导用户创建新的认证

使用 links.connect_new_authentication 链接的最佳方式如下:

1. 在弹出窗口中打开 links.connect_new_authentication 链接

弹出窗口中,用户将被提示对应用进行认证,并授权 Zapier 访问该应用。

2. 创建事件监听器,以监听该弹出窗口发送的 zapier.popup.close 消息

当弹出窗口中的认证流程完成时,会发送该类型消息。

3. 从消息中提取新的 authentication_id

随后,使用该 authentication_id 继续 Zap 创建过程。

// 1. 打开一个弹出窗口,指向 `links.connect_new_authentication` URL。
const authPopup = window.open(app.links.connect_new_authentication, '_blank', 'width=1280,height=1024');
if (!authPopup) {
alert("请允许弹出窗口以继续操作。");
return;
}

// 2. 添加事件监听器,在用户完成认证流程时收到通知。
window.addEventListener('message', function(event) {
if (event.origin === 'https://zapier.com') {
const action = event.data;
if (action?.type === "zapier.popup.close") {
if (authPopup) { // 如果弹出窗口已关闭
authPopup.close();
}
const new_authentication_id = action.authentication_id;
// 3. 使用 `new_authentication_id` 继续 Zap 创建过程。
// `new_authentication_id` 是用户刚刚创建的认证的 ID。
}
}
});

希望为您的应用添加认证?您可以直接提供认证详情并简化流程。请查看添加认证

当不需要认证时

某些应用无需认证,例如 Webhooks。通过获取应用时无法添加新认证,即可判断这种情况。

从 /apps 的示例响应

...
"links": {
"connect_new_authentication": null
},
...

在使用这些应用创建 Zap 时,应使用 null 替换有效的认证 ID;

示例:POST 到 /inputs

示例:POST 到 /zaps

// POST https://api.zapier.com/v2/actions/core:8yjfgwyq03zskh3LOe9jPa5dOeW/inputs
{
"data": {
"authentication": null,
"inputs": {}
}
}