GMail: TestTockens/RefreshAccess always fail

Hello;

The GMailDemo saves the Token on ini file but if I close the demo and then I run it again, TestTockens-RefreshAccess always fail and it has to go to DoAuth again.

The same is what I get in my own project. But, in the same project I have a similar situation using TAdvGCalendar and with this component the TestTockens-RefreshAccess works very well.

It has to be some problem with TAdvGMail.

Thank you.

Hi,

I have not been able to reproduce this issue in the GMailDemo.

Can you please make sure the ini file is saved correctly? Is there a value assigned for the "ACCESS_TOKEN" and "REFRESH_TOKEN" fields in the ini file?

If the values are not assigned, can you please make sure SaveTokens is called after the OnReceivedAccessToken or OnConnected event is triggered?

Hi;
The demos is saving the values on the ini file very well. Please note the changes I did in your original demo project (left side is mine):

Hi,
I also tried to make it more simple in this way:

procedure TForm1.AdvGMail1ReceivedAccessToken(Sender: TObject);
begin
FWrite('c:\test\token', AdvGMail1.TokensAsString);
....

procedure TForm1.Button1Click(Sender: TObject);
var
Acc: Boolean;
begin
AdvGMail1.App.Key := GAppkey;
AdvGMail1.App.Secret := GAppSecret;
if AdvGMail1.App.Key <> '' then
begin
AdvGMail1.ClearTokens;
AdvGMail1.TokensAsString := FRead('c:\test\token');
Acc := AdvGMail1.TestTokens;
Connected := Acc;
if not Acc then
begin
AdvGMail1.RefreshAccess;
Acc := AdvGMail1.TestTokens;
if not Acc then
AdvGMail1.DoAuth

This is the same way I use for TAdvCalendar and it works. But no does not work with TAdvGmail, it always go to DoAuth.

Thank you.

Can you please make sure you are using the latest version of TMS VCL Cloud Pack?

If the problem persists, please try one of the following:

  • Remove the ClearTokens call from your code.

  • Enable logging to find out what the response data of the TestTokens call is.
    Logging can be enabled by setting Logging to True and LogLevel to llDetail. The log file is created automatically in the Windows Documents folder.

  • Use the Connect call in combination with the OnConnected event instead of a combination of TestTokens/RefreshAccess/DoAuth and OnReceivedAcessToken.

Hello;

Oh the attached you can see my actual TMS files to know the version I am using.

This is what I get on the log file:

20220427T093936:HTTPS GET: https://www.googleapis.com/gmail/v1/users/me/labels?key=594725854454-72as614t7pc5susg8u6874ho8je69hh0.apps.googleusercontent.com
20220427T093936:HTTPS GET RESULT:{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"errors": [
{
"message": "Invalid Credentials",
"domain": "global",
"reason": "authError",
"location": "Authorization",
"locationType": "header"
}
],
"status": "UNAUTHENTICATED"
}
}

20220427T093936:HTTPS POST: accounts.google.com/o/oauth2/token
20220427T093936:HTTPS POST RES:200
20220427T093936:HTTPS POST RESULT:{
"access_token": "ya29.A0ARrdaM8IQY2pAfJBHtNeLEFimLW-dG4x71jsD5451MORdcheZUlkVzSZX2_sSTEXh7W2rHQjxk-kN4XjoIndcEQduEBqGXiYHlSZ1p0tDYPHDfegpohtBNppAqT3Q6AG11oSySmMVYSqb_SCCIlEjx06txa_BQ",
"expires_in": 3599,
"scope": "https://www.googleapis.com/auth/gmail.send",
"token_type": "Bearer"
}
20220427T093936:HTTPS GET: https://www.googleapis.com/gmail/v1/users/me/labels?key=594725854454-72as614t7pc5susg8u6874ho8je69hh0.apps.googleusercontent.com
20220427T093937:HTTPS GET RESULT:{
"error": {
"code": 403,
"message": "Request had insufficient authentication scopes.",
"errors": [
{
"message": "Insufficient Permission",
"domain": "global",
"reason": "insufficientPermissions"
}
],
"status": "PERMISSION_DENIED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT",
"domain": "googleapis.com",
"metadata": {
"service": "gmail.googleapis.com",
"method": "caribou.api.proto.MailboxService.ListLabels"
}
}
]
}
}
tms.txt (79.0 KB)

Hi,

Please note that, with your demo, after I "connect", I can send the mail, and it works. The problem if that, TestTokens and RefreshAccess always are returning false.

I cannot find why.

The log indicates insufficient authentication scopes for the request to retrieve labels.
This request is used to determine if the tokens are valid during the TestTokens call.

Can you please try if adding the following scopes resolves the issue?

https://www.googleapis.com/auth/gmail.labels

Hello;

I added the scope and now it is working. Thank you for your support.

Happy to help!

We'll have to investigate if TestTokens for TAdvGMail can be adapted to not require this scope in a future version.