How can I authenticate users to their own google drive account?

Viewed 285

I have a winforms application. This application basically for backup and restore. Users can be login to their own google drive accounts and use google drive for download and upload files. All users enable their own google drive API and use the credentials of that account. Is that a rigth way or not?(#1 question)

I am already using google drive api (v3) for this but the authentication method does not work as expected.

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        new ClientSecrets
                        {
                            ClientId = this.ClientId,
                            ClientSecret = this.ClientSecret
                        },
                        _scopes,
                        Environment.UserName,
                        CancellationToken.None,
                        new FileDataStore("Drive.Api.Auth.Store")).Result;

This code work in local and also work in production. But when I obfuscate(obfuscator: confuserex) the code, this method throws an error. Error;

One or many error occurred
System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)   location: System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)   location: System.Threading.Tasks.Task`1.get_Result()   location: App.Driver.GoogleDrive.DriveConnection.Authentication()

I wondered can I authenticate users with different way?(#2 question) Like, open a google drive login page and users can be login from it or something like that.

Note: At the first login with this code, the google drive login page was opened and I logged into my account and got permission for the google application. And then the web page turns to the api key. But this only happened for fist time. I don't have a good information about this. I followed this tutorial;

https://www.daimto.com/google-drive-authentication-c/

1 Answers
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        new ClientSecrets
                        {
                            ClientId = this.ClientId,
                            ClientSecret = this.ClientSecret
                        },
                        _scopes,
                        Environment.UserName,
                        CancellationToken.None,
                        new FileDataStore("Drive.Api.Auth.Store")).Result;

The There are two key points the the code above.

File datastore is used to store the user credentials after the user has authenticated your application the credentials are by default stored in %appData%. When a user is authenticated the credetials are stored for each user who logs in based up on Environment.UserName

When the application runs the first time the library will check %appData% for a credentials file for Environment.UserName. If it does not find one then the user will be presented with either web page opening up and the user requesting to consent to the application accessing their data. If the application detects that there is already a file in %appData% for a credentials file for Environment.UserName then no consent screen will be presented to the user.

It is important that you control who is running your application and change the field you have sent with Environment.UserName accordingly

If you are instreeted in how IDatastore works you can read my article about it Google .net – FileDatastore demystified

Related