Skip to main content
Known Participant
Nov 6, 2023 4:34 PM UTC

File Representation url returns 401

url=f'https://api.box.com/2.0/files/{file.file_id}?fields=representations'
                headers = {
                    "x-rep-hints" : "[extracted_text]",
                    "Authorization": f'Bearer {user.box_access_token}'
                }
                ext_text = requests.get(url,headers)
                print(ext_text.status_code)
                url_template = str(ext_text["representations"]["entries"][0]["content"]["url_template"])
                url = url_template.replace("{+asset_path}", "")
                resp = requests.get(url, headers={"Authorization": f"Bearer {user.box_access_token}"})
                resp.raise_for_status()
                print( resp.content)

The same call made through postman return success.

    17 replies

    rbarbosa Box
    Developer Advocate
    Nov 6, 2023 5:15 PM UTC

    Hi @MBenny

    Interesting. Look like a permission issue.

    However you seem to have a file object which I assume you got using the client object, under the same security context.

    You don’t say however where is it failing in your script, is it the first get or the second?

    Cheers

    Translate
    MBennyAuthor
    Known Participant
    Nov 6, 2023 5:40 PM UTC
                ext_text = requests.get(url,headers)
    

    This is where it get 401

    Translate
    MBennyAuthor
    Known Participant
    Nov 6, 2023 5:41 PM UTC

    I have all the necessary permissions in the access token. As using the same token in the postman API returns the values.

    Translate
    MBennyAuthor
    Known Participant
    Nov 6, 2023 5:45 PM UTC

    @rbarbosa
    I refactored the code to use make_request from the Client object.

                    res = get_file_information(client,file.file_id)
                    if res:
                        for entry in res['representations']['entries']:
                            if entry['representation'] == 'extracted_text':
                                extracted_text_url = entry['info']['url']
                                break
                        resp = client.make_request(
                            method='GET',
                            url=extracted_text_url,
                        )
                        rs = resp.json()
                        url_template = rs["content"]["url_template"]
                        updated_url = url_template.replace("{+asset_path}","")
                        headers = {
                            "x-rep-hints" : "[extracted_text]",
                            "Authorization": f'bearer {user.box_access_token}',
                            'responseType': 'text'
                        }
                        ext_text = requests.get(updated_url,headers)
                        print(ext_text)
    

    When I use the requests.get I still get the 401 error. But if I use the make_request of the Client object then I get the following error

    Non-json response received, while expecting json response.

    Translate
    rbarbosa Box
    Developer Advocate
    Nov 6, 2023 6:29 PM UTC

    Sorry @MBenny I didn’t explain my self properly.

    In your script are you getting the file object under the same security context?

    You code looks fine.

    In fact you can compare with this article:

    And sample code:

    Cheers

    Translate
    MBennyAuthor
    Known Participant
    Nov 7, 2023 1:52 AM UTC

    I have tried the sample code in the github but when I look into the File object in the installed sdk. I couldn’t find the ‘get_representation_info’ function, hence when I run the script I get the following error Error occurred : ‘get_representation_info’

    Yes, Everything is under the same secuirty context.

    Also I checked with the make_request function it returns a session response, Which is a BoxSession, inside which they expect_json_response=True is set, Hence when the response is of text type they invoke the following error ‘Non-json response received, while expecting json response.’

    @rbarbosa I have followed the installation of sdk from the following commands https://developer.box.com/guides/tooling/sdks/python/#installation

    Yet, some functions inside objects and authentications are not reflected in the installed sdk module.

    Translate
    smartoneinok Box
    Senior Developer Advocate
    Nov 7, 2023 3:02 PM UTC

    Hey - I sent this over to our Python SDK team - they should reply soon! Thanks.

    Translate
    Box Employee
    Nov 7, 2023 3:33 PM UTC

    Hi,
    @MBenny can you tell, which version of Python SDK do you use? get_representation_info method was introduced in Box Python SDK v2.1.0. Is it possible that you’re using older version than that? I’ve run your code in the most recent v3.9.2 and it worked without any trouble. Can you confirm that the same issue occurs with the most recent version? Can you share the result of the code below?

    import boxsdk
    import requests
    print(boxsdk.__version__)
    print(requests.__version__)
    
    Translate
    MBennyAuthor
    Known Participant
    Nov 17, 2023 6:31 AM UTC

    @lsocha , @rbarbosa

        print(boxsdk.__version__)
    AttributeError: module 'boxsdk' has no attribute '__version__'
    

    This is response

    Translate
    Box Employee
    Nov 20, 2023 1:02 PM UTC

    Hi @MBenny,

    You can also check the version with the following command:

    pip freeze | grep 'boxsdk'

    The latest version is 3.9.2, so if yours is different, please upgrade the package with the following command:

    pip install boxsdk --upgrade

    Then try calling the get_representation_info function again according to the example in the documentation 

    Hope this help!

    Translate