在访问Google Drive Api时部署后拒绝访问

时间:2018-03-02 12:11:30

标签: c# oauth asp.net-mvc-5 google-api google-oauth

使用Google OAuth开发MVC应用程序以访问驱动器API。我在开发环境中访问Google API时效果很好。但是,当我在IIS或共享主机中部署它时,它显示访问被拒绝,具有以下异常。

[Win32Exception (0x80004005): Access is denied]


 System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) +602
   System.Diagnostics.Process.Start(ProcessStartInfo startInfo) +60
   Google.Apis.Auth.OAuth2.<ReceiveCodeAsync>d__14.MoveNext() +265

[NotSupportedException: Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=1062976495544-dg9ak1e6uovp7p6gmlf1vcqvvq6mnefv.apps.googleusercontent.com&redirect_uri=http:%2F%2Flocalhost:7862%2Fauthorize%2F&scope=https:%2F%2Fwww.googleapis.com%2Fauth%2Fdrive" for authorization. See inner exception for details.]
   Google.Apis.Auth.OAuth2.<ReceiveCodeAsync>d__14.MoveNext() +1029
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
   Google.Apis.Auth.OAuth2.<AuthorizeAsync>d__8.MoveNext() +730
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
   Google.Apis.Auth.OAuth2.<AuthorizeAsync>d__4.MoveNext() +571
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
   Google.Apis.Auth.OAuth2.<AuthorizeAsync>d__1.MoveNext() +438

我尝试过的事情:

  1. 在google dev控制台中验证域。
  2. 将协议从HTTP更改为 HTTPS

1 个答案:

答案 0 :(得分:1)

本文可帮助我解决此问题: Implementing OAuth2.0 Authorization For Google In ASP.NET

工作原理(按文章):

    1. 创建Web服务器client_secret.json。
    1. GetAuthorizationUrl()-为获取代币临时令牌创建URL。
    1. 重定向到GoogleCallback()并使用ExchangeAuthorizationCode()获取刷新和访问令牌
    1. 将它们保存到文件"~/Resources/driveApiCredentials/drive-credentials.json/Google.Apis.Auth.OAuth2.Responses.TokenResponse-{account}"
    1. 使用此保存的令牌。

之后,只需对驱动器使用google身份验证:

     // Client for requests
    private DriveService client;

    // Class constructor
    public GoogleDrive(string account)
    {
        UserCredential credential;
        Login = account;

        // Load Web Secret file
        using (var stream =
            new FileStream(System.Web.HttpContext.Current.Server.MapPath($"~/Resources/client_secret_{account}.json"), FileMode.Open, FileAccess.Read))
        {
            // Path to token folder 
            string credPath = System.Web.HttpContext.Current.Server.MapPath(Path.Combine("~/Resources/driveApiCredentials", "drive-credentials.json"));

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                account,
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            //Console.WriteLine("Credential file saved to folder: " + credPath);
        }

        // Create Drive API service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = account,
        });

        client =  service;
    }

此代码使用之前加载的令牌进行初始化DriveService

相关问题