发布后谷歌登录不起作用

本文关键字:登录 不起作用 谷歌 | 更新日期: 2023-09-27 17:58:51

我使用VS2015,C#。

我在谷歌登录时遇到问题。从我的调试配置(localhost)来看,一切都很好。发布到服务器后,谷歌登录窗口根本不会打开。并且没有抛出任何异常。这是我的代码:

[AllowAnonymous]
public async Task LoginWithGoogle()
{
    HttpRequest request = System.Web.HttpContext.Current.Request;
    string redirectUri = ConfigurationReaderHelper.GetGoogleRedirectUri();
    try
    {            
        ClientSecrets secrets = new ClientSecrets
        {
            ClientId = "***",
            ClientSecret = "***"
        };
        IEnumerable<string> scopes = new[] { PlusService.Scope.UserinfoEmail, PlusService.Scope.UserinfoProfile };
        GoogleStorageCredentials storage = new GoogleStorageCredentials();
        dsAuthorizationBroker.RedirectUri = redirectUri;
        UserCredential credential = await dsAuthorizationBroker.AuthorizeAsync(secrets,
            scopes, "", CancellationToken.None, storage);
    }
    catch(Exception ex)
    {
        throw ex;
    }            
}

//just getting value from applicationSettings - web.config
            public static string GetGoogleRedirectUri()
            {
    #if DEBUG
                return GetValueFromApplicationSettings("RedirectUriDEBUG");
    #elif PRODUKCIJA
                return GetValueFromApplicationSettings("RedirectUriSERVER");            
    #endif
            }

当然,我将服务器的地址添加到了原始uri中,也添加到了谷歌控制台上为开发人员授权的重定向uri中。(就像我对localhost所做的那样)。我只是不明白怎么了,为什么登录窗口没有打开?

编辑:

添加类dsAuthorizationBroker(在我的第一篇文章中缺失-很抱歉):

namespace Notes
{
    public class dsAuthorizationBroker : GoogleWebAuthorizationBroker
    {
        public static string RedirectUri;
        public static async Task<UserCredential> AuthorizeAsync(
            ClientSecrets clientSecrets,
            IEnumerable<string> scopes,
            string user,
            CancellationToken taskCancellationToken,
            IDataStore dataStore = null)
        {
            var initializer = new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = clientSecrets,
            };
            return await AuthorizeAsyncCore(initializer, scopes, user,
                taskCancellationToken, dataStore).ConfigureAwait(false);
        }
        private static async Task<UserCredential> AuthorizeAsyncCore(
            GoogleAuthorizationCodeFlow.Initializer initializer,
            IEnumerable<string> scopes,
            string user,
            CancellationToken taskCancellationToken,
            IDataStore dataStore)
        {
            initializer.Scopes = scopes;
            initializer.DataStore = dataStore ?? new FileDataStore(Folder);
            var flow = new dsAuthorizationCodeFlow(initializer);
            return await new AuthorizationCodeInstalledApp(flow,
                new LocalServerCodeReceiver())
                .AuthorizeAsync(user, taskCancellationToken).ConfigureAwait(false);
        }
    }

    public class dsAuthorizationCodeFlow : GoogleAuthorizationCodeFlow
    {        
        public dsAuthorizationCodeFlow(Initializer initializer)
            : base(initializer) { }
        public override AuthorizationCodeRequestUrl
                       CreateAuthorizationCodeRequest(string redirectUri)
        {            
            return base.CreateAuthorizationCodeRequest(dsAuthorizationBroker.RedirectUri);            
        }
    }
}

发布后谷歌登录不起作用

public static async Task<UserCredential> AuthorizeAsync

此方法已在GoogleWebAuthorizationBroker中声明,因此,如果您希望此函数的实现优先于基本实现,则需要使用new关键字。

public new static async Task<UserCredential> AuthorizeAsync

这就是为什么我认为你的日志记录在之前停止了线路

UserCredential credential = await dsAuthorizationBroker.AuthorizeAsync

此时,它正在调用基本实现。

除此之外,我通常倾向于使用DotNetOpenAuth与谷歌进行交互,下面有很多简单的例子,比如这里和这里。。但是,如果你真的想只使用Google Apis推出自己的产品,那么这是启动的最佳位置