How to use access token during making GET Request to Sharepoint file?

2023-01-11T12:42:20.88+00:00

Hi, i use sharepoint as cloud storage. I have a few .pdf files there now, and gave them public accessibility. So i'm available to open file by this url in anonymous window.

I need to use current files in another system (Salesforce), and i need to make api callouts to GET those files(base64).

I got access_token by this endpoint: [https://accounts.accesscontrol.windows.net

User's image

This is file public url: [https://lpnu-my.sharepoint.com/personal/marian_lyzhychka_mtrte_2021_edu_lpnu_ua/_layouts/15/onedrive.aspx?id=%2Fpersonal%2Fmarian%5Flyzhychka%5Fmtrte%5F2021%5Fedu%5Flpnu%5Fua%2FDocuments%2Ftest%5Ffile%2Epdf&parent=%2Fpersonal%2Fmarian%5Flyzhychka%5Fmtrte%5F2021%5Fedu%5Flpnu%5Fua%2FDocuments&ga=1

When i tried to send GET request to this endpoint and set received Access_token as bearer token, i received 401 (Unauthorized).

I am available to get file if i get cookie from developer tools, and set as header. But for every file cookies are different and it work's for me only for testing.

Could you please explain me what i do wrong? And how to access public files via request. Thank you!

Microsoft 365 and Office SharePoint For business Windows
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2023-01-12T05:49:40.6266667+00:00

    Hi @Лижичка Мар'ян Ярославович,

    According to your description, do you want to get files in SharePoint using Rest API? If my understanding is wrong, please feel free to contact me.

    Based on my research and testing, you can use the following Rest API to get files in SharePoint:

    GET https://{site_url}/_api/web/lists/GetByTitle('Test')/items
    Authorization: "Bearer " + accessToken
    Accept: "application/json;odata=verbose"
    
    

    If you want to get a specific file, you can use the following REST API:

    GET https://{site_url}/_api/web/lists/GetByTitle('Test')/items({item_id})
    Authorization: "Bearer " + accessToken
    Accept: "application/json;odata=verbose"
    
    

    For example:

    01

    In addition, when you generate the access_token, are permissions granted to the add-in, and what permissions are granted? Please try granting Full Control permissions, for example: enter the below XML in the “Permission Request XML” box:

    <AppPermissionRequests AllowAppOnlyPolicy="true">
      <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
    </AppPermissionRequests>
    

    More information for reference:

    1. Rest API: Working with lists and list items with REST
    2. Generate access-token and access SharePoint: In 4 steps access SharePoint online data using postman tool

    Hope it can help you. Thanks for your understanding.

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    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.



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.