Home General
New Blog Posts: Merging Reports - Part 1 and Part 2

Save GMail authorization data

Hello!

I use GMail plugin for sending the mail from my app. Upon first sending the user must authorize on Gmail (in a separate browser window). After that sending e-mail is without authentication.

But if the user closes and reopens the application, then he must authorize again to use Gmail.

Is possible to save somewhere the authorization data so there will be no need to reauthorize after next application execution?

Comments

  • Generally the access token is valid for 1 hour. ReportBuilder stores this token in memory so it is lost once the application is closed. It is on our to-do list to add a feature automatically saving the access token to disk once it is obtained. This, along with "Refresh Token" support will make the re-authorization process much easier.

    Currently you will need to retrieve the access token from the SMTP object once authorization has occurred, save it for further use, then reload it manually. The valid access token is stored in the SMTP.Authenticator object.
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • Hello!

    Is this already available in the new version?
  • Yes, refresh token support has been added for RB 22.
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • Hi!

    I have a question about the refresh token. I have both properties set to true (EmailSettings/ConnectionSettings/WebMail/GMailSettings/OAuth2), but after I leave the application and re-run it, GMail again request the login.

    Is there any additional code that I need to add for saving the tokens permanently? Or does RB save it automatically to an INI file or something like this?
  • Hi Marko,

    These properties are published so they are saved with the report definition (either in the .dfm or report template). Note that Access Token and Refresh Token storage are enabled by default.
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • Where do the tokens get stored after the authorization?
  • edited February 6
    Afrer the upgrade to RB 22 I don't see any difference. After the application is restarted, sending again requires the authorization.. I don't understand...

    I load the reports dynamically from rtm files before printzing/sending. could this be the reason?
  • edited February 7
    if the data is stored in the template file (RTM file, right?), then I must save the template after each sending?

    If I would go the manual way, then I would kindly ask if the following code is OK or for a code sample how to save and load the token from a string, I will provide the token before sending and save it after.

    To save:
    MySettingAccessToken := TppRESTMailGmail(Engine.Email).Authenticator.AccessToken;
    MySettingRefreshToken := TppRESTMailGmail(Engine.Email).Authenticator.RefreshToken;

    To load:
    TppRESTMailGmail(Engine.Email).Authenticator.AccessToken := MySettingAccessToken;
    TppRESTMailGmail(Engine.Email).Authenticator.RefreshToken := MySettingRefreshToken;

    The problem is loading, because it gives me access violation, probably the Authenticator is still nil. But when I can assign the token to the authenticator?

  • I suggest to update the RB wiki with this :)
  • Hi Marko,

    The data is not stored in the template.

    Storing the Auth tokens in the template would not be ideal for this feature. This is why the data is stored either inside the RBuilder.ini file or in the registry. If the OAuth2Settings.AuthStorage property is set properly, the Auth Token and Refresh Token are saved each time the user authorizes.

    If you are loading templates, it's possible the AuthStorage property is being overwritten and the tokens are not being properly saved. You may need to load and re-save each template with the properties set or manually set them in code after the template has loaded.
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • I apologize, but I need some more help here. I just can't make it work to remember the tokens. What I've done until now:

    1. When starting the application, I set the INI save position (in FormCreate)
    rptDesigner.IniStorageName := SysFolders.AppData+'RBuilder.ini';

    2. Before mailing the report I set the tokens save flags (in BeforeMail event)
    rptMain.EmailSettings.ConnectionSettings.WebMail.GmailSettings.OAuth2.AuthStorage := [oasAccessToken, oasRefreshToken];

    Is there anything more I need to do? Or I'm doing it wrong?
  • edited February 12
    Hi Marko,

    Let's get everything working with a simple, minimal application first before moving to your main application.

    Create a new Delphi application, add a Report and a Button and add similar code to its OnClick event. (Adding your ClientID, Secret and email).
    uses
    ppRESTMailGmail, ppTypes;

    procedure TForm2.Button1Click(Sender: TObject);
    begin
    ppReport1.EmailSettings.Enabled := True;
    ppReport1.EmailSettings.PreviewInEmailClient := False;

    ppReport1.EmailSettings.Subject := 'Test';
    ppReport1.EmailSettings.Body.Add('Test');
    ppReport1.EmailSettings.Recipients.Add('MyEmail@MyServer.com');

    ppReport1.EmailSettings.ConnectionSettings.WebMailSetup(ctGmail, 'ClientID', 'Client Secret');

    ppReport1.Print;

    end;
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • It works. After first sending I coles the app and reopen it - if I send the report again, there is no more authorization page and the mail is immediately sent.

    This means that there is a difference in the componenet properties in this app and in mine.
  • Hello!

    Justo to notifiy you that I solved the problem. I've compared the ppReport component in both applications and changed the differences directly in the DFM file. The soultio worked and the application now remembers the login data. If I encounter any additional problerms I will open a new topic.
Sign In or Register to comment.