Jump to content

marcin

Members
  • Content Count

    13
  • Joined

  • Last visited

Posts posted by marcin


  1. Remy

    I'm obtaining the access_token the way you helped me in another thread:

     

      scope := 'https://graph.microsoft.com/.default';
      msUrl := 'https://login.microsoftonline.com/%s/oauth2/v2.0/token';
    
      
      sslHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
      sslHandler.SSLOptions.Method := sslvTLSv1_2;
      sslHandler.SSLOptions.SSLVersions := [sslvTLSv1_2];
    
      httpClient := TIdHTTP.Create;
      httpClient.HTTPOptions := [hoForceEncodeParams, hoNoProtocolErrorException, hoWantProtocolErrorContent];
      httpClient.IOHandler := sslHandler;
      httpClient.Request.ContentType := 'application/x-www-form-urlencoded';
    
      tsRequestBody := TStringList.Create;
      tsRequestBody.Add('grant_type=client_credentials');
      tsRequestBody.Add('client_id=' + ClientID);
      tsRequestBody.Add('client_secret=' + ClientSecret);
      tsRequestBody.Add('scope=' + scope);
    
      sResponse := httpClient.Post(Format(msUrl, [TenantID]), tsRequestBody);
      responseCode := httpClient.ResponseCode;
    
      if responseCode = 200
       then begin
             jsonOject := TJSONObject.ParseJSONValue(sResponse) as TJSONObject;
             jsonValue := jsonOject.Values['access_token'];
             accessToken := jsonValue.ToString;
             Result := True;
            end
       else begin
             Result := False;
             accessToken := sResponse;
            end;

    TIA

    Marcin


  2. Hi

     

    Since Office 365 account I'm trying to connect to with TIdIMAP4  does not support App-specific password, I have to use TIdSASLXOAuth2 from ssl-oauth branch.

    I have managed to obtain access token from login.microsoftonline.com, but I'm getting 'Authentication failed' when trying to log in to IMAP. How can I check (if possible) if there is something wrong with the token (i.e incorrect scope) or TIdSASLXOAuth2 itself requires some tweaking? Or maybe there is something wrong with my code:

     

    
     sslHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
     sslHandler.SSLOptions.Method := sslvTLSv1_2;
     sslHandler.SSLOptions.SSLVersions := [sslvTLSv1_2];
    
     IMAP := TIdIMAP4.Create(Self);
     IMAP.IOHandler := sslHandler;
     IMAP.UseTLS := utUseImplicitTLS;
     IMAP.Host := 'outlook.office365.com';
     IMAP.Port := 993;
     IMAP.OnStatus := IdIMAP41Status;
    
     xoauthSASL := IMAP.SASLMechanisms.Add;
     xoauthSASL.SASL := TIdSASLXOAuth2.Create(nil);
     
     TIdSASLXOAuth2(xoauthSASL.SASL).UserPassProvider := TIdUserPassProvider.Create();
     TIdSASLXOAuth2(xoauthSASL.SASL).UserPassProvider.Username := microsoft_clientaccount;
     TIdSASLXOAuth2(xoauthSASL.SASL).UserPassProvider.Password := AccessToken;
    
     IMAP.AuthType := iatSASL;
    
     IMAP.Connect(True);

    Can someone help?

    TIA

    Marcin

     


  3. 1 hour ago, omnibrain said:

    Can you show a bit more code? How do you setup the stream? Perhaps you need to (re)set the position of the stream to 0 before sending.

    Last version is:

     

     StringWriter := TStringWriter.Create();
     Writer := TJsonTextWriter.Create(StringWriter);
     Writer.Formatting := TJsonFormatting.Indented;
     Writer.WriteStartObject;
     Writer.WritePropertyName('grant_type');
     Writer.WriteValue('client_credentials');
     Writer.WritePropertyName('client_id');
     Writer.WriteValue(ClientID);
     Writer.WritePropertyName('client_secret');
     Writer.WriteValue(ClientSecret);
     Writer.WritePropertyName('scope');
     Writer.WriteValue('https://graph.microsoft.com/.default');
     Writer.WriteEndObject;
     requestBody := TStringStream.Create(StringWriter.ToString, TEncoding.UTF8);
     requestBody.Position := 0;

    and presented JSON comes from:

     

    TStringStream(RequestBody).DataString

     


  4. Hi Guys

     

    I need a little help here

     

    I'm trying to send following JSON to https://login.microsoftonline.com/

     

    {
        "grant_type": "client_credentials",
        "client_id": "xxx",
        "client_secret": "_xxx_",
        "scope": "https://graph.microsoft.com/.default"
    }

     

    using following code:


     

    IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
    url := 'https://login.microsoftonline.com/' + TenantID + '/oauth2/v2.0/token';
    FLastResult := IdHTTP.Post (url, requestBody);
    

    where requestBody is  a TStream containing the string with the JSON.

     

    I got "400" error response from microsoft saying that "The request body must contain the following parameter: 'grant_type'"

    Apparently I'm missing something here, since I've tested this with Postman I there I get proper response containing requested token.

     

    Any clues?

     

    TIA

    Marcin

     

     


  5. Hi

     

    Is Indy IMAP component capable to connect to Office 365 account? How "mature" is Indy ssl-oauth branch?

    I'm looking for some solution to only read 'unread' emails from this account, mark them as 'read' and optionally copy them to another folder

     

    TIA

    Marcin


  6. @Attila Kovacs

    Thanks for sharing the unit - it works in environments where other solutions have failed.

    I'd like to ask if it possible to prepare body of the message as html. I've read the unit couple of times, but I  have not found the way.

     

    TIA

    Marcin


  7. Hi

    Is there a place where latest version of the unit is available?

     

    I'm facing similar issues on newer windows 10 machines where attemp of sending a message via MAPI return MAPI error. Old machines with updated Win10 work perfectly.

     

    Thanks in advance

     

    Merry Christmas / Happy Holidays 

    M.

     


  8. Hi Guys

     

    I have serious problem with my FMX application compiled with Delphi Rio and run on Android 5.1.

    The case is that when I place TEdit and TStringGrid on the same form - application freezes. When both are on the main form, application even won't start.

    There is no problem if I compile same sources with Delphi Tokyo.

     

    Has anybody experienced something like this?

    I'd appreciate any possible solutions other that getting rid of the grid (than it works)

     

    TIA

    Marcin

×