8

I have created a project in console.developers.google.com to use Google Calendar API. there we need to generate credential and select application type

For Localhost and application type other following is the Json which works fine.

{
    "installed": {
    "client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
    "project_id": "xxxxxx-00000",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "xxxxxxxxxxxx",
    "redirect_uris": [ "urn:ietf:wg:oauth:2.0:oob", "http://localhost" ]
    }
}

And for Application type Web Application to Host on Server Few parameters has different Values in Json as shown below.

{
    "web": {
    "client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
    "project_id": "xxxxxxxx-99999",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "xxxxxxxxxxxxxxxxxxxxxxx",
    "redirect_uris": [ "http://demo.mydemo.com" ],
    "javascript_origins": [ "http://demo.mydemo.com" ]
    }
}

Here when user(Only those who have integrated Google Calendar) try to create an event Google should take consent of which google account to use and ask to read personal data before redirecting to the redirected page. On giving consent it will add the provided data in the selected google account's calendar. The Problem is It is not redirecting the user to Oauth Authentication i.e. The Consent Screen.

Image Of Consent Screen Can anyone help me to solve this issue?

Error is as follows

System.AggregateException: One or more errors occurred. ---> System.NotSupportedException: Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=xxxxxxxxxxxx-vqi4faduqgcpt3d2ruv53ltgr0rkgfsb.apps.googleusercontent.com&redirect_uri=http:%2F%2Flocalhost:53998%2Fauthorize%2F&scope=https:%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar" for authorization. See inner exception for details. ---> System.ComponentModel.Win32Exception: Access is denied
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.<ReceiveCodeAsync>d__8.MoveNext()
   --- End of inner exception stack trace ---
   at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.<ReceiveCodeAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.<AuthorizeAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.<AuthorizeAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.<AuthorizeAsync>d__1.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at KriyaCRM.Controllers.ActivitiesController.GetCalendarConnection(Activities objact, String Operation)
---> (Inner Exception #0) System.NotSupportedException: Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=430760523223-vqi4faduqgcpt3d2ruv53ltgr0rkgfsb.apps.googleusercontent.com&redirect_uri=http:%2F%2Flocalhost:53998%2Fauthorize%2F&scope=https:%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar" for authorization. See inner exception for details. ---> System.ComponentModel.Win32Exception: Access is denied
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.<ReceiveCodeAsync>d__8.MoveNext()
   --- End of inner exception stack trace ---
   at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.<ReceiveCodeAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.<AuthorizeAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.<AuthorizeAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.<AuthorizeAsync>d__1.MoveNext()<---
--> Important Link related to API : https://www.codeproject.com/Articles/1183150/Securing-ASP-NET-Web-API-using-Custom-Token-Based <--
Divya
  • 1,203
  • 2
  • 13
  • 31
  • 1
    What exactly do you mean by "it is not redirecting me" - what is not redirecting who? Please provide more details as to exactly what you're doing, including your code. – Jon Skeet Jun 30 '17 at 09:18
  • I've Updated the Question. If you still have confusion you can ask. Thank You – Divya Jun 30 '17 at 09:49
  • Okay, so what exactly *is* happening when you sign in? Is it redirecting to somewhere else? What does the response look like? – Jon Skeet Jun 30 '17 at 13:23
  • Actually neither it is redirecting to authenticate nor it is processing further. – Divya Jun 30 '17 at 13:30
  • I've updated Error in the Question – Divya Jun 30 '17 at 13:36
  • 1
    Hang on - are you trying to use `GoogleWebAuthorizationBroker` from a web application? That won't work - `GoogleWebAuthorizationBroker` is for *local* applications - it launches a browser locally to the server. It sounds like you want Google.Apis.Auth.Mvc. (This is why it's important to show the code you're using...) – Jon Skeet Jun 30 '17 at 13:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148058/discussion-between-divya-and-jon-skeet). – Divya Jun 30 '17 at 13:42
  • @Jon Skeet - I'm still stuck at one point and not getting any proper solution. If you can then please provide further solution. I think you have the answer as you guided in right direction. – Divya Sep 12 '17 at 06:12

1 Answers1

1
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
                                                                             scopes,
                                                                             userName,
                                                                             CancellationToken.None,
                                                                             new FileDataStore(credPath, true)).Result;

Is used for installed applications. It will probably work when you are running your code in debug mode because you have access to open a web browser on your local-machine. However when you try to host it on a server its not going to work because you don't have access to open a web browser on the server what you want to do is open a web browser on the clients machine.

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

You should try following this article. Google Oauth2 asp.net MVC

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I tried this but this isn't working as when we use it for Local i.e. `var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, scopes, userName, CancellationToken.None, new FileDataStore(credPath, true)).Result;` It gives one Token – Divya Jul 12 '17 at 05:44
  • and with that we can Add/Modify any Events but `IAuthorizationCodeFlow` don't provide that. So i'm stuck at this point. What else is needed to add. There is something missing for sure – Divya Jul 12 '17 at 05:48
  • I'm working on a .net6 project that already uses Microsoft Identity. Any idea how to handle the OpenID for Google Drive API at the same time? I can get it working by using `GoogleWebAuthorizationBroker.AuthorizeAsync`, but the `AddGoogleOpenIdConnect` sort of conflicts with the Identity login – Kappacake Oct 06 '22 at 22:47
  • 1
    @Kappacake Unforuantatly microsoft identify and the google .net client library [mvc example](https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web-applications-aspnet-mvc) do not play well together. I have yet to get it to work. They clobber each others cookies. What i need to do is try and create an overload method for Microsoft Identity that will support the Google .net client library. Its just not something I have had time to do yet. Check issue [#2202](https://github.com/googleapis/google-api-dotnet-client/issues/2202) – Linda Lawton - DaImTo Oct 07 '22 at 09:56
  • The main issue is that Microsoft Identity is just that Identity signin and authencation, where the google .net client library is Oauth2 and authorization. Its two different things. – Linda Lawton - DaImTo Oct 07 '22 at 10:00
  • Just want to clarify that I was asking about .net core, so the [ASP.NET CORE 3 example](https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web-applications-asp.net-core-3) rather than the [mvc example](https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web-applications-aspnet-mvc). I also raised an issue on the Google API github page in case you want to chip in: https://github.com/googleapis/google-api-dotnet-client/issues/2225 – Kappacake Oct 07 '22 at 13:25
  • Chimed in not sure how much it will help. I have tried to bring this to their attention and mostly gotten this is how it works. Pick one. – Linda Lawton - DaImTo Oct 07 '22 at 13:59