{"record":{"id":"09d74d2ebc94447c","repo":"binary-husky/gpt_academic","slug":"baidu-cloud-api-key","errorCode":null,"errorMessage":"没有配置BAIDU_CLOUD_API_KEY","messagePattern":"没有配置BAIDU_CLOUD_API_KEY","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"request_llms/bridge_qianfan.py","lineNumber":40,"sourceCode":"\n            # Call the function and cache the result\n            result = func(*args, **kwargs)\n            cache[key] = (result, datetime.now())\n            return result\n        return wrapper\n    return decorator\n\n@cache_decorator(timeout=3600)\ndef get_access_token():\n    \"\"\"\n    使用 AK，SK 生成鉴权签名（Access Token）\n    :return: access_token，或是None(如果错误)\n    \"\"\"\n    # if (access_token_cache is None) or (time.time() - last_access_token_obtain_time > 3600):\n    BAIDU_CLOUD_API_KEY, BAIDU_CLOUD_SECRET_KEY = get_conf('BAIDU_CLOUD_API_KEY', 'BAIDU_CLOUD_SECRET_KEY')\n\n    if len(BAIDU_CLOUD_SECRET_KEY) == 0: raise RuntimeError(\"没有配置BAIDU_CLOUD_SECRET_KEY\")\n    if len(BAIDU_CLOUD_API_KEY) == 0: raise RuntimeError(\"没有配置BAIDU_CLOUD_API_KEY\")\n\n    url = \"https://aip.baidubce.com/oauth/2.0/token\"\n    params = {\"grant_type\": \"client_credentials\", \"client_id\": BAIDU_CLOUD_API_KEY, \"client_secret\": BAIDU_CLOUD_SECRET_KEY}\n    access_token_cache = str(requests.post(url, params=params).json().get(\"access_token\"))\n    return access_token_cache\n    # else:\n    #     return access_token_cache\n\n\ndef generate_message_payload(inputs, llm_kwargs, history, system_prompt):\n    conversation_cnt = len(history) // 2\n    if system_prompt == \"\": system_prompt = \"Hello\"\n    messages = [{\"role\": \"user\", \"content\": system_prompt}]\n    messages.append({\"role\": \"assistant\", \"content\": 'Certainly!'})\n    if conversation_cnt:\n        for index in range(0, 2*conversation_cnt, 2):\n            what_i_have_asked = {}\n            what_i_have_asked[\"role\"] = \"user\"","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/request_llms/bridge_qianfan.py#L22-L58","documentation":"RuntimeError from get_access_token in bridge_qianfan: BAIDU_CLOUD_API_KEY is empty. Together with the secret-key check it guards the Baidu OAuth token request; both must be non-empty before the aip.baidubce.com/oauth/2.0/token POST is made. This is a pure configuration error raised before any network activity.","triggerScenarios":"Qianfan model selected with BAIDU_CLOUD_API_KEY unset; only the secret key configured; wrong config file edited (config.py instead of config_private.py being overwritten by updates).","commonSituations":"Partial credential setup; editing the wrong config file; CI without Baidu env vars running qianfan tests.","solutions":["Set BAIDU_CLOUD_API_KEY in config_private.py to the Qianfan app's API Key","Confirm BAIDU_CLOUD_SECRET_KEY is also set (the sibling check will fire next otherwise)","Prefer config_private.py so updates to config.py do not clobber your keys"],"exampleFix":"# config_private.py\n# before\nBAIDU_CLOUD_API_KEY = \"\"\n\n# after\nBAIDU_CLOUD_API_KEY = \"your-qianfan-api-key\"","handlingStrategy":"validation","validationCode":"api_key, secret = get_conf('BAIDU_CLOUD_API_KEY', 'BAIDU_CLOUD_SECRET_KEY')\nassert len(api_key) > 0, 'BAIDU_CLOUD_API_KEY empty - get it from Qianfan console > 应用详情'","typeGuard":"def baidu_api_key_present() -> bool:\n    return len(get_conf('BAIDU_CLOUD_API_KEY', '')[0] if isinstance(get_conf('BAIDU_CLOUD_API_KEY'), tuple) else get_conf('BAIDU_CLOUD_API_KEY')) > 0","tryCatchPattern":"try:\n    token = get_access_token()\nexcept RuntimeError as e:\n    if 'BAIDU_CLOUD_API_KEY' in str(e):\n        raise SystemExit('Set BAIDU_CLOUD_API_KEY in config_private.py') from e","preventionTips":["Configure both Baidu keys together - checking one at a time produces two failures","Copy keys from Qianfan console exactly (no surrounding quotes/spaces)","Validate at startup, not lazily mid-conversation"],"tags":["qianfan","baidu","configuration","credentials","api-key"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}