Xia
Xia

Reputation: 166

Basic Microsoft Graph App to retrieve a shared Excel File

I am trying to understand Microsoft Graph APIs but I found them kind of confusing. There's an excel file that is shared with me on OneDrive. I want to download it using Microsoft Graphs. I was able to generate the app and set up its configurations and permissions so that I can send a request and get an authorization token that I can use to send requests.

However, I have a couple problems, even in Graph Explorer, which I use as my account, not as the app.

To find out the file Id that is shared with me, I used the "files shared with me" API that is under OneDrive. (https://graph.microsoft.com/v1.0/me/insights/shared)

I copied the id that I get from there and used it in "worksheets in a workbook" API under Excel category. https://graph.microsoft.com/v1.0/me/drive/items/{drive-item-id}/workbook/worksheets)

However I got Item Not found error. The problem is, even though I can see it on oneDrive as a shared object I can't seem to open it using the second API.

How can I download the Excel file that is shared with me through OneDrive? Which API should I use? The app that will send the requests is a standalone app that will send the requests without human authorization. I don't know the correct terminology here but Microsoft doesn't allow it to have a /me request, it gets "/me request is only valid with delegated authentication flow.", therefore I probably need someway for app to see that oneDrive link. But I was unable to find a working way.

Upvotes: 0

Views: 476

Answers (1)

Shane Powell
Shane Powell

Reputation: 14148

The error message "/me request is only valid with delegated authentication flow." means what it says. There are two main ways to authenicate a application with the graph API. Application authenication and On behalf of user authencitation. So the error message is telling you that you are using the application authenication and the API path /me is only accessable with On behalf of user authencitation (e.g. delegated authentication). This makes sense because when using the application authenication who is me?

The API endpoints says what authentication types they support. If you take the worksheets API it says under permissions section:

Permission type - Permissions (from least to most privileged)

Delegated (work or school account) - Files.ReadWrite

Delegated (personal Microsoft account) - Not supported.

Application - Not supported.

Which is telling us that Application and Delegated for personal Microsoft accounts are not supported and that Delegated for work/school accounts requires the Files.ReadWrite permission.

To download the drive item you can use the download item API.

This supports both Application and Delgated permissions. For Application permisions you will not be able to use the /me path though. You will most likely have to use something like:

/users/{userId}/drive/items/{item-id}/content

I am assuming your file is stored in a work account one drive storage and not in one of the other storage places (AD group, etc).

Upvotes: 1

Related Questions