- Регистрация
- 9 Май 2015
- Сообщения
- 1,562
- Баллы
- 155
Introduction
If youve ever worked with cloud authentication, you know how crucial a smooth access token flow is for keeping everything running seamlessly.
We in earlier this year and briefly showed how FetchAccessToken fits in. We created it to take the complexity out of authentication and make cloud integrations easier to manage. In this post, well take a closer look at what FetchAccessToken does, why its so helpful, and how it can make your integrations more robust!
What Is FetchAccessToken
Every component inheriting from TTMSFNCCloudOAuth gains a method:
function FetchAccessToken: IPromise<string>;
That promise does something smart: If the stored access token is still valid, it resolves immediately with it. If the token has expired, it runs the refresh flow and then resolves with the new token.
Note: If you'd like to learn more about promises in Delphi, please visit our .
Why Should You Use FetchAccessToken
If you call FetchAccessToken right before your cloud API request, it becomes the gateway to the latest valid token every time. Remember, it is a promise, which is why it handles so much of the heavy lifting for you!
Ensures consistent authentication state: When the token check happens right before the call, you always operate with the latest credentials, even after long workflows or user inactivity.
Avoids scattered refreshes: Tokens are checked or refreshed only when needed, keeping your logic clean and predictable.
Simpler error handling: Authentication issues are handled in line with your request logic, rather than through detached event handlers.
How to Use FetchAccessToken
There are two options to use FetchAccessToken with promises: chaining or await.
1. Chain with ThenBy
This is the idiomatic, safe way for asynchronous flows:
MyOAuthComponent.FetchAccessToken
.Op.ThenBy(function(const AToken: string): TVoid
begin
// At this point AToken is known to be valid
MyOAuthComponent.SomeServiceCall; // you dont need to pass the token explicitly
Result := Void;
end)
.Catch(function(const E: Exception): TVoid
begin
// Handle failure
HandleError(E);
end);
2. Await in Thread
Sometimes you may want to block until resolution:
TThread.CreateAnonymousThread(
procedure
begin
try
MyOAuthComponent.FetchAccessToken.Await;
MyOAuthComponent.SomeServiceCall;
except
on E: Exception do
HandleError(E);
end;
end).Start;
Calling Await on the main UI thread will block it from further execution.
Example
Below is an example of using FetchAccessToken in combination with TTMSFNCCloudGoogleGmail. In this scenario we'd like to make sure the access token did not expire before we attempt to refresh our mail list:
procedure TForm1.refreshMailsBtnClick(Sender: TObject);
begin
//First make sure we have the latest access token
TMSFNCCloudGoogleGmail1.FetchAccessToken.Op.ThenBy<TVoid>(function(const AResult: string): IPromise<TVoid>
begin
//Then retrieve the mails from the account
Result := TMSFNCCloudGoogleGmail1.GetMailsPromise;
end)
.Main.ThenBy(procedure (const AResult: TVoid)
var
I: Integer;
begin
//In the main thread, show the subjects
lbMails.Clear;
for I := 0 to TMSFNCCloudGoogleGmail1.Mails.Count - 1 do
begin
lbMails.Items.Add(TMSFNCCloudGoogleGmail1.Mails.Subject);
end;
end)
.Main.Catch(procedure (E: Exception)
begin
//Handle error
end);
end;
Conclusion
FetchAccessToken simplifies OAuth management across TMS FNC Cloud Pack components. It abstracts away token validity checks and refresh logic a single, reliable call! Whether you chain with promises or await in background threads, using FetchAccessToken keeps your cloud integrations robust, responsive, and clean.
If youve ever worked with cloud authentication, you know how crucial a smooth access token flow is for keeping everything running seamlessly.
We in earlier this year and briefly showed how FetchAccessToken fits in. We created it to take the complexity out of authentication and make cloud integrations easier to manage. In this post, well take a closer look at what FetchAccessToken does, why its so helpful, and how it can make your integrations more robust!
What Is FetchAccessToken
Every component inheriting from TTMSFNCCloudOAuth gains a method:
function FetchAccessToken: IPromise<string>;
That promise does something smart: If the stored access token is still valid, it resolves immediately with it. If the token has expired, it runs the refresh flow and then resolves with the new token.
Note: If you'd like to learn more about promises in Delphi, please visit our .
Why Should You Use FetchAccessToken
If you call FetchAccessToken right before your cloud API request, it becomes the gateway to the latest valid token every time. Remember, it is a promise, which is why it handles so much of the heavy lifting for you!
How to Use FetchAccessToken
There are two options to use FetchAccessToken with promises: chaining or await.
1. Chain with ThenBy
This is the idiomatic, safe way for asynchronous flows:
MyOAuthComponent.FetchAccessToken
.Op.ThenBy(function(const AToken: string): TVoid
begin
// At this point AToken is known to be valid
MyOAuthComponent.SomeServiceCall; // you dont need to pass the token explicitly
Result := Void;
end)
.Catch(function(const E: Exception): TVoid
begin
// Handle failure
HandleError(E);
end);
2. Await in Thread
Sometimes you may want to block until resolution:
TThread.CreateAnonymousThread(
procedure
begin
try
MyOAuthComponent.FetchAccessToken.Await;
MyOAuthComponent.SomeServiceCall;
except
on E: Exception do
HandleError(E);
end;
end).Start;
Example
Below is an example of using FetchAccessToken in combination with TTMSFNCCloudGoogleGmail. In this scenario we'd like to make sure the access token did not expire before we attempt to refresh our mail list:
procedure TForm1.refreshMailsBtnClick(Sender: TObject);
begin
//First make sure we have the latest access token
TMSFNCCloudGoogleGmail1.FetchAccessToken.Op.ThenBy<TVoid>(function(const AResult: string): IPromise<TVoid>
begin
//Then retrieve the mails from the account
Result := TMSFNCCloudGoogleGmail1.GetMailsPromise;
end)
.Main.ThenBy(procedure (const AResult: TVoid)
var
I: Integer;
begin
//In the main thread, show the subjects
lbMails.Clear;
for I := 0 to TMSFNCCloudGoogleGmail1.Mails.Count - 1 do
begin
lbMails.Items.Add(TMSFNCCloudGoogleGmail1.Mails.Subject);
end;
end)
.Main.Catch(procedure (E: Exception)
begin
//Handle error
end);
end;
Conclusion
FetchAccessToken simplifies OAuth management across TMS FNC Cloud Pack components. It abstracts away token validity checks and refresh logic a single, reliable call! Whether you chain with promises or await in background threads, using FetchAccessToken keeps your cloud integrations robust, responsive, and clean.
Источник: