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?
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
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.
Nico Cizik
Digital Metaphors
http://www.digital-metaphors.com
Is this already available in the new version?
Nico Cizik
Digital Metaphors
http://www.digital-metaphors.com
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?
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.
Nico Cizik
Digital Metaphors
http://www.digital-metaphors.com
I load the reports dynamically from rtm files before printzing/sending. could this be the reason?
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?
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.
Nico Cizik
Digital Metaphors
http://www.digital-metaphors.com
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?
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).
Nico Cizik
Digital Metaphors
http://www.digital-metaphors.com
This means that there is a difference in the componenet properties in this app and in mine.
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.