简体   繁体   中英

Business Central Get Token SharePoint

I'm trying to connect the business central with the sharepoint, I already did it in the postman, it worked fine, the GET and POST. But now Im trying to do it on BC, the token is created but not working, I think it has some problem with the connection or the URL's.

codeunit 50100 "APISharePoint"
{

    var
        OAuth2: Codeunit OAuth2;
        ClientIdTxt: Label '4d148348-137a-40e7-b20c-a458c2a03c65', Locked = true;
        ClientSecret: Label 'Ai2pOhE7yeZ8WRxrwSrHdpVtNqJg51tg2X+s8CJDLy4=', Locked = true;
        ResourceUrlTxt: Label 'https://crmbc384959.sharepoint.com', Locked = true;
        OAuthAuthorityUrlTxt: Label 'https://login.microsoftonline.com/ec36749a-af47-4f04-892c-98666b6cac1b/oauth2/v2.0/token', Locked = true;


    procedure GetAccessToken(): Text
    var
        PromptInteraction: Enum "Prompt Interaction";
        AccessToken: Text;
        AuthCodeError: Text;
        RedirectURLTxt: Text;
    begin
        // OAuth2.GetDefaultRedirectURL(RedirectURLTxt);
        RedirectURLTxt := 'https://localhost';
        if OAuth2.AcquireTokenWithClientCredentials(ClientIdTxt, ClientSecret, OAuthAuthorityUrlTxt, RedirectURLTxt, ResourceURLTxt, AccessToken) then
            exit(AccessToken)
        else
            exit('');
    end;


    procedure HttpGet(AccessToken: Text; Url: Text /*var JResponse: JsonObject*/): Boolean
    var
        Client: HttpClient;
        Headers: HttpHeaders;
        RequestMessage: HttpRequestMessage;
        ResponseMessage: HttpResponseMessage;
        RequestContent: HttpContent;
        ResponseText: Text;
        IsSucces: Boolean;

    begin
        Headers := Client.DefaultRequestHeaders();
        Headers.Add('Authorization', StrSubstNo('Bearer %1', AccessToken));
        Headers.Add('Accept', 'application/json;odata=verbose');
        RequestMessage.Content.GetHeaders(Headers); //asd
        Headers.Remove('Content-Type');
        Headers.Add('Content-Type', 'application/json;odata=verbose');
        RequestMessage.SetRequestUri(Url);
        RequestMessage.Method := 'GET';

        if Client.Send(RequestMessage, ResponseMessage) then
            if ResponseMessage.IsSuccessStatusCode() then begin
                if ResponseMessage.Content.ReadAs(ResponseText) then
                    IsSucces := true;
                //     end else
                //         ResponseMessage.Content.ReadAs(ResponseText);

                // JResponse.ReadFrom(ResponseText);
                exit(IsSucces);

            end;

    end;

Maybe your problem is the combination of GET with a request content. As far as i know, the underlaying implementation of the HttpClient in .NET does not support GET with a request content. You can read more about it here: How to use HttpClient to send content in body of GET request?

Maybe you can try GET without setting the content-type on the content or use POST or PUT instead.

But you didn't supply a error message so this is just a guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM