该插件协议与OAuth兼容。我们期望的OAuth流程的一个简单示例应如下所示:
- 首先,在ChatGPT插件商店中选择“开发自己的插件”,并输入托管您的插件的域名(不能是localhost)。
- 在ai-plugin.json中,将auth.type设置为"oauth",如我们的OAuth示例中所示。
- 然后,您将被提示输入OAuth客户端ID和客户端密钥。
- 客户端ID和密钥可以是简单的文本字符串,但应遵循OAuth的最佳实践。
- 我们存储客户端密钥的加密版本,而客户端ID对最终用户是可用的。
- 一旦您将客户端ID和客户端密钥添加到ChatGPT UI中,您将收到一个验证令牌。
- 将验证令牌添加到ai-plugin.json文件的auth部分,如下所示。
- OAuth请求将包括以下信息:request={'grant_type': 'authorization_code', 'client_id': 'id_set_by_developer', 'client_secret': 'secret_set_by_developer', 'code': 'abc123', 'redirect_uri': 'https://chat.openai.com/aip/plugin-some_plugin_id/oauth/callback'}
- 为了让某人使用带有OAuth的插件,他们需要安装插件,然后在ChatGPT UI中显示“使用...登录”按钮。
- authorization_url端点应返回如下响应:{ "access_token": "example_token", "token_type": "bearer", "refresh_token": "example_token", "expires_in": 59 }
- 在用户登录过程中,ChatGPT会使用指定的authorization_content_type向您的authorization_url发出请求,我们期望得到一个访问令牌和一个可选的刷新令牌,我们用它定期获取新的访问令牌。
- 每次用户向插件发出请求时,用户的令牌都会在Authorization头中传递:(“Authorization”:“[Bearer/Basic][用户的令牌]”)。
出于安全原因,我们要求OAuth应用程序使用state参数。
以下是ai-plugin.json文件中的OAuth配置的示例:
```json
"auth": {
"type": "oauth",
"client_url": "https://example.com/authorize",
"scope": "",
"authorization_url": "https://example.com/auth/",
"authorization_content_type": "application/json",
"verification_tokens": {
"openai": "Replace_this_string_with_the_verification_token_generated_in_the_ChatGPT_UI"
}
},
```
为了更好地理解OAuth的URL结构,以下是字段的简短描述:
- 当您使用ChatGPT设置插件时,您将被要求提供您的OAuth client_id和client_secret。
- 当用户登录插件时,ChatGPT会将用户的浏览器定向到"[client_url]?response_type=code&client_id=[client_id]&scope=[scope]&state=xyz123&redirect_uri=https%3A%2F%2Fchat.openai.com%2Faip%2F[plugin_id]%2Foauth%2Fcallback"
- plugin_id是通过发送到您的OAuth端点的请求传递的(请注意,它今天在ChatGPT UI中不可见,但将来可能会)。您可以在那里检查请求以查看plugin_id。我们期望在重定向回redirect_uri时传递状态。如果状态与初始状态不匹配,或已过期,身份验证流程将失败。
- 在您的插件重定向回给定的redirect_uri之后,ChatGPT将通过向authorization_url发出POST请求并使用内容类型authorization_content_type和参数{ “grant_type”: “authorization_code”, “client_id”: [client_id], “client_secret”: [client_secret], “code”: [重定向返回的代码], “redirect_uri”: [与之前相同的重定向uri] }来完成OAuth流程。
评论