11

I'm trying to make an calendar view for my company to show which employees are available. I made an app in Azure, I can logon in to the system, I get an access token, but I can't get information.

I build the request url using curl and send this header with my request

array(6) {
 [0]=>
  string(28) "User-Agent: php-tutorial/1.0"
  [1]=>
  string(588) "Authorization: Bearer ~ACCESS_TOKEN~"
  [2]=>
  string(24) "Accept: application/json"
  [3]=>
  string(55) "client-request-id: ~GUID~"
  [4]=>
  string(30) "return-client-request-id: true"
  [5]=>
  string(45) "X-AnchorMailbox: ~MAIL~"
}

And all I get as a response is: " "

What am I doing wrong?

UPDATE

After logging in, Microsoft does not ask if i want to grant access to the app, but it does grant me an access token. Could this be the problem? And how can I make it ask for permission?

UPDATE 2

After logging in and trying to get information, I get a 401 Error saying: Access denied. Which is strange I think cause it already granted me an access token.

UPDATE 3

Couple days further and I have tried some example and tutorial code to check if it works. But even in these apps the api seem not to work. These are the projects I have tried.

https://github.com/microsoftgraph/php-connect-rest-sample

https://dev.outlook.com/restapi/tutorial/php

apero
  • 1,074
  • 2
  • 23
  • 38
  • Which API you are using? – Gary Liu Aug 10 '16 at 01:16
  • I tried different API's: openid, mail.read, calendars.read, contacts.read. But the strange thing is, when I logon it don't ask for permisson – apero Aug 10 '16 at 07:17
  • Hi, apero, have you tried to leverage https://graph.microsoft.io/en-us/graph-explorer# to test the APIs, to check whether its a permission issue? – Gary Liu Aug 11 '16 at 05:55
  • Hi Gary, Yes that works. And it asks me if I want to grant permession for the app. So something else is going wrong :( – apero Aug 11 '16 at 06:50
  • Please try the answer of http://stackoverflow.com/questions/37893089/requests-to-ms-graph-api-gives-me-authorization-request-denied-insufficient-p/37917392#37917392 – Gary Liu Aug 11 '16 at 06:59
  • Thank you for helping Gary. I have no access to the power shell. – apero Aug 11 '16 at 13:05

2 Answers2

7

According the error list at https://graph.microsoft.io/en-us/docs/overview/errors, when you get the 401 error, it means

Required authentication information is either missing or not valid for the resource.

It may be that your access token doesn't contain the scope for the resource your application requesting. E.g.

If you want to list the resource of calendar view, and according the document at https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/user_list_calendarview:

One of the following scopes is required to execute this API: Calendars.Read; Calendars.ReadWrite

Please login the portal of your Azure AD application, check the permissions Have full access to user calendars or Read user calendars of Microsoft Graph whether have been marked.

And you can check the "scope" section of the response body with your access token, refer to https://azure.microsoft.com/en-us/documentation/articles/active-directory-protocols-oauth-code/#use-the-authorization-code-to-request-an-access-token for more info.

Gary Liu
  • 13,758
  • 1
  • 17
  • 32
  • Sorry for the late reaction. This also did not help me :( please see my updated question – apero Aug 18 '16 at 06:44
  • Were you successful in curl function? Could you get anything with `$error= curl_error($ch);`? And what do you mean do not work? Do you get any info? – Gary Liu Aug 18 '16 at 07:23
  • Yes the curl function is succesfull,I get no errors. $error is empty. I get zero information thats what is very confussing – apero Aug 18 '16 at 08:41
0

Question is old, but i will post solution which worked for me.

Similarily as you i struggled to setup php client to work with Office365 (Sharepoint). Official docs are lacking examples (curls / requests / resposnes etc) so is hard to setup custom client.

Finally i found this library: https://github.com/vgrem/phpSPO - and with it i managed to authenticate via PHP and do stuff which i needed in Sharepoint.

domis86
  • 1,227
  • 11
  • 9