Google drive OAuth2 without browser

SkyDancer

I am trying to authenticate my login via Google drive OAuth2 in my c# desktop app. And when the user enters Client ID and Client secret, the browser opens and asks for confirmation. Also browser opens when user enters a wrong Client ID and Client secret and shows an error message on the Google web page...

So, haw can I authenticate without browser confirmation and handle all google OAuth2 errors inside my code and without any browser pages?

Here is my authentication code:

//...code
string[] scopes = new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile };

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    new ClientSecrets
    {
        ClientId = "WrongID",
        ClientSecret = "WrongSecret"
    },
    scopes, _authUserName,
    CancellationToken.None, new FileDataStore(_authDataStorePath, true)).Result;
//...code

And when error occurs in this block, execution of code just stops and nothing happens.

UPDATE1:

OK, suppose that I know how to get the json file with tokens. Now, how can I make DriveService object with this file? Now I create it as follows:

DriveService _drive = new DriveService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential, // How to make this credential from tokens?
    ApplicationName = _applicationName
});

Also, how about catching Google OAuth exceptions in my c# code? Couse now the Google just show me an error info in browser and my app just hangs...

referscus

ClientId and Client Secret are used to Identify your app. These need to be hard coded or pulled from a database, not input by the user. As for no browser opening for OAuth2. You can't do that anymore with API V3, and Google has deprecated all previous versions.

So you have two choices with Google Drive if you don't want to open a browser for Auth. If you want everyone who uses the app to share 1 Google drive you can set up a service account https://developers.google.com/accounts/docs/OAuth2ServiceAccount.

If everyone who uses your app will install it on their device and use their own Google drive you can set it up as an installed app with Offline/long-lived access. Google doesn't document this very well, but if you search for it there are a few good examples. You'll open the browser once to get the refresh token for that user, then store that in a database and you'll be able to access their drive every time they get on without opening a browser.

These are the only ways to do it with no browser, Google doesn't allow you to pass them a username and password anymore.

Update--

You're going to need something like this, This is for offline but online will be similar.

string path = whereYouSaveClientSecret+ "client_secret.json";
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
        StoredResponse myStoredResponse = new StoredResponse(RefreshToken);
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
            new[] { DriveService.Scope.Drive},
            "user",
            CancellationToken.None,
            new SavedDataStore(myStoredResponse)).Result;
}
DriveService _drive = new DriveService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential, 
    ApplicationName = _applicationName
});

Here is Googles example https://developers.google.com/drive/web/examples/dotnet

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Access Google Drive without OAuth2

From Dev

How to Insert a Document to google drive using java without user interaction Oauth2

From Dev

Authenticating to Google Drive with Java and Oauth2

From Dev

Authorization in google with using OAuth 2 without browser

From Dev

Google Drive - Unable to download thumbnails with an authenticated (OAuth2) request

From Dev

Ruby google_drive gem oAuth2 saving

From Dev

Google Drive - Unable to download thumbnails with an authenticated (OAuth2) request

From Dev

Google Drive OAuth2 Authentification with visual basic .net

From Dev

Google drive API - Cannot read property 'OAuth2' of undefined

From Dev

How to use google drive API without web browser

From Dev

Omniauth and google oauth2 error without Devise - Rails 4

From Dev

Google OAuth2 callbacks calls me without parameters

From Dev

Oauth2 Google Drive offline access not working for non-google app files?

From Dev

Google API Google Drive Node.js can´t upload file oAuth2

From Dev

Access content of a google drive spreadsheet without oauth with rails

From Dev

Good tutorial on Google Drive SDK and OAuth 2?

From Dev

Good tutorial on Google Drive SDK and OAuth 2?

From Dev

Google OAuth2 api

From Dev

Google Contacts OAUTH2

From Dev

Google Drive - Access Drive without consent form (Username/password known)

From Dev

Sync SQLite Database to google drive without confirmation

From Dev

Google Drive API authentication without user interaction

From Dev

How to create folders in google drive without duplicate?

From Dev

Google Drive Api - Document without authorization

From Dev

Linkedin direct share without oauth2

From Dev

Oauth2 without MVC in webforms

From Dev

Tools for OAuth2 without client secret

From Dev

Google Drive API (java) - OAuth2 Get Request for mp4 Video Stream - HTML5 Video causes Status 403 (Forbidden)

From Dev

Access Google Drive API with refresh token, no browser, 401 error

Related Related

  1. 1

    Access Google Drive without OAuth2

  2. 2

    How to Insert a Document to google drive using java without user interaction Oauth2

  3. 3

    Authenticating to Google Drive with Java and Oauth2

  4. 4

    Authorization in google with using OAuth 2 without browser

  5. 5

    Google Drive - Unable to download thumbnails with an authenticated (OAuth2) request

  6. 6

    Ruby google_drive gem oAuth2 saving

  7. 7

    Google Drive - Unable to download thumbnails with an authenticated (OAuth2) request

  8. 8

    Google Drive OAuth2 Authentification with visual basic .net

  9. 9

    Google drive API - Cannot read property 'OAuth2' of undefined

  10. 10

    How to use google drive API without web browser

  11. 11

    Omniauth and google oauth2 error without Devise - Rails 4

  12. 12

    Google OAuth2 callbacks calls me without parameters

  13. 13

    Oauth2 Google Drive offline access not working for non-google app files?

  14. 14

    Google API Google Drive Node.js can´t upload file oAuth2

  15. 15

    Access content of a google drive spreadsheet without oauth with rails

  16. 16

    Good tutorial on Google Drive SDK and OAuth 2?

  17. 17

    Good tutorial on Google Drive SDK and OAuth 2?

  18. 18

    Google OAuth2 api

  19. 19

    Google Contacts OAUTH2

  20. 20

    Google Drive - Access Drive without consent form (Username/password known)

  21. 21

    Sync SQLite Database to google drive without confirmation

  22. 22

    Google Drive API authentication without user interaction

  23. 23

    How to create folders in google drive without duplicate?

  24. 24

    Google Drive Api - Document without authorization

  25. 25

    Linkedin direct share without oauth2

  26. 26

    Oauth2 without MVC in webforms

  27. 27

    Tools for OAuth2 without client secret

  28. 28

    Google Drive API (java) - OAuth2 Get Request for mp4 Video Stream - HTML5 Video causes Status 403 (Forbidden)

  29. 29

    Access Google Drive API with refresh token, no browser, 401 error

HotTag

Archive