托管到IIS后无法启动浏览器访问被拒绝

时间:2018-03-03 10:10:26

标签: wcf

我正在使用Google.net客户端库对Google API进行身份验证,此代码可在本地运行,但当我将其上传到我的服务器时出现此错误

  

“无法使用\”https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=123&redirect_uri=http%3A%2F%2Flocalhost%3A54597%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive \“启动浏览器以进行授权。有关详细信息,请参阅内部异常。”

我的代码

Google.Apis.Drive.v3.DriveService service = null;             尝试             {                 UserCredential凭证;                // string strUploadFolder = ConfigurationManager.AppSettings [“UploadFolder”];

            string credPath = AppDomain.CurrentDomain.BaseDirectory;
            credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
               new ClientSecrets
               {
                   ClientId = ConfigurationManager.AppSettings["client_id"],
                   ClientSecret = ConfigurationManager.AppSettings["client_secret"],
               },
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;

            // Create Drive API service.
            service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });
        }
        catch (Exception ex)
        {
            throw;
        }
        return service;

1 个答案:

答案 0 :(得分:0)

您使用的代码适用于已安装的应用程序。使用安装应用程序,浏览器窗口在计算机上打开。

使用Web应用程序需要在用户计算机上打开用户同意的浏览器窗口

documentation on mvc authentication

using System;
using System.Web.Mvc;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Mvc;
using Google.Apis.Drive.v2;
using Google.Apis.Util.Store;

namespace Google.Apis.Sample.MVC4
{
    public class AppFlowMetadata : FlowMetadata
    {
        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")
                });

        public override string GetUserId(Controller controller)
        {
            // In this sample we use the session to store the user identifiers.
            // That's not the best practice, because you should have a logic to identify
            // a user. You might want to use "OpenID Connect".
            // You can read more about the protocol in the following link:
            // https://developers.google.com/accounts/docs/OAuth2Login.
            var user = controller.Session["user"];
            if (user == null)
            {
                user = Guid.NewGuid();
                controller.Session["user"] = user;
            }
            return user.ToString();

        }

        public override IAuthorizationCodeFlow Flow
        {
            get { return flow; }
        }
    }
}