python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

onedrive : missing or not documented way to get file checksum

Open magowiz opened this issue 3 years ago • 2 comments

I read examples, documentation (both readme on github and https://o365.github.io/python-o365) but I was unable to find a way to get checksum information on a file stored in onedrive. Is it supported ?

Reading microsoft documentation here: https://docs.microsoft.com/en-us/graph/api/resources/onedrive?view=graph-rest-1.0 , it seems supported upstream, anyway I cannot find an hash/checksum attribute in a O365.drive.File object

Do you suggest me something to get this information?

magowiz avatar Jun 19 '22 15:06 magowiz

It is not implement for sure and I don't know if the MS graph api has this info. If the answer is yes, then some PR can be done to integrate this functionality.

alejcas avatar Jul 11 '22 09:07 alejcas

Hi @janscas , I implemented missing feature in my module using requests, I put my method here, in case it may help you to find where to get the information:

def _get_hash_from_id(self, file_id):
        with open(self.token_file, 'r') as token_file:
            token_data = json.loads(token_file.read())
        headers = {'Authorization': f'Bearer {token_data["access_token"]}'}
        base_url = 'https://graph.microsoft.com/v1.0/'
        response = requests.get(f'{base_url}drive/items/{file_id}', headers=headers)
        hashes = response.json()['file']['hashes']
        hashes_dict = {}
        for key in hashes:
            new_key = key.replace('Hash', '')
            hashes_dict[new_key] = hashes[key]
        return hashes_dict

it takes a file id which is attribute object_id of a File object, the method returns a dictionary with all available hashes, I removed prefix "Hash" from keys to leave only hash algorithm as key, I was able to reuse the token saved by your library.

magowiz avatar Jul 11 '22 09:07 magowiz

Implemented in #1061

Thanks @Chrisrdouglas

alejcas avatar Mar 19 '24 09:03 alejcas