REFRESHTOKENS DOES NOT WORK

This is bizarre.

The savetokens method did not save the refresh token into the INI file if this entry was already in the INI file:

REFRESHTOKEN=

ie. REFERSHTOKEN= with nothing following the =

This entry was put into the INI file by TADVDROPBOX component. So I frigged around for a couple of days trying to REFERSHTOKENS to work and could not. I traced the code to CloudCustomeDropbox.pas and put in a breakpoint to see that Token_Refresh being passed in was blank. So I went to the INI file and suer enough, the REFRESH_TOKEN was blank. Long story short, I cleared out all of the TOKEN section and ran my code. This time the INI file was updated with this:

REFRESH_TOKEN=ypIPHO2+isPKY/L1kAdic5nibADFeRZCVLrB5waK/a/0R4XUftazaImkOp8NAeyzYIG7Xiv22hGdUWfKdYUrIA

And I have now tested REFERSHTOKENS which returns TRUE.

So are you purposefully not updating the REFRESH_TOKEN= entry in the INI file if there is ANY OCCURRENCE OF REFRESH_TOKEN= in the INI file. In your SaveTokens code you only don't enter the function if the Key or Section is blank and that's not the case.

Just trying to figure out what is going on????

As you can see in CloudBase.pas in SaveTokens, the Token_Refresh is persists as-is in the INI file.
If Token_Refresh is blank, I can only think that there was NOT a successful authentication/authorization cycle BEFORE SaveTokens was called?

Look, so far this process is 100% repeatable.

If I have NOTHING in the INI file WRT REFRESH_TOKEN, it works as expected:

INIFILE Section, note REFRESH_TOKEN is NOT in there:

[EMPSecure]
ACCESS_TOKEN=0pfLjfDTbkMKEfa7oXOIz2V2I3UwpFXsxrz6jnkJUQi3ZDLkgIPafMz1AYCDLPyD2grCCr1T9m4YzRJIQrvIdB8APuQkW+mX5c471SSfWGQSjyj93fexZqlfVAU4ldu3TUKpQN6HGxXDLIYre1DJJXv/q2EFUmhq9HDbz0dMbhLAnquychkOrOJmlJ5oZvH
AUTH_TOKEN=j1qw+p9S7zf3d2h7kg9sZMBc0gN4OtRXHZBw3igMeyqQI+SnM9vp6VRxkA
ACCESS_TOKEN_SECRET=
AUTH_TOKEN_SECRET=
EXTRA_DATA=

Note there is NO ENTRY for REFRESHTOKEN.

I run this code:

  TMSDropbox.LoadTokens;
  showmessage('tokens loaded');
  if TMSDropbox.RefreshAccess then
  begin
    ShowMessage('tokens refreshed');
    Result := True;
  end
  else
  begin
    ShowMessage('refresh failed');
    Result := False;
  end;
  Exit;

And I get the message 'tokens refreshed' with the INIFILE looking like THIS:

[EMPSecure]
ACCESS_TOKEN=0pfLjfjTa3nGKeffgzOZKuKsw4DNjucPNsN+se/bGc9jzG765HPwoYaP2vEMKDBdtZSA1nbSqkBzmf9Qz+q7n66FZgEuc9g5WC5+uR5PXWHHDZfyX7rdKEmYHAWCdIYKEZvyxXl3lIUjTMkG+uD0nB5ac/biwScZ/LkiVuEBgin2ddN8zMdJESOzBOUJ6VK
AUTH_TOKEN=j1qw+p9S7zf3d2h7kg9sZMBc0gN4kU1wjw/7pw7ItLfb7qLHhC/1MnUXPC
ACCESS_TOKEN_SECRET=
AUTH_TOKEN_SECRET=
EXTRA_DATA=
REFRESH_TOKEN=P5qUUi0CB3a+N1xf+/xwi+5hx3bUfwdwbCxNSPAqDLpmlHZ/nEmjMfVmV75HcUxFYntbqEXz0UFt7EFQxCVwEA

If, however I start with the INI File like this (note the REFRESH_TOKEN=blank) I get the message 'refresh failed' and the INI file is unchanged:

[EMPSecure]
ACCESS_TOKEN=0pfLjfjTa3nGKeffgzOZKuKsw4DNjucPNsN+se/bGc9jzG765HPwoYaP2vEMKDBdtZSA1nbSqkBzmf9Qz+q7n66FZgEuc9g5WC5+uR5PXWHHDZfyX7rdKEmYHAWCdIYKEZvyxXl3lIUjTMkG+uD0nB5ac/biwScZ/LkiVuEBgin2ddN8zMdJESOzBOUJ6VK
AUTH_TOKEN=j1qw+p9S7zf3d2h7kg9sZMBc0gN4kU1wjw/7pw7ItLfb7qLHhC/1MnUXPC
ACCESS_TOKEN_SECRET=
AUTH_TOKEN_SECRET=
REFRESH_TOKEN= <<<<<------THIS DOES NOT GET UPDATED
EXTRA_DATA=

So if the INI file does not contain a reference to REFRESH_TOKEN the code will update it with a refresh token. If it has REFRESH_TOKEN=blank, the refresh token is not updated. For completeness here are the other callbacks:


class procedure TDBEventHandlers.TMSDropboxAccessDenied(Sender: TObject);
begin
  WriteLog('DEBUG', 'Enter TMSDropboxAccessDenied');
  try
    try
      WriteLog('ERROR', 'Dropbox authentication failed, Access Denied');

      // Authentication failed
      Opened := False;
      AuthToken := '';
      AccessToken := '';
    except
      on E: Exception do
      begin
        WriteLog('ERROR', 'TMSDropboxAccessDenied error: ' + E.Message);
      end;
    end;
  finally
    WriteLog('DEBUG', 'Exit TMSDropboxAccessDenied');
  end;
end;

class procedure TDBEventHandlers.TMSDropboxConnected(Sender: TObject);
var
  cs: TCloudStorage;

begin
  WriteLog('DEBUG', 'Enter TMSDropboxConnected');
  try
    try
      // Need to save tokens
      Opened := True;

      if (Sender is TCloudStorage) then
      begin
        cs := Sender as TCloudStorage;
        AuthToken := cs.Token_Auth;
        AccessToken := cs.Token_Access;
        WriteLog('DEBUG', 'Auth Token: ' + AuthToken);
        WriteLog('DEBUG', 'Access Token: ' + AccessToken);
        // Save tokens Regardless
        cs.SaveTokens;
      end
      else
        raise Exception.Create('Invalid object passed to TMSDropboxConnected');
    except
      on E: Exception do
      begin
        WriteLog('ERROR', 'Problem with Dropbox OnConnection: ' + E.Message);
      end;
    end;
  finally
    WriteLog('DEBUG', 'Exit TMSDropboxConnected');
  end;
end;

class procedure TDBEventHandlers.TMSDropboxReceivedAccessToken(Sender: TObject);
var
  cs: TCloudStorage;

begin
  WriteLog('DEBUG', 'Enter TMSDropboxReceivedAccessToken');
  try
    try
      // Need to save tokens
      Opened := True;

      if (Sender is TCloudStorage) then
      begin
        cs := Sender as TCloudStorage;
        AuthToken := cs.Token_Auth;
        AccessToken := cs.Token_Access;
        WriteLog('DEBUG', 'Auth Token: ' + AuthToken);
        WriteLog('DEBUG', 'Access Token: ' + AccessToken);
        // Save tokens Regardless
        cs.SaveTokens;
      end
      else
        raise Exception.Create('Invalid object passed to TMSDropboxReceivedAccessToken');
    except
      on E: Exception do
      begin
        WriteLog('ERROR', 'Problem recieving Access Token: ' + E.Message);
      end;
    end;
  finally
    WriteLog('DEBUG', 'Exit TMSDropboxReceivedAccessToken');
  end;

end;

A refresh token is what is used to generate A NEW ACCESS token.
So, when there is blank (empty string) refresh token, a RefreshAccess will obviously fail.
As I explained, you need to verify that after a successful authentication/authorization, it provides the refresh token for future renewals of both access & refresh token.
If RefreshAccess failed, you should do a new Auth cycle.

The problem is/was if I do NOT have a valid REFRESH_TOKEN, but do have a valid TOKEN_ACCESS and do a doConnect, ie TMSDropbox.Connect this succeeds, but does NOT generate a REFRESH_TOKEN.

If I do not have a valid TOKEN_ACCESS and do a TMSDropbox.Connect then this does generate a REFRESH_TOKEN.

This is where I was getting into trouble, ie. the INI file has a valid TOKEN_ACCESS, but REFRESH_TOKEN=blank does not generate a REFRESH_TOKEN when signing in with a TMSDropbox.Connect.

I assume this is expected behaviour.

Thanks for the clarification.

Kevin