1

I am using google external login in my application, and I need a functionality that user can upload images from the application to his google drive.

In my localhost code the google drive file uploading functionality working fine, after hosting it to IIS user unable to upload the image.

I have created an error log file. in that I found the below exception:

System.AggregateException: One or more errors occurred. ---> System.NotSupportedException: Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=347588538121-jeali0jufd389gqsi4ge22ent3939b4m.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A60931%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive" for authorization. See inner exception for details. ---> System.ComponentModel.Win32Exception: Access is denied

In the redirect_uri I found "localhost". but after hosting it should be www.abc.com, and the port number is also always varying here.

Tried in many ways from the stachoverflow and other websites. but did not find the solution.

Please help me to resolve this.

Thanks.

Tried with

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    obj,
    Scopes,
    "user",
    CancellationToken.None,
    new FileDataStore(AppDomain.CurrentDomain.BaseDirectory + "Daimto.GoogleDrive.Auth.Store")
).Result;

and also with:

using (var stream = new FileStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "credentials.json", FileMode.Open, FileAccess.Read))
{
    // The file token.json stores the user's access and refresh tokens, and is created
    // automatically when the authorization flow completes for the first time.
    string credPath = AppDomain.CurrentDomain.BaseDirectory + "token.json";
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        Scopes,
        "user",
        CancellationToken.None,
        new FileDataStore(credPath, true)).Result;
}
DaImTo
  • 88,623
  • 26
  • 153
  • 389
  • Possible duplicate of [Google Calendar Api working fine Locally but not raising its Authentication on Server](https://stackoverflow.com/questions/44842813/google-calendar-api-working-fine-locally-but-not-raising-its-authentication-on-s) – Andres Duarte Jul 04 '19 at 11:38
  • I have tried that one too, but the problem isn't resolved. – divya chinni Jul 04 '19 at 12:33
  • @divyachinni you didnt try that one your code says GoogleWebAuthorizationBroker.AuthorizeAsync no GoogleAuthorizationCodeFlow as i directed in the other question that was linked – DaImTo Jul 04 '19 at 12:46

1 Answers1

2

You are using GoogleWebAuthorizationBroker.AuthorizeAsync which is designed for use with installed applications. It spanws the web browser on the machine that it runs on. This will work fine when you are working locally but it will fail when you try to run it on a server as you dont have access to spawn a browser on the server (not that it would help as the user will not see it)

what you should be using is something like this

private static readonly IAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId = "PUT_CLIENT_ID_HERE",
                    ClientSecret = "PUT_CLIENT_SECRET_HERE"
                },
                Scopes = new[] { DriveService.Scope.Drive },
                DataStore = new FileDataStore("Drive.Api.Auth.Store")
            });

A full example can be found here Web applications (ASP.NET MVC)

DaImTo
  • 88,623
  • 26
  • 153
  • 389
  • 1
    Thanks for your guidance. Its working for me. Thanks a lot :) – divya chinni Jul 08 '19 at 14:17
  • I need this functionality in Xamarin forms also. we tried multiple samples in Xamarin, but no use. can You suggest us? – divya chinni Jul 12 '19 at 10:35
  • The client library dosent support authentication in xamarin you will have to code the authorization process yourself – DaImTo Jul 12 '19 at 14:25
  • how to do this authorization process? can i get any ideas? – divya chinni Jul 13 '19 at 05:57
  • These are the calls you will need to make https://www.daimto.com/google-3-legged-oauth2-flow/ – DaImTo Jul 13 '19 at 12:32
  • Can I save the files into drive without user authorization? – divya chinni Sep 03 '19 at 10:09
  • No you cant write a file to a users Google drive account without their permission. – DaImTo Sep 03 '19 at 10:21
  • ok, but in xamarin I am unable to implement the 3 legged auth2 flow. it is saying that "Google cant sign you in safely inside this app you can use google sign in by visiting this app's website in a browser like safari or chrome" 403 error. Ho to fix this – divya chinni Sep 04 '19 at 11:34
  • The google .net client library does not support Xamarin. Your going to have to code the flow manally on your own or check around see if anyone else has created it already – DaImTo Sep 04 '19 at 11:37
  • I don't find any useful info. tried to create flow manually also, but no luck – divya chinni Sep 04 '19 at 12:32
  • This will give you an idea of the flow you need. https://www.daimto.com/google-3-legged-oauth2-flow/ You will still need to open the browser window in the device browser. – DaImTo Sep 04 '19 at 12:57
  • I have already tried this, but not tried with the browser window in device browser. will check that – divya chinni Sep 04 '19 at 14:54