Hi all at the Microsoft Community,
I've been developing a simple Flask app designed to upload a small test file to a user's OneDrive on a once-daily schedule. It handles the 0Auth flow, and uses stored refresh tokens to request new access tokens when necessary. I've run in to a persistent problem I'm hoping you can help me with.
The scheduled download fails with the error message CompactToken validation failed with reason code: 80049228.
I'm aware that this issue can be caused by either (1) an expired access token, or (2) an incorrectly formatted upload request, especially viz the format of the header f'Authorization': 'Bearer {access_token}'. With regards to (1), the error occurs both on the app's initial request to the API (using a stored access token) and again after the app has successfully refreshed the token. If the issue was caused by a token's expiration, refreshing the token ought to solve it. As for (2), I believe I have formatted the request correctly.
I am working on a Linux Mint system, writing in Python 3.10.12.
Below is the Flask route called to initiate the upload. Sorry for all the extra unnecessary logging - that's been part of my effort to get the app uploading successfully.
Don't hesitate to ask for any more information you may need.
@app.route('/upload')
def upload():config._load("mysite/.env")global TRANS_PATHlogging.info("Attempting to decode MS_ACCESS_TOKEN")try:access_token = decode_token("MS_ACCESS_TOKEN")except Exception as e:logging.error(f"Error decoding Microsoft token: {e}")return f"Error decoding Microsoft token: {e}"logging.info("MS_ACCESS_TOKEN successfully decoded.")logging.info("Starting upload.")headers = {"Authorization": f"Bearer {access_token}",'Content-Type': 'application/octet-stream',}directory_split = TRANS_PATH.split("/")filename = f"{directory_split[-1]}.zip" # Adding .zip here# Use the filename with the .zip extension in the endpoint URLendpoint = f"https://graph.microsoft.com/v1.0/me/drive/items/root:/{filename}:/content"try:with open(f'mysite/{filename}', 'rb') as file:file_content = file.read()except IOError as e:logging.error(f"An error occurred while reading the file's contents: {e}")return f"File read error: {e}"try:response = requests.put(endpoint, headers=headers, data=file_content)except requests.RequestException as e:logging.error(f"A network error occurred during the upload to Microsoft: {e}")return f"Network error occurred: {e}"if response.ok:logging.info("Upload successful.")return f"Upload Successful", 200else:logging.info(f"The upload failed: {response.text}, {response.status_code}")return f"Upload failed: {response.text}", response.status_code