开场白的apikey 生成方式是什么,会不会过期? #5258

Closed
opened 2026-02-21 18:10:08 -05:00 by yindo · 5 comments
Owner

Originally created by @haike-1213 on GitHub (Aug 27, 2024).

Self Checks

  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Provide a description of requested docs changes

开场白的apikey 生成方式是什么,会不会过期?

Originally created by @haike-1213 on GitHub (Aug 27, 2024). ### Self Checks - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Provide a description of requested docs changes 开场白的apikey 生成方式是什么,会不会过期?
yindo added the 📚 documentation label 2026-02-21 18:10:08 -05:00
yindo closed this issue 2026-02-21 18:10:08 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 27, 2024):

开场白的 API key 生成方式是使用 generate_token 函数。生成的 API key 会在 3 分钟(180 秒)后过期,这是由 API_TOKEN_TTL_SECONDS 常量定义的。

以下是相关代码:

API_TOKEN_TTL_SECONDS = 3 * 60

@cachetools.func.ttl_cache(maxsize=10, ttl=API_TOKEN_TTL_SECONDS - 30)
def generate_token(apikey: str):
    try:
        api_key, secret = apikey.split(".")
    except Exception as e:
        raise Exception("invalid api_key", e)

    payload = {
        "api_key": api_key,
        "exp": int(round(time.time() * 1000)) + API_TOKEN_TTL_SECONDS * 1000,
        "timestamp": int(round(time.time() * 1000)),
    }
    ret = jwt.encode(
        payload,
        secret,
        algorithm="HS256",
        headers={"alg": "HS256", "sign_type": "SIGN"},
    )
    return ret

生成的 token 会在 3 分钟后过期[1].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Aug 27, 2024): <!-- Answer --> 开场白的 API key 生成方式是使用 `generate_token` 函数。生成的 API key 会在 3 分钟(180 秒)后过期,这是由 `API_TOKEN_TTL_SECONDS` 常量定义的。 以下是相关代码: ```python API_TOKEN_TTL_SECONDS = 3 * 60 @cachetools.func.ttl_cache(maxsize=10, ttl=API_TOKEN_TTL_SECONDS - 30) def generate_token(apikey: str): try: api_key, secret = apikey.split(".") except Exception as e: raise Exception("invalid api_key", e) payload = { "api_key": api_key, "exp": int(round(time.time() * 1000)) + API_TOKEN_TTL_SECONDS * 1000, "timestamp": int(round(time.time() * 1000)), } ret = jwt.encode( payload, secret, algorithm="HS256", headers={"alg": "HS256", "sign_type": "SIGN"}, ) return ret ``` 生成的 token 会在 3 分钟后过期<sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/model_runtime/model_providers/zhipuai/zhipuai_sdk/core/_jwt_token.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@haike-1213 commented on GitHub (Aug 27, 2024):

/api/parameters 里面的 authorization 会过期吗?

@haike-1213 commented on GitHub (Aug 27, 2024): /api/parameters 里面的 authorization 会过期吗?
Author
Owner

@dosubot[bot] commented on GitHub (Aug 27, 2024):

/api/parameters 端点中的 authorization 参数会过期。Dify 平台使用 JWT(JSON Web Tokens)进行身份验证,JWT 的过期时间由配置文件中的 "{token_type.upper()}_TOKEN_EXPIRY_HOURS" 键来设置。具体的过期时间取决于配置中的设置值[1].

此外,Dify 还支持通过 GitHub 和 Google 等提供商进行 OAuth 身份验证,这些提供商的访问令牌也有各自的过期时间[2].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Aug 27, 2024): <!-- Answer --> `/api/parameters` 端点中的 `authorization` 参数会过期。Dify 平台使用 JWT(JSON Web Tokens)进行身份验证,JWT 的过期时间由配置文件中的 `"{token_type.upper()}_TOKEN_EXPIRY_HOURS"` 键来设置。具体的过期时间取决于配置中的设置值<sup>[[1]](https://github.com/langgenius/dify/blob/main/api/libs/helper.py)</sup>. 此外,Dify 还支持通过 GitHub 和 Google 等提供商进行 OAuth 身份验证,这些提供商的访问令牌也有各自的过期时间<sup>[[2]](https://github.com/langgenius/dify/blob/main/api/controllers/console/auth/oauth.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@crazywoola commented on GitHub (Aug 27, 2024):

I confirm that I am using English to submit report (我已阅读并同意 https://github.com/langgenius/dify/issues/1542).

@crazywoola commented on GitHub (Aug 27, 2024): I confirm that I am using English to submit report (我已阅读并同意 https://github.com/langgenius/dify/issues/1542).
Author
Owner

@crazywoola commented on GitHub (Aug 27, 2024):

你好,提问之前请查看我们相关的语言政策,为了更好的交流请使用英语并且使用相应的模版进行提交,有疑问可以使用 Discussion,我注意到你这边已经多次违反我们的语言政策,如果后续还是不遵守我们相关政策,我们会进行相关封禁处理。

@crazywoola commented on GitHub (Aug 27, 2024): 你好,提问之前请查看我们相关的语言政策,为了更好的交流请使用英语并且使用相应的模版进行提交,有疑问可以使用 Discussion,我注意到你这边已经多次违反我们的语言政策,如果后续还是不遵守我们相关政策,我们会进行相关封禁处理。
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#5258