Search code examples
sharepointmicrosoft-graph-apionedrive

Through which API do I access OneDrive at SharePoint


I have got an account username@someone.onmicrosoft.com.
When I log to OneDrive with this account using this link https://onedrive.live.com/about/en-us/, I am redirected to this link (line breaks added for clarity)

https://someone-my.sharepoint.com/
personal/itsupport_company_com
/_layouts/15
/onedrive.aspx?
FolderCTID=0x012000908740EE6082C74E82DD6BBFB737D5D5
&id=%2Fpersonal%2Fitsupport%5Fcompany%5Fcom%2FDocuments%2FReportsFolder

question 1: Can I extract something useful from the url (what does personal and itsupport_company_com represent?)?

I can see the files and folders in the web UI, all good so far.

Now I want to download some files from that OneDrive using REST API.

Right now, I am trying to use the Graph API. I have a registered application, I can get access_token with correct permissions (checked in http://jwt.calebb.net/) (The permissions are Files.Read.All Sites.Read.All User.Read), I can make requests (for example /me endpoint returns correct information.

So far so good.

Now I tried the GET /v1.0/sites endpoint (according to this, which yields this response:

{
  "error": {
    "code": "invalidRequest",
    "message": "Cannot enumerate sites",
    "innerError": {
      "request-id": "0d449eaf-21b8-4c9d-a372-7e9827e1ef19",
      "date": "2017-08-11T09:37:01"
    }
  }
}

So I can't get my site_id (whatever that means) to get the associated drive.

Next, I tried to request this endpoint GET /v1.0/me/drive, to no avail

{
  "error": {
    "code": "ResourceNotFound",
    "message": "User's mysite not found.",
    "innerError": {
      "request-id": "526c5bb3-23c0-4a85-8295-195b5e2d316c",
      "date": "2017-08-11T09:39:56"
    }
  }
}

Question 2: Why can't I access the resources? Is something wrong with my permissions? Is there a whole different api to access the onedrive/sharepoint?

I have a vague feeling, that there is no drive associated with my account and that the folder I can see in the UI is a shared one. Can I access that through the API?


Solution

  • Whew, i've got it. I don't understand everything, but here are the pieces. Hopefully someone might find it useful.

    The answer is, that you can access OneDrive/Sharepoint drives with the Graph API.

    In my case, the issue was that the drive I was trying to access was not my drive, but it was tied to a different account instead. I probably have some very unpriviledged account.

    In the end, querying this url works (with Bearer access token properly set in the header)

    https://graph.microsoft.com/v1.0/users/itsupport@company.com/drive/root:/RootFolderName:/children
    

    I got the username from the web url

    https://someone-my.sharepoint.com/
    personal
    /itsupport_company_com     #<--- this is the drive owner
    /_layouts/15
    /onedrive.aspx?
    FolderCTID=0x012000908740EE6082C74E82DD6BBFB737D5D5
    &id=%2Fpersonal%2Fitsupport%5Fcompany%5Fcom%2FDocuments%2FReportsFolder