1

I made a site that contains a function where it uploads files to the google drive, however I need to authenticate it every time I start a site session, so if I close the page and go back and I need to open the Authentication screen again to create another token for the new session. I Have this code:

public class Autorizacao : FlowMetadata
{
    private static readonly IAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = new ClientSecrets
            {
                ClientId = "...................",
                ClientSecret = ".................."
            },
            Scopes = new[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile },
            DataStore = new FileDataStore("C:\\Temp\\Autorização.Tokens")
        });
    public override string GetUserId(Controller controller)
    {

        var user = controller.Session["user"];
        if (user == null)
        {
            user = Guid.NewGuid();
            controller.Session["user"] = user;
        }
        return user.ToString();

    }

    public override IAuthorizationCodeFlow Flow
    {
        get { return flow; }
    }

I wanted to do something that does not need to be done doing these authentications every session started, does anyone have any suggestions?

And sorry for my bad english.

Brayan
  • 111
  • 3

1 Answers1

0

You may want to try Refreshing an access token.

Here are the options that you can do:

  • If you use a Google API Client Library, the client object refreshes the access token as needed as long as you configure that object for offline access.
  • If you are not using a client library, you need to set the access_type HTTP query parameter to offline when redirecting the user to Google's OAuth 2.0 server. In that case, Google's authorization server returns a refresh token when you exchange an authorization code for an access token. Then, if the access token expires (or at any other time), you can use a refresh token to obtain a new access token.

Refresh token will save the user's access token so you don't have to login when you start another session. User's session will only expire when logged out.

This related SO post might also help.

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22