AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Netzwerke Indy und OAuth / Microsoft365
Thema durchsuchen
Ansicht
Themen-Optionen

Indy und OAuth / Microsoft365

Ein Thema von friedt99 · begonnen am 5. Sep 2023 · letzter Beitrag vom 7. Sep 2023
Antwort Antwort
friedt99

Registriert seit: 17. Mär 2010
46 Beiträge
 
#1

AW: Indy und OAuth / Microsoft365

  Alt 7. Sep 2023, 07:37
So, ich habe nochmal nachgesehen.

Es wird nur der tatsächliche Token verwendet, nicht das komplette JSON,
das war nur eine Logausgabe von dem Teil, der den Token abfragt.

Habe es auch mit der hier erwähnten Variante von TIdSASLXOAuth versucht.
Eine TIdSASLXOAuth2 ist im Fork von Remy Lebeau enthalten, diesen Fork habe ich verwendet.

Als Scope verwendet die Bibliothek (sgcWebSockets) 'https://graph.microsoft.com/.default'
(Token Abfrage für eine Service App).

Bekomme immer noch exakt den gleichen Fehler. Kann ich in Microsoft365 noch etwas
entscheidendes übersehen haben ?

Kann man in Microsft365 irgendwo fehlgeschlagene Anmeldeversuche anzeigen lassen ?
Habe jetzt unter https://compliance.microsoft.com/auditlogsearch die Überwachung aktiviert.
Mal schauen ob da dann etwas angezeigt wird.

Hat jemand einen Source zur Abfrage des Tokens mit "Bordmitteln" oder eine Empfehlung für
eine andere Bibliothek dazu mit der es schon mal funktioniert hat ? Eventuell stimmt ja was
bei der Tokenabfrage nicht.

Habe es mit https://github.com/geoffsmith82/GmailAuthSMTP versucht, da klappt aber die Tokenabfrage
schon nicht. Scheint als ob da die tenant-id nicht berücksichtigt wird.

Vielen Dank für die Antworten bisher.

Geändert von friedt99 ( 7. Sep 2023 um 08:38 Uhr)
  Mit Zitat antworten Zitat
Thomasl

Registriert seit: 19. Jun 2006
Ort: Vreden
67 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: Indy und OAuth / Microsoft365

  Alt 7. Sep 2023, 07:42
und mit Scopes:

Delphi-Quellcode:


https://outlook.office.com/SMTP.Send offline_access openid email profile

kann sein das das für dich zu viele sind, die verwende ich
Thomas Levering
  Mit Zitat antworten Zitat
Benutzerbild von Olli73
Olli73

Registriert seit: 25. Apr 2008
Ort: Neunkirchen
781 Beiträge
 
#3

AW: Indy und OAuth / Microsoft365

  Alt 7. Sep 2023, 08:47
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")
  Mit Zitat antworten Zitat
friedt99

Registriert seit: 17. Mär 2010
46 Beiträge
 
#4

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
Antwort Antwort

 

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 04:46 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz