Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

asp.net mvc - Google Drive API upload Fails after hosting it to IIS. showing the error as Failed to launch the Browser with

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...