Einzelnen Beitrag anzeigen

friedt99

Registriert seit: 17. Mär 2010
45 Beiträge
 
#12

AW: Indy und OAuth / Microsoft365

  Alt 7. Sep 2023, 09:55
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.

Geändert von friedt99 ( 7. Sep 2023 um 09:59 Uhr)
  Mit Zitat antworten Zitat