OAthV2
ContentText := 'grant_type=password' +
'&client_id=' + UriBuilder.EscapeDataString(ClientID) +
'&client_secret=' + UriBuilder.EscapeDataString(ClientSecret) +
'&username=' + UriBuilder.EscapeDataString(UserID) +
'&password=' + UriBuilder.EscapeDataString(UserPassword) +
'&scope=' + UriBuilder.EscapeDataString('all');
procedure HttpRequest()
var
Token: Text;
AuthString: Text;
BearerLbl: Label 'Bearer %1', Locked = true;
begin
Token := GetToken();
Headers := Client.DefaultRequestHeaders();
AuthString := StrSubstNo(BearerLbl, Token);
Headers.Add('Authorization', AuthString);
RequestMessage.SetRequestUri(URL);
// your code
Client.Send(RequestMessage, ResponseMessage);
end;
procedure GetToken(): Text
var
UriBuilder: Codeunit Uri;
Content: HttpContent;
ContentHeaders: HttpHeaders;
Request: HttpRequestMessage;
Response: HttpResponseMessage;
ResponseText: Text;
Token: Text;
ContentText: Text;
begin
ContentText := 'grant_type=client_credentials'
'&client_id=' UriBuilder.EscapeDataString(ClientId)
'&client_secret=' UriBuilder.EscapeDataString(ClientSecret)
'&scope=' UriBuilder.EscapeDataString(Scope);
Content.GetHeaders(ContentHeaders);
ContentHeaders.Remove('Content-Type');
ContentHeaders.Add('Content-Type', 'application/x-www-form-urlencoded');
Request.Method := 'POST';
Request.SetRequestUri(Url);
Request.Content(Content);
if Client.Send(Request, Response) then
if Response.IsSuccessStatusCode() then
if Response.Content.ReadAs(ResponseText) then
Token := ResponseText; //check responsetext; sometimes is a json with key access_token
end;
// Line breaks for legibility only https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize? client_id=00001111-aaaa-2222-bbbb-3333cccc4444
&response_type=code &redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F &response_mode=query &scope=https%3A%2F%2Fgraph.microsoft.com%2Fmail.read
&state=12345 &code_challenge=YTFjNjI1OWYzMzA3MTI4ZDY2Njg5M2RkNmVjNDE5YmEyZGRhOGYyM2IzNjdmZWFhMTQ1ODg3NDcxY2Nl &code_challenge_method=S256
posted on 2024-09-24 14:39 lingdanglfw 阅读(7) 评论(0) 编辑 收藏 举报