0

I have made an application for sending the calendar events to the client in my application. My Code is:

namespace NotInstalled.Service
{
    public class AppFlowMetadata : FlowMetadata
    {
        private static readonly IAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId = "CLIENT_ID",
                    ClientSecret = "CLIENT_SECRET"
                },
                Scopes = new[] { "https://www.googleapis.com/auth/calendar.events" },
                DataStore = new FileDataStore("Calendar.Api.Auth.Store")
            });

        public override string GetUserId(Controller controller)
        {
            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; }
        }
    }
}

And my code for sending the events is :

 public async Task<ActionResult> Contact(CancellationToken cancellationToken)
        {
            var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
            AuthorizeAsync(cancellationToken);

            if (googleSerive.RedirectUri != null)
            {
                return Redirect(googleSerive.RedirectUri);
            }

            if (result.Credential != null)
            {
                var service = new CalendarService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName = ApplicationName,
                });

               //Events Snending Code Here which works perfectly

                return View();
            }
           
                return View();
            
        }

and my auth call back controller is :

 public class AuthCallbackController : Google.Apis.Auth.OAuth2.Mvc.Controllers.AuthCallbackController
    {
        protected override Google.Apis.Auth.OAuth2.Mvc.FlowMetadata FlowData
        {
            get { return new AppFlowMetadata(); }
        }
    }

Now Each Time I open the application and go through Contact Method in a different browser It asks me to login with my gamil used in this Google API.I have already authorize my account and also i have token for it.How can I use the same token for usercredentials and use the service.

Biraz Dahal
  • 361
  • 1
  • 5
  • 20
  • Are the clients from your same domain? Or is it an open App? – Jescanellas Sep 30 '20 at 13:35
  • @Jescanellas It's an open app . Like there are three users Admin,,Company and Individual. The Company hires the Individual for an event that that event should be set to individual's google calendar.There might be multiple number of comapny and individual. – Biraz Dahal Sep 30 '20 at 17:05
  • I think the only way would be storing the access token as a cookie on the client side. What is the purpose of using the same session through different browsers? – Jescanellas Oct 01 '20 at 08:40
  • 1
    @Jescanellas I got the answer here https://stackoverflow.com/questions/38390197/how-to-create-a-instance-of-usercredential-if-i-already-have-the-value-of-access – Biraz Dahal Oct 01 '20 at 11:14

1 Answers1

1

This is what I was looking for. The value of access token and refresh token can be found on the local store after authenticating for the first time DataStore = new FileDataStore(YOUR_PATH_TO_LOCAL_STORE)

var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId = "CLIENT_ID",
                    ClientSecret = "CLIENT_SECRET"
                },
                Scopes = new[] { "https://www.googleapis.com/auth/calendar.events" },
                DataStore = new FileDataStore("Calendar.Api.Auth.Store")
            });

            var token = new TokenResponse
            {
                AccessToken = "ACCESS_TOKEN",
                RefreshToken = "REFRESH_TOKEN"
            };

            var credentals = new UserCredential(flow, Environment.UserName, token);

            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credentals,
                ApplicationName = ApplicationName,
            });

 //Events Snending Code Here which works perfectly

Biraz Dahal
  • 361
  • 1
  • 5
  • 20