0
votes

This is what I'm doing.

I want to develop an Application that can have access and management to calendars in office 365 tenant, using Microsoft Graph API, through Microsoft Azure. The company has the office 365 Business with 10 users and access to Azure Active Directory. I am using python 3.5 and requests library to layout the Authorization Code Grant Flow.

I have registered my application within Windows Azure Active Directory, and provided this application with all access permissions needed, as well as the Reply URL. Client Secret key has also been issued.

I read the following link: https://graph.microsoft.io/en-us/docs/authorization/app_authorization


Here, the process i followed:

Firstly, getting the Autorization code:

def triggerAutorization(request):
state = str(uuid4())
payload = { 
"client_id": client_id, 
"response_type": "code", 
"state": state, 
"redirect_uri": "http://localhost:8000/authorized",
"prompt": "consent"
}
url = "https://login.microsoftonline.com/{tenant}/oauth2/authorize?" + urllib.parse.urlencode(payload)
return  HttpResponseRedirect(url)

Secondly, getting the token

def requestToken(request):
    headers = { 'Content-Type' : "application/x-www-form-urlencoded"}
    post_data = {
    "client_id": client_id,
    "client_secret": client_secret,
    "code" : request.session['code'],
    "redirect_uri" : "http://localhost:8000/authorized",
    "grant_type": "authorization_code",
    "resource": "https://graph.microsoft.com/"
    }
    raw_response = requests.post("https://login.microsoftonline.com/{tenant}/oauth2/token?",  data=post_data, headers= headers)
    json_response = raw_response.json()
    if json_response['access_token']:
        request.session['access_token'] = json_response['access_token']

    return HttpResponseRedirect('/createquote')

Third, everything it is fine, I got a access token along with the rights to access calendars(i suppose):

'scope': 'Calendars.Read Calendars.Read.All Calendars.Read.Shared Calendars.ReadWrite Calendars.ReadWrite.All Contacts.Read.Shared Directory.AccessAsUser.All Directory.Read.All Files.Read Files.Read.All Files.Read.Selected Files.ReadWrite Files.ReadWrite.All Mail.Read Mail.ReadWrite.All Mail.Send Mail.Send.All openid profile User.Read User.Read.All User.ReadBasic.All', 
'expires_on': '1485932306', 
'refresh_token': 'AQAB..', 
'resource': 'https://graph.microsoft.com/', 
'token_type': 'Bearer', 
'expires_in': '3600', 
'ext_expires_in': '0', 
'not_before': '1485928406', 
'access_token': 'eyJ0...', 
'id_token': 'eyJ0...'

Fourth, the problem comes here, when i try to make the api call, becouse the response is a 500 server error, with no meaninful details.

def getCalendarList(request):

    token = request.session['access_token']
    headers = { 
    'User-Agent' : 'pythoncontacts/1.2',
    'Authorization' : 'bearer {0}' . format(token ),
    'Content-Type' :"application/json;odata.metadata=minimal;odata.streaming=true"
}
    request_id = str(uuid.uuid4())
    instrumentation = { 'client-request-id' : request_id,
                        'return-client-request-id' : 'true' }
    headers.update(instrumentation)
    raw_response = requests.get("https://graph.microsoft.com/v1.0/me/calendars", headers = headers)
    json_response = raw_response.json()
    return HttpResponse(" %s" %str(json_response))

The funny thing, is that when i change the api call with "https://graph.microsoft.com/v1.0/me/" it works.

Community, i hope you can help me out. I have read tons of documentation and tried different approaches, yet i couldn get this to work.

Many Thanks is advance.

Much appreciate your attention.

2
Can you confirm that you can see the default calendar for this user at outlook.offfice365.com? Also, it isn't clear from your description if which Business license they have or they're using Exchange Online.Marc LaFleur
Thanks Marc LaFleur. !! :-)Jose Luis Muñoz

2 Answers

1
votes

I have found the solution. It happened to be my Office 365 plan. Users created with "Office 365 business" edition are not able to use Microsoft Graph in full. After a lot research, I signed up for a Business Premium subscription trial. I created a user under that plan and then I use that account to sign in and i finally get his calendars. Moreover, I changed the process to use the V2.0 endpoints.

The funny thing is that sometimes, Microsoft services error responses dont explain the real causes of the problems.

for further developers, that may have to deal with the same problem, please be aware of the business plans that actually work with microsoft Graph and Azure AD.

Office 365 Midsize Business (now call as Office 365 Business premium) Office 365 Enterprise E1, E3, E4, or K1 Office 365 Education Office 365 Developer

For more information, this is the URL:

https://msdn.microsoft.com/en-us/office/office365/howto/setup-development-environment#bk_Office365Account

I would be happy to post the code that work for me. Finally, excuse me please grammar or semantic errors.

Thanks to all the community.

0
votes

I haven't done the whole process in raw, I am sure that there must be some oath2 libraries to help work at a higher level without writing the headers yourself. There is a great tutorial from an MSFT guy jasonjoh even though he uses django. How are you storing the tokens? I think they are more than 4k!

Hope this helps