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.