How to read an Excel table from OneDrive using code (Typescript)

Mateus F Poletto 0 Reputation points
2023-07-07T18:55:11.0733333+00:00

In a project I'm working on, I need to read an Excel table from OneDrive based on a shared link, how can I do this? I'm having trouble doing it. I was previously able to read using a code flow provided in tutorials, but that flow requires me to first log in with an account and authorize through the browser with the code to access, but after a while, access is already expired. How can I read the table with the shared link or in some other way that can automate this, without having to authorize something or some code every time I run it?

The code flow tutorial i was using was this one:
https://developer.microsoft.com/en-us/graph/quick-start

But i don´t think is a good flow to make my thing automated.

Microsoft 365 and Office SharePoint Development
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2023-07-10T02:21:55.8666667+00:00

    Hi @Mateus F Poletto ,

    Do you want to access an Excel sheet from OneDrive using a shared link without user authorization every time you run your code?

    Please use the Microsoft Graph API and application-level permissions.

    Register the application in the Azure portal and grant it the required permissions to access OneDrive.

    User's image

    Create APP registrations:

    User's image

    Get Client ID and Tenant ID

    User's image

    Get Secret:

    User's image

    User's image

    User's image

    Add permissions

    User's image

    User's image

    User's image

    Install the necessary dependencies:

    npm install @microsoft/microsoft-graph-client
    

    Import the required modules in your TypeScript file:

    import { ClientSecretCredential } from "@azure/identity";
    import { Client } from "@microsoft/microsoft-graph-client";
    

    Configure the credentials and client:

    const clientId = "YourClientId";
    const clientSecret = "YourClientSecret";
    const tenantId = "YourTenantId";
    const credentials = new ClientSecretCredential(tenantId, clientId, clientSecret);
    
    //Create the Graph client:
    const graphClient = Client.initWithMiddleware({
      authProvider: credentials,
    });
    

    Use the client to make the API call

    Replace YourClientId, YourClientSecret, YourTenantIdwith the actual values corresponding to your environment

    Remember to grant the necessary permissions to your registered application in the Azure portal for accessing OneDrive and Excel.

    Please note that you may need to configure additional settings, handle authentication, and adjust the API endpoints based on your specific requirements.

    Through this method, before executing to obtain the content of the excel file, the code is verified first.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards

    Cheng Feng


  2. Mateus F Poletto 0 Reputation points
    2023-07-11T01:26:17.3166667+00:00

    Captura de tela de 2023-07-10 22-25-57


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.