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
Seite 2 von 2     12   
Benutzerbild von Olli73
Olli73

Registriert seit: 25. Apr 2008
Ort: Neunkirchen
662 Beiträge
 
#11

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
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
Antwort Antwort
Seite 2 von 2     12   

 

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 01:26 Uhr.
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