0

I try to make programm in python to add appointments to Microsoft To Do I'm using the Microsoft Graph API with following code:

import requests
import msal
import atexit
import os.path

TENANT_ID = 'X
CLIENT_ID = 'Y'


AUTHORITY = 'https://login.microsoftonline.com/' + TENANT_ID
ENDPOINT = 'https://graph.microsoft.com/v1.0'

SCOPES = [
    'Files.ReadWrite.All',
    'Sites.ReadWrite.All',
    'User.Read',
    'User.ReadBasic.All',
    'Tasks.ReadWrite'
]


cache = msal.SerializableTokenCache()

if os.path.exists('token_cache.bin'):
    cache.deserialize(open('token_cache.bin', 'r').read())

atexit.register(lambda: open('token_cache.bin', 'w').write(cache.serialize()) if cache.has_state_changed else None)

app = msal.PublicClientApplication(CLIENT_ID, authority=AUTHORITY, token_cache=cache)

accounts = app.get_accounts()
result = None
if len(accounts) > 0:
    result = app.acquire_token_silent(SCOPES, account=accounts[0])

if result is None:
    flow = app.initiate_device_flow(scopes=SCOPES)
    if 'user_code' not in flow:
        raise Exception('Failed to create device flow')

    print(flow['message'])

    result = app.acquire_token_by_device_flow(flow)
   
if 'access_token' in result:
    print(result['access_token'])
    result = requests.get(f'https://graph.microsoft.com/v1.0/me/todo/lists', headers={'Authorization': 'Bearer ' + result['access_token']})
    print(result.json())

else:
    raise Exception('no access token in result')


This is the error I get:

{'error': {'code': 'UnknownError', 'message': 'The service is unavailable.', 'innerError': {'date': '2022-01-25T18:52:03', 'request-id': 'X', 'client-request-id': 'X'}}}

I tried to google the error but I didn't found any solution that worked for me.

martineau
  • 119,623
  • 25
  • 170
  • 301
Mehmet
  • 31
  • 1
  • 1
  • 4

2 Answers2

1

Just for Curiosity ,I would like to know have you entered your TENANT_Id and CLIENT_ID in place of "X" and "Y". if yes and still its not working.

Could you please try to run the URL - https://graph.microsoft.com/v1.0/me/todo/lists on Graph explorer and postman. let us know if you are still facing the issue.

vicky kumar
  • 563
  • 3
  • 11
  • using the Graph Explorer I get this now: ```{ "error": { "code": "InvalidAuthenticationToken", "message": "CompactToken parsing failed with error code: 80049217", "innerError": { "date": "2022-02-07T15:28:56", "request-id": "8b5e71fa-962e-41f9-be3e-d12a2e1a65c4", "client-request-id": "971d0024-3ad5-d746-332f-388bb77826bf" } } }``` using this as header: `Authorization: Bearer eyXXXXXX` I get the Token for Bearer Authorization from the python code above – Mehmet Feb 07 '22 at 15:30
  • 1
    Thanks for the update @Mehmet , looks like there is something wrong with your token . to get more clarity we would like to know , have you logged in graph explorer with your credential before API call ? – vicky kumar Feb 07 '22 at 15:59
  • Have you also tried in postman? – vicky kumar Feb 07 '22 at 15:59
  • Yes I logged in before. Postman response: `{ "error": { "code": "InvalidAuthenticationToken", "message": "Access token has expired or is not yet valid.", "innerError": { "date": "2022-02-07T16:57:37", "request-id": "9963da52-6203-4b7f-afdf-2f1e02744b75", "client-request-id": "9963da52-6203-4b7f-afdf-2f1e02744b75" } } }` but the token should work because I can acces: `https://graph.microsoft.com/v1.0/me/` and get my name, email and more back. Maybe I have a wrong "token" for the to do list api – Mehmet Feb 07 '22 at 16:58
  • Hi @Mehmet , could you please use access token and check if you have required permissions or not to perform those actions on https://jwt.ms/ . – vicky kumar Feb 08 '22 at 07:59
  • "scp": "Files.ReadWrite.All Sites.ReadWrite.All Tasks.ReadWrite User.Read User.ReadBasic.All profile openid email", – Mehmet Feb 08 '22 at 19:20
  • Do you maybe have a working code in python or c# – Mehmet Feb 08 '22 at 19:20
  • Thanks @Mehmet for the update ,Everything looks perfect , but the issue is not with your python code , there is some issue with your token . let me check what's going wrong . – vicky kumar Feb 09 '22 at 05:59
1

could you please check your token expiry time (exp), might be it got expired. Go to jwt.ms to check token details, find the attached image to see where to find token time. check exp time

If the token got expired ,please the docs get new token.

vicky kumar
  • 563
  • 3
  • 11
  • I get this for "exp": 1644251799. I assume its UNIX Time and converted it tellks me 2 days ago. – Mehmet Feb 09 '22 at 16:58
  • After getting a new token: ```"iat": 1644425838, "nbf": 1644425838, "exp": 1644430565,``` i get the same 503 error: ``` result = requests.get('https://graph.microsoft.com/v1.0/me/todo/lists', headers={'Authorization': 'Bearer ' + result['access_token']}) result.raise_for_status() print(result.json()) ``` I get this error: ```raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 503 Server Error: Service Unavailable for url: https://graph.microsoft.com/v1.0/me/todo/lists``` Funfact: I dont have Office365 Subscription – Mehmet Feb 09 '22 at 17:05
  • Interesting! , that is the main issue ,accessing todo list requires 0365 subscription. Please try to get the subscription first - https://techcommunity.microsoft.com/t5/microsoft-365-developer-platform/idb-p/Microsoft365DeveloperPlatform . – vicky kumar Feb 11 '22 at 07:02