8

I use VS2015, C#.

I have a problem with Google login. From my debug configuration (localhost) everything works fine. After publishing to the server, google login window simply doesn't get opened. And no exception is thrown. Here is my code:

[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
            }

Of course I added server's address to the origin uri and also to the authorised redirect uri on the google console for developers. (just like I did for the localhost). I just don't get it what is wrong, why login windows doesn't get opened?

EDIT:

Adding class dsAuthorizationBroker (was missing from my first post - sorry on that one):

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);            
        }
    }
}
FrenkyB
  • 6,625
  • 14
  • 67
  • 114
  • 1
    Put some debug statments to find out what last line was executed. That will help to zero down on root cause. I'm guessing it's not getting proper value from AppSettings – JWP Jul 27 '16 at 17:11
  • If You have published it to virtual machine than try to see the errors from Event Viewer – Lakhtey Jul 27 '16 at 17:18
  • @JohnPeters - I've added logging. I successfuly log one line before UserCredential credential = .... redirect uri (from the log) is uri which I have in return URI on google console. – FrenkyB Jul 27 '16 at 17:24
  • Tell us more about this line: dsAuthorizationBroker.RedirectUri = redirectUri; – JWP Jul 27 '16 at 19:11
  • I've added a class in my post. Is using google sign in - like it is in my code - for free or do I have to register and pay for it? – FrenkyB Jul 27 '16 at 19:21
  • Check the browser console? – Paul C Aug 18 '16 at 13:42
  • Nothing, no exception in console. – FrenkyB Aug 18 '16 at 17:27

1 Answers1

3
public static async Task<UserCredential> AuthorizeAsync

This method is already declared in GoogleWebAuthorizationBroker and therefore if you intend for your implementation of this function to take precedence over the base implementation, then you need to use the new keyword.

public new static async Task<UserCredential> AuthorizeAsync

This is why I assume you logging stops the line before

UserCredential credential = await dsAuthorizationBroker.AuthorizeAsync

At this point, it is calling the base implementation.

Aside from this, I generally tend to use DotNetOpenAuth for interacting with Google and there are plenty of simple examples to follow, like here and here.. but if you really want to roll your own using Google Apis only then this is best place to start

Community
  • 1
  • 1
timkly
  • 793
  • 6
  • 14
  • thanks for answer. I'll try out both, your answer and DotNetOpenAuth. If I may ask you - perhaps you know for a working example with DotNetOpenAuth with Twitter / Facebook? – FrenkyB Aug 18 '16 at 09:11
  • I can add some code over the next few days but can I ask what your use case is? Do you only need to authorise or do you want emails etc as well? If so, there are a few more hoops to jump through with Twitter. In saying that, here is the [Sample Code/Examples](https://github.com/DotNetOpenAuth/DotNetOpenAuth.Samples) for DotNetOpenAuth to get you started, which has a twitter example. – timkly Aug 19 '16 at 02:23
  • All I need is login - get users email. If you have any more test cases for social logins, you are warmly welcome. – FrenkyB Aug 19 '16 at 09:52