dify - 💡(How to fix) Fix When using Python code in the workflow to retrieve the list of knowledge bases, an error 401 Client Error: Unauthorized occurred. [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langgenius/dify#34881Fetched 2026-04-10 03:45:37
View on GitHub
Comments
1
Participants
2
Timeline
3
Reactions
1
Timeline (top)
closed ×1commented ×1labeled ×1

Error Message

When using Python code in the workflow to retrieve the list of knowledge bases, an error 401 Client Error: Unauthorized occurred. for url: http://xxxx/v1/datasets?page=1&limit=20" except Exception as e: an error 401 Client Error: Unauthorized occurred

RAW_BUFFERClick to expand / collapse

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.13.2

Cloud or Self Hosted

Cloud, Self Hosted (Docker)

Steps to reproduce

When using Python code in the workflow to retrieve the list of knowledge bases, an error 401 Client Error: Unauthorized occurred. for url: http://xxxx/v1/datasets?page=1&limit=20" import requests

def main(**kwargs): url = "http://xxxx/v1/datasets?page=1&limit=20" headers = {"Authorization": "Bearer app-VG3YqutGZYUkQ3JkMj71AFMU"}

try:
    response = requests.get(url, headers=headers)
    response.raise_for_status()

    return {
        "result": response.text
    }

except Exception as e:
    return {
        "result": str(e)
    }

✔️ Expected Behavior

The knowledge base list should be obtained.

❌ Actual Behavior

an error 401 Client Error: Unauthorized occurred

extent analysis

TL;DR

Verify the validity and scope of the provided Authorization Bearer token to resolve the 401 Unauthorized error.

Guidance

  • Check the expiration date and validity of the app-VG3YqutGZYUkQ3JkMj71AFMU token to ensure it has not expired and is correctly formatted.
  • Confirm that the token has the necessary permissions to access the http://xxxx/v1/datasets endpoint.
  • Review the documentation for the API to ensure the correct endpoint and authentication method are being used.
  • Test the token with a different endpoint or tool to isolate the issue.

Example

import requests

def main(**kwargs):
    url = "http://xxxx/v1/datasets?page=1&limit=20"
    headers = {"Authorization": "Bearer app-VG3YqutGZYUkQ3JkMj71AFMU"}

    # Add a check for the token's validity before making the request
    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()

        return {
            "result": response.text
        }

    except requests.exceptions.HTTPError as e:
        # Handle the 401 error specifically
        if e.response.status_code == 401:
            return {
                "result": "Invalid or expired token"
            }
        else:
            return {
                "result": str(e)
            }

Notes

The issue may be related to the token's validity, expiration, or permissions. Further investigation is needed to determine the root cause.

Recommendation

Apply workaround: Verify the token's validity and permissions before making the request, and handle the 401 error specifically to provide more informative error messages.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING