django Box SDK客户端as_user请求需要比访问令牌提供的权限更高的权限

ca1c2owp  于 2023-01-27  发布在  Go
关注(0)|答案(1)|浏览(127)

我在Django项目中有以下代码:

# implememtation
module_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))  # get current directory
box_config_path = os.path.join(module_dir, 'py_scripts/transactapi_funded_trades/config.json') # the config json downloaded
config = JWTAuth.from_settings_file(box_config_path) #creating a config via the json file
client = Client(config) #creating a client via config  
user_to_impersonate = client.user(user_id='8********6') #iget main user
user_client = client.as_user(user_to_impersonate) #impersonate main user

上面的代码是我用来将用户从Box创建的服务帐户转移到ID为8*********的主帐户用户的代码。到目前为止,没有抛出任何错误,但是当我尝试实现检索文件的实际逻辑时,我得到了以下结果:

[2022-09-13 02:50:26,146: INFO/MainProcess] GET https://api.box.com/2.0/folders/0/items {'headers': {'As-User': '8********6',
             'Authorization': '---LMHE',
             'User-Agent': 'box-python-sdk-3.3.0',
             'X-Box-UA': 'agent=box-python-sdk/3.3.0; env=python/3.10.4'},
 'params': {'offset': 0}}
[2022-09-13 02:50:26,578: WARNING/MainProcess] "GET https://api.box.com/2.0/folders/0/items?offset=0" 403 0
{'Date': 'Mon, 12 Sep 2022 18:50:26 GMT', 'Transfer-Encoding': 'chunked', 'x-envoy-upstream-service-time': '100', 'www-authenticate': 'Bearer realm="Service", error="insufficient_scope", error_description="The request requires higher privileges than provided by the access token."', 'box-request-id': '07cba17694f7ea32f0c2cd42790bce39e', 'strict-transport-security': 'max-age=31536000', 'Via': '1.1 google', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"'}
b''
[2022-09-13 02:50:26,587: WARNING/MainProcess] Message: None
Status: 403
Code: None
Request ID: None
Headers: {'Date': 'Mon, 12 Sep 2022 18:50:26 GMT', 'Transfer-Encoding': 'chunked', 'x-envoy-upstream-service-time': '100', 'www-authenticate': 'Bearer realm="Service", error="insufficient_scope", error_description="The request requires higher privileges than provided by the access token."', 'box-request-id': '07cba17694f7ea32f0c2cd42790bce39e', 'strict-transport-security': 'max-age=31536000', 'Via': '1.1 google', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"'}
URL: https://api.box.com/2.0/folders/0/items
Method: GET
Context Info: None

它说它需要更高的访问权限。我可能做错了什么?我已经被这个特殊的问题困扰了一个多星期了,所以任何帮助都是非常感谢的。

ivqmmu1c

ivqmmu1c1#

你能测试一下用户是否真的被模拟了吗?

from boxsdk import JWTAuth, Client

def main():
    """main function"""
    auth = JWTAuth.from_settings_file('./.jwt.config.json')
    auth.authenticate_instance()
    client = Client(auth)

    me = client.user().get()
    print(f"Service account user: {me.id}:{me.name}")

    user_id_to_impersonate = '18622116055'
    folder_of_user_to_impersonate = '0'

    user_to_impersonate = client.user(user_id=user_id_to_impersonate).get()
    # the .get() is just to be able to print the impersonated user
    print(f"User to impersonate: {user_to_impersonate.id}:{user_to_impersonate.name}")

    user_client = client.as_user(user_to_impersonate)
    items = user_client.folder(folder_id=folder_of_user_to_impersonate).get_items()

    print(f"Items in folder:{items}")
    # we need a loop to actually get the items info
    for item in items:
        print(f"Item: {item.type}\t{item.id}\t{item.name}")

if __name__ == '__main__':
    main()

查看我的输出:

Service account user: 20344589936:UI-Elements-Sample
User to impersonate: 18622116055:Rui Barbosa
Items in folder:<boxsdk.pagination.limit_offset_based_object_collection.LimitOffsetBasedObjectCollection object at 0x105fffe20>
Item: folder    172759373899    Barduino User Folder
Item: folder    172599089223    Bookings
Item: folder    162833533610    Box Reports
Item: folder    163422716106    Box UI Elements Demo

相关问题