2
votes

I try to authenticate my user using Google authentication services When i run this code on local server its working fine (It redirects to google login and after successful login its hit call back on redirectPath). But when publish this code on Production server then its not working. When I debug this code, I found its redirect and open the google login page on hosted environment(Where application is published).

here is my code - Please help

string redirecrPath = "http://localhost:1212/Admin/YouTubeIntegration/Success";

            UserCredential credential;
            using (var stream = new FileStream(Server.MapPath("/XmlFile/client_secrets.json"), FileMode.Open, FileAccess.Read))
            {
                GoogleAuth.RedirectUri = redirecrPath;
                credential = await GoogleAuth.AuthorizeAsync(
                             GoogleClientSecrets.Load(stream).Secrets,
                             new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeReadonly, YouTubeService.Scope.YoutubeUpload },
                             "user",
                             CancellationToken.None,
                             new FileDataStore(this.GetType().ToString())
                         );
            }

Please let me know if you need more information. Thanks in Advance

1
Hi, I have tried with actual redirect path also string redirecrPath = "dummy.production.com/Admin/YouTubeIntegration/Success"; - Danish Ajazi
makes sure you have the corect redirect uri set in google developer console - DaImTo
@DaImTo In case of production uri path Its opening on server machine but not on client machine - Danish Ajazi
And local host and your server are two diffrent places. Localhost will be localhost/whatever. server will be yourwebsite/whatever its to diffrent redirect uris - DaImTo
you also appear to be using the code for installed clients you should be using the web code developers.google.com/api-client-library/dotnet/guide/… - DaImTo

1 Answers

1
votes

The code to login from a web page is not the same as the code to login with an installed application. Installed applications can spawn the login screen directly on the current machine. If you tried to do that on a webserver it wouldnt work the following is the code for using web login

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

copied from here