User’s mysite not found. OneDrive to Business Central

I’m attempting to automatically import a file from a cloud storage folder, in this case Microsoft OneDrive, into a table in a Microsoft Business Central SaaS environment using an AL extension. At the moment I’m getting error: ‘User’s mysite not found’. Any idea where I’m going wrong?

I’ve copied a test file into a folder on my OneDrive see below:

I’ve created an App Registration as below:

… and I have the AL extension code below

codeunit 50120 "OneDriveFileImporter"
{

    trigger OnRun()
    begin
        ImportFilesFromOneDrive();
    end;

    procedure ImportFilesFromOneDrive()
    var
        HttpClient: HttpClient;
        HttpRequestMessage: HttpRequestMessage;
        HttpResponseMessage: HttpResponseMessage;
        Headers: HttpHeaders;
        JsonResponse: JsonObject;
        JsonArray: JsonArray;
        JsonToken: JsonToken;
        JsonTokenLoop: JsonToken;
        JsonValue: JsonValue;
        JsonObjectLoop: JsonObject;
        AuthToken: Text;
        OneDriveFolderUrl: Text;
        ResponseText: Text;
        FileContent: InStream;
        FileName: Text;
        txtTemp: Text;
        UserPrincipalName: Text;
    begin
        // Get OAuth token
        AuthToken := GetOAuthToken();

        if AuthToken = '' then
            Error('Failed to obtain access token.');

        // Define the user principal name (replace with the actual user principal name)
        UserPrincipalName := '[email protected]';

        // Define the OneDrive folder URL

        // delegated permissions
        // OneDriveFolderUrl := 'https://graph.microsoft.com/v1.0/me/drive/root:/OneDriveFileImporter:/children';

        // application permissions
        OneDriveFolderUrl := 'https://graph.microsoft.com/v1.0/users/' + UserPrincipalName + '/drive/root:/OneDriveFileImporter:/children';

        // Initialize the HTTP request
        HttpRequestMessage.SetRequestUri(OneDriveFolderUrl);
        HttpRequestMessage.Method := 'GET';
        HttpRequestMessage.GetHeaders(Headers);
        Headers.Add('Authorization', 'Bearer ' + AuthToken);

        // Send the HTTP request
        if HttpClient.Send(HttpRequestMessage, HttpResponseMessage) then begin
            // Log the status code for debugging
            // Message('HTTP Status Code: %1', HttpResponseMessage.HttpStatusCode());

            if HttpResponseMessage.IsSuccessStatusCode() then begin
                HttpResponseMessage.Content.ReadAs(ResponseText);
                JsonResponse.ReadFrom(ResponseText);

                if JsonResponse.Get('value', JsonToken) then begin
                    JsonArray := JsonToken.AsArray();

                    foreach JsonTokenLoop in JsonArray do begin
                        JsonObjectLoop := JsonTokenLoop.AsObject();
                        if JsonObjectLoop.Get('name', JsonTokenLoop) then begin
                            JsonValue := JsonTokenLoop.AsValue();
                            FileName := JsonValue.AsText();
                            DownloadAndProcessFile(FileName, AuthToken);
                        end;
                    end;
                end;

            end else begin
                // HERE'S WHERE THE ERROR IS REPORTED!!!
                HttpResponseMessage.Content.ReadAs(ResponseText);
                Error('Failed to fetch files from OneDrive: %1 %2', HttpResponseMessage.HttpStatusCode(), ResponseText);
            end;
        end else
            Error('Failed to send HTTP request to OneDrive');
    end;

    procedure GetOAuthToken() AuthToken: Text
    var
        HttpClient: HttpClient;
        HttpRequestMessage: HttpRequestMessage;
        HttpResponseMessage: HttpResponseMessage;
        HttpContent: HttpContent;
        Headers: HttpHeaders;
        ResponseText: Text;
        JsonToken: JsonToken;
        JsonResponse: JsonObject;
        JsonValue: JsonValue;
        ClientID: Text;
        ClientSecret: Text;
        TenantID: Text;
        TokenEndpoint: Text;
        Content: Text;
    begin
        // Get client ID, client secret, and tenant ID from setup
        ClientID := 'myclientid.....';
        ClientSecret := 'myclientsecret.....';
        TenantID := 'mytenantid.....';

        // Define the token endpoint
        TokenEndpoint := 'https://login.microsoftonline.com/' + TenantID + '/oauth2/v2.0/token';

        // Define the request content
        Content := 'grant_type=client_credentials&client_id=' + ClientID + '&client_secret=' + ClientSecret + '&scope=https://graph.microsoft.com/.default';

        // Initialize the HTTP request
        HttpRequestMessage.SetRequestUri(TokenEndpoint);
        HttpRequestMessage.Method := 'POST';

        // Initialize the HTTP content
        HttpContent.WriteFrom(Content);
        HttpContent.GetHeaders(Headers);
        Headers.Remove('Content-Type');
        Headers.Add('Content-Type', 'application/x-www-form-urlencoded');
        HttpRequestMessage.Content := HttpContent;

        // Send the HTTP request
        if HttpClient.Send(HttpRequestMessage, HttpResponseMessage) then begin
            if HttpResponseMessage.IsSuccessStatusCode() then begin
                HttpResponseMessage.Content.ReadAs(ResponseText);
                JsonResponse.ReadFrom(ResponseText);
                if JsonResponse.Get('access_token', JsonToken) then begin
                    JsonValue := JsonToken.AsValue();
                    AuthToken := JsonValue.AsText();
                    Message(AuthToken);
                    exit(AuthToken);
                end else
                    Error('Failed to get access token from response');
            end else
                Error('Failed to get access token: %1', HttpResponseMessage.HttpStatusCode());
        end else
            Error('Failed to send HTTP request for access token');
    end;

    procedure DownloadAndProcessFile(FileName: Text; AuthToken: Text)
    var
        HttpClient: HttpClient;
        HttpRequestMessage: HttpRequestMessage;
        HttpResponseMessage: HttpResponseMessage;
        Headers: HttpHeaders;
        FileUrl: Text;
        FileContent: InStream;
    begin
        // haven't reached this pocedure yet.
    
        // Define the file URL
        FileUrl := 'https://graph.microsoft.com/v1.0/me/drive/root:/OneDriveFileImporter/' + FileName + ':/content';

        // Initialize the HTTP request
        HttpRequestMessage.SetRequestUri(FileUrl);
        HttpRequestMessage.Method := 'GET';
        HttpRequestMessage.GetHeaders(Headers);
        Headers.Add('Authorization', 'Bearer ' + AuthToken);

        // Send the HTTP request
        if HttpClient.Send(HttpRequestMessage, HttpResponseMessage) then begin
            if HttpResponseMessage.IsSuccessStatusCode() then begin
                HttpResponseMessage.Content.ReadAs(FileContent);
                // Process the file content (e.g., import into a table)
                ImportFileContent(FileContent);
            end else
                Error('Failed to download file: %1', HttpResponseMessage.HttpStatusCode());
        end else
            Error('Failed to send HTTP request to download file');
    end;

    procedure ImportFileContent(FileContent: InStream)
    var
    // haven't reached this pocedure yet.       
    // Define your table and variables here
    // FileTable: Record "Your File Table";
    // other variables
    begin
        // Implement your file content import logic here
        // For example, reading CSV data and inserting into a table
        // ...
    end;
}

I was expecting to see the test file in the OneDrive folder.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật