IndySASLOAuth2
IndySASLOAuth2 copied to clipboard
Get New token from RefreshToken
Hi Asbjørn,
I've add a function that I get a new token with the refreshtoken.
procedure TSASLXOAuth2.GetNewTokenFromRefreshToken;
var
LClient: TRestClient;
LRequest: TRESTRequest;
LToken: string;
LIntValue: int64;
begin
if OAuth2Authenticator.RefreshToken.IsEmpty then
raise EOAuth2Exception.Create('Empty RefreshToken');
LClient := TRestClient.Create(OAuth2Authenticator.AccessTokenEndpoint);
try
LRequest := TRESTRequest.Create(LClient);
LRequest.Method := TRESTRequestMethod.rmPOST;
LRequest.AddAuthParameter('client_id', OAuth2Authenticator.ClientID, TRESTRequestParameterKind.pkGETorPOST);
LRequest.AddAuthParameter('client_secret', OAuth2Authenticator.ClientSecret, TRESTRequestParameterKind.pkGETorPOST);
LRequest.AddAuthParameter('refresh_token', OAuth2Authenticator.RefreshToken, TRESTRequestParameterKind.pkGETorPOST);
LRequest.AddAuthParameter('redirect_uri', OAuth2Authenticator.RedirectionEndpoint, TRESTRequestParameterKind.pkGETorPOST);
LRequest.AddAuthParameter('grant_type', 'refresh_token', TRESTRequestParameterKind.pkGETorPOST);
LRequest.Execute;
if LRequest.Response.GetSimpleValue('access_token', LToken) then
OAuth2Authenticator.AccessToken := LToken;
if LRequest.Response.GetSimpleValue('refresh_token', LToken) then
OAuth2Authenticator.RefreshToken := LToken;
if LRequest.Response.GetSimpleValue('token_type', LToken) then
OAuth2Authenticator.TokenType := OAuth2TokenTypeFromString(LToken);
// if provided by the service, the field "expires_in" contains
// the number of seconds an access-token will be valid
if LRequest.Response.GetSimpleValue('expires_in', LToken) then
begin
LIntValue := StrToIntdef(LToken, -1);
if (LIntValue > -1) then
OAuth2Authenticator.AccessTokenExpiry := IncSecond(Now, LIntValue)
else
OAuth2Authenticator.AccessTokenExpiry := 0.0;
end;
finally
LClient.DisposeOf;
end;
That was helpful, thank you!