You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python使用blpapi连接远程bpipe遇INVALID_USER授权错误求助

BLPAPI连接远程bpipe时INVALID_USER授权错误排查

我在Python 3.9环境下使用blpapi连接远程bpipe应用,Demo Tool可正常运行,但自行编写的代码运行时返回INVALID_USER授权错误,连接看似正常却无法通过认证。以下是代码实现及错误返回信息,请问是否需要调整会话设置?

代码实现

from optparse import OptionParser
import blpapi

def parseCmdLine():
    parser = OptionParser(description="Retrieve reference data.")
    parser.add_option("-a",
                      "--ip",
                      dest="host",
                      help="server name or IP (default: %default)",
                      metavar="ipAddress",
                      default="xxx.xx.xxx.xx")
    parser.add_option("-p",
                      dest="port",
                      type="int",
                      help="server port (default: %default)",
                      metavar="tcpPort",
                      default=xxxx)

    (options, args) = parser.parse_args()
    return options

def main():
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost(options.host)
    sessionOptions.setServerPort(options.port)
    sessionOptions.setAuthenticationOptions("AuthenticationMode=APPLICATION_ONLY;ApplicationAuthenticationType=APPNAME_AND_KEY;ApplicationName=app:key")
    print("Connecting to %s:%s" % (options.host, options.port))
    # Create a Session
    session = blpapi.Session(sessionOptions)

    # Start a Session
    if not session.start():
        print("Failed to start session.")
        return

    try:
        # Open service to get historical data from
        if not session.openService("//blp/refdata"):
            print("Failed to open //blp/refdata")
            return
        session.generateToken()
        # Obtain previously opened service
        refDataService = session.getService("//blp/refdata")

        # Create and fill the request for the historical data
        request = refDataService.createRequest("HistoricalDataRequest")
        request.getElement("securities").appendValue("IBM US Equity")
        request.getElement("securities").appendValue("MSFT US Equity")
        request.getElement("fields").appendValue("PX_LAST")
        request.getElement("fields").appendValue("OPEN")
        request.set("periodicityAdjustment", "ACTUAL")
        request.set("periodicitySelection", "MONTHLY")
        request.set("startDate", "20060101")
        request.set("endDate", "20061231")
        request.set("maxDataPoints", 100)

        print("Sending Request:", request)
        # Send the request
        session.sendRequest(request)

        # Process received events
        while(True):
            # We provide timeout to give the chance for Ctrl+C handling:
            ev = session.nextEvent(500)
            for msg in ev:
                print(msg)

            if ev.eventType() == blpapi.Event.RESPONSE:
                # Response completly received, so we could exit
                break
    finally:
        # Stop the session
        session.stop()

if __name__ == "__main__":
    print("SimpleHistoryExample")
    try:
        main()
    except KeyboardInterrupt:
        print("Ctrl+C pressed. Stopping...")

返回错误信息

CID: {[ valueType=AUTOGEN classId=0 value=3 ]}
RequestId: 0c21a895-5bd5-449c-9296-86e1e8927933
HistoricalDataResponse = {
    responseError = {
        source = "rsfhdsvc2"
        code = 28
        category = "NO_AUTH"
        message = "Received error response from sescreate (service ID 343432) - Failed to load parmcm, due to a service error. Response=[ appCode = 1 message = "Failed to load parmcm asid 24756 asidSerial 118491 uuid 0 app 0 svc 0 flags 0: parmcmofflinethinpipe_managed_with_features: asid 24756 server mode (no identity(rc = 1217)(rc 1217)" isTransient = NULL ] [nid:44404] "
        subcategory = "INVALID_USER"
    }
}

问题分析与调整方案

从错误信息和代码逻辑来看,核心问题出在认证参数格式和会话流程上,以下是具体修正点:

  1. 修正认证参数配置格式
    当前ApplicationName用app:key的合并格式不符合APPNAME_AND_KEY类型的要求,需要拆分应用名称和密钥为独立参数:

    # 替换原认证配置代码
    sessionOptions.setAuthenticationOptions(
        "AuthenticationMode=APPLICATION_ONLY;"
        "ApplicationAuthenticationType=APPNAME_AND_KEY;"
        "ApplicationName=你的实际应用名称;"
        "ApplicationAuthenticationKey=你的实际应用密钥"
    )
    
  2. 调整generateToken()调用时机
    session.generateToken()需要在会话启动后、打开服务前调用,确保令牌生成在服务认证前完成:

    # Start a Session
    if not session.start():
        print("Failed to start session.")
        return
    try:
        # 先生成令牌,再打开服务
        session.generateToken()
        if not session.openService("//blp/refdata"):
            print("Failed to open //blp/refdata")
            return
        # 后续代码不变
    
  3. 匹配Demo Tool的认证配置
    由于Demo Tool可正常运行,需确保代码的认证参数与Demo Tool完全一致:

    • 确认Demo Tool使用的是APPLICATION_ONLY认证模式
    • 核对应用名称、密钥的拼写,确保无空格或字符错误

内容的提问来源于stack exchange,提问作者Alex Song

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 08:24:55