Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

HttpClient.Get() returns FORBIDDEN when making call to SharePoint URL

(1) ShareShare
ReportReport
Posted on by 227
Hello!
 
I have this small piece of code that is the beginning of a bigger procedure:
 

procedure ImportItemPictureFromURL()

    var

        Item: Record Item;

        Client: HttpClient;

        Content: HttpContent;

        Response: HttpResponseMessage;

        InStr: InStream;

    begin

        if PictureURL = '' then

            Error('Please enter a URL');

        Client.Get(PictureURL, Response);

        Response.Content.ReadAs(InStr);

        Item.SetRange("No.", ItemNo);

 
 
The procedure was made to upload images via a URL to items in Business Central, and it works really well.
However, I have encountered an issue, and that is the fact that all the images are stored in a SharePoint environment, and the URL's also reflect that.
When making a Client.Get() to the SharePoint URL, I get a 403 FORBIDDEN response, which is expected I suppose.
 
My question is - how can I modify my code to make sure I am authorized and can do a Client.Get() call to the URL and get the image in response?
 
  • Hagru Profile Picture
    Hagru 227 on at
    HttpClient.Get() returns FORBIDDEN when making call to SharePoint URL
    I have already set up an app in Azure in order to make API calls from Postman, I think similar to how Yun Zhu did in the first link he posted. I tried using the same authorization in Postman with a valid token, but got the "401 UNAUTHORIZED" in response. 
     
    Is there something specific I need to do to give it access to the SharePoint as well?
     
    Once I have the authorization down, I am sure I can modify the code in a similar way Yun Zhu did to send the token with the call.
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 74,115 Super User 2024 Season 2 on at
    HttpClient.Get() returns FORBIDDEN when making call to SharePoint URL
    I recently tested some OneDrive examples and hope they can give you some tips.
    Dynamics 365 Business Central: How to use OAuth 2.0 in AL with SecretText (Using codeunit 501 OAuth2)
    Dynamics 365 Business Central: How to import/read files from OneDrive to Business Central via AL (Graph API)
    Dynamics 365 Business Central: How to upload files from Business Central to OneDrive via AL (Graph API)
     
    Thanks.
    ZHU
     
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 9,818 Super User 2024 Season 2 on at
    HttpClient.Get() returns FORBIDDEN when making call to SharePoint URL
    Hi,
     
    You must have the access token to authorize.
     
    How get the access token ? here you must read the MS documentation.
     
    BR,
    Mohamed Amine MAHMOUDI

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans