Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Indy und OAuth / Microsoft365 (https://www.delphipraxis.net/213664-indy-und-oauth-microsoft365.html)

Olli73 7. Sep 2023 08:47

AW: Indy und OAuth / Microsoft365
 
Hilft dir das hier weiter?

https://learn.microsoft.com/de-de/ex...by-using-oauth

unter anderem:

Zitat:

base64("user=test@contoso.onmicrosoft.com^Aauth=Be arer EwBAAl3BAAUFFpUAo7J3Ve0bjLBWZWCclRC3EoAA^A^A")

friedt99 7. Sep 2023 09:55

AW: Indy und OAuth / Microsoft365
 
So Leute,

habe es hinbekommen (ChatGPT sei Dank). Es lag an der Tokenabfrage.
Mit ChatGPT habe ich folgende Funktion zur Tokenabfrage für eine "App-Registrierung" erstellt:

Delphi-Quellcode:

uses
  System.Net.HttpClient, System.Net.URLClient, System.SysUtils, System.JSON, System.Classes, System.NetEncoding;

function TForm1.GetAccessToken: string;
var
  HttpClient: THTTPClient;
  AccessTokenURL, ClientID, ClientSecret, Scope: string;
  Response: IHTTPResponse;
begin
  // Set your application-specific values
  AccessTokenURL := 'https://login.microsoftonline.com/<YOUR-TENANT-ID>/oauth2/v2.0/token';
  ClientID := '<ANWENDUNGS-ID>';
  ClientSecret := '<YOUR-CLIENT-SECRET>';
  Scope := 'https://outlook.office365.com/.default'; // Scope for Microsoft 365 API

  // Create and configure the HTTP client
  HttpClient := THTTPClient.Create;
  try
    // Prepare the request parameters
    HttpClient.ContentType := 'application/x-www-form-urlencoded';
    HttpClient.Accept := 'application/json';

    // Create the request body
    var RequestData := 'grant_type=client_credentials' +
      '&client_id=' + TNetEncoding.URL.Encode(ClientID) +
      '&client_secret=' + TNetEncoding.URL.Encode(ClientSecret) +
      '&scope=' + TNetEncoding.URL.Encode(Scope);

    // Send the POST request to obtain an access token
    Response := HttpClient.Post(AccessTokenURL, TStringStream.Create(RequestData));

    // Check for a successful response
    if Response.StatusCode = 200 then
    begin
      // Parse the JSON response to extract the access token
      // You may use a JSON library or implement your own parsing logic
      // Example: Extract the access token assuming JSON response format
      // You should add appropriate error handling and validation
      var AccessToken := ExtractAccessToken(Response.ContentAsString);

      // Return the access token
      Result := AccessToken;
    end
    else
    begin
      // Handle the case where the request fails (e.g., non-200 status code)
      // You should implement error handling according to your requirements
      Result := '';
    end;
  finally
    HttpClient.Free;
  end;
end;

// Implement a function to extract the access token from the JSON response
function TForm1.ExtractAccessToken(const JsonResponse: string): string;
var
  JsonValue: TJSONValue;
begin
  // Parse the JSON response to extract the access token
  // You may use a JSON library or implement your own parsing logic
  // Example (assuming JSON response format):
  // Search for the "access_token" key and extract its value
  // You should add appropriate error handling and validation
  // For demonstration purposes, we'll use Delphi's built-in JSON parser.

  JsonValue := TJSONObject.ParseJSONValue(JsonResponse);
  try
    if (JsonValue is TJSONObject) then
    begin
      Result := (JsonValue as TJSONObject).GetValue('access_token').Value;
    end
    else
    begin
      // Handle the case where JSON parsing fails
      Result := '';
    end;
  finally
    JsonValue.Free;
  end;
end;
Mit dem Token der damit zurück kommt, klappt der Mailversand mit dem Source oben im Thread.


Vielen Dank für Eure Hilfe.


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:04 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz