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

Check_status of copied DriveItem not working as intended

Open jeroenvermunt opened this issue 2 years ago • 5 comments

I want to modify a copied DriveItem like so:

copy = calculators.copy(folder)

for status, perc_complete in copy.check_status():
    print(status, perc_complete)

item = copy.get_item()

However, the check_status() method seems to fail on me, returning the following:

Client Error: 401 Client Error: Unauthorized for url: https://my_domain.sharepoint.com/_api/v2.0/drives/{drive_id}/items/item_id}) | Error Message: Invalid audience Uri 'https://graph.microsoft.com/'.

This Error arises as a requests.exceptions.HTTPError when running CopyOperation._request_status(). I highlighted the line below. It seems as the monitor_url breaks when the copy is complete, whilst not properly finishing the _request_status() method. I tried catching the error around the relevant line, but can't find a good way to get the driveItem in the form of https://graph.microsoft.com/beta/drive/{drive_id}/items/{item_id}. Any ideas to solve this issue?

def _request_status(self):
        """ Checks the api endpoint to check if the async job progress """

        if self.item_id:
            return True

        response = self.con.get(self.monitor_url)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            
        if not response:
            return False

        data = response.json()

        self.status = data.get('status', 'inProgress')
        self.completion_percentage = data.get(self._cc('percentageComplete'),
                                              0)
            
        self.item_id = data.get(self._cc('resourceId'), None)

        return self.item_id is not None

jeroenvermunt avatar Nov 09 '22 14:11 jeroenvermunt

I don't have a clue right now... sorry

alejcas avatar Nov 11 '22 11:11 alejcas

Hi @alejcas @jeroenvermunt ,

I encountered this issue yesterday and after some debugging I found the following issues:

  1. The monitor url request is being sent using an OAuthSession object however in the documentation here no auth is actually required for the monitor url. In fact the auth headers seem to cause this to fail. I ran a test by replacing the CopyOperation.con.session attribute with a CopyOperation.con.get_naive_session() object and this resolved the issue. Resolution seems to be to use a naive session for the monitor request.
  2. The call to get_item method fails after patching the above because when the CopyOperation is initialized here the parent is passed as self.drive but should actually be target.drive for the logic in get_item to work. Not sure what the best resolution to this is, maybe to create new attributes on the CopyOperation called source_drive and target_drive to make this clearer and parent can stay as the source_drive?

Happy to put a PR (or two) in for these issues in the next week or so

will-byrne-cardano avatar Dec 12 '23 17:12 will-byrne-cardano

Hi! @will-byrne-cardano thanks for your effort!

I've merged PR #1031, Can you make another one to fix 2). I think having source_drive and target_drive it's perfect.

Thanks!

alejcas avatar Jan 08 '24 08:01 alejcas

@alejcas - PR is supplied here https://github.com/O365/python-o365/pull/1033/files. Forgot to mention this issue on the PR summary but have updated now

will-byrne-cardano avatar Jan 08 '24 09:01 will-byrne-cardano

thanks!

alejcas avatar Jan 08 '24 09:01 alejcas